ITANetAudioStreamingServer.cpp 9.57 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 > {};

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

67 68 69 70 71 72
CITANetAudioStreamingServer::~CITANetAudioStreamingServer()
{
	delete m_pNetAudioServer;
	delete m_pServerLogger;
}

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

Anne's avatar
Anne committed
78
	if ( !m_pNetAudioServer->Start( sAddress, iPort ) ) // blocking
79 80
		return false;

Anne's avatar
Anne committed
81
	m_pConnection = m_pNetAudioServer->GetConnection( );
82

Anne's avatar
Anne committed
83 84
	m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag( ) );
	m_pMessage->ResetMessage( );
85
	m_pMessage->SetConnection( m_pConnection );
86
	while ( !m_pMessage->ReadMessage( 0 ) ); //blocking
87

Anne's avatar
Anne committed
88 89
	assert( m_pMessage->GetMessageType( ) == CITANetAudioProtocol::NP_CLIENT_OPEN );
	CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters( );
90

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
91
	bool bOK = false;
Anne's avatar
Anne committed
92
	m_oServerParams.iRingBufferSize = oClientParams.iRingBufferSize;
93
	m_oServerParams.iTargetSampleLatency = oClientParams.iTargetSampleLatency;
Anne Heimes's avatar
Anne Heimes committed
94
	m_oServerParams.iBlockSize = oClientParams.iBlockSize;
95 96 97
	m_iClientRingBufferFreeSamples = m_oServerParams.iTargetSampleLatency;

	m_dLastTimeStamp = ITAClock::getDefaultClock()->getTime();
Anne's avatar
Anne committed
98
	if ( m_oServerParams == oClientParams )
Anne's avatar
Anne committed
99
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
100
		bOK = true;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
101
#ifdef NET_AUDIO_SHOW_TRAFFIC
102
		vstr::out() << "[ITANetAudioStreamingServer] Server and client parameters matched. Will resume with streaming" << std::endl;
Anne Heimes's avatar
Anne Heimes committed
103
#endif
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
104 105 106
	}
	else
	{
Anne Heimes's avatar
Anne Heimes committed
107
#ifdef NET_AUDIO_SHOW_TRAFFIC
108
		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
109
#endif
Anne's avatar
Anne committed
110
	}
111

Anne Heimes's avatar
Anne Heimes committed
112
	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("_tl") + std::to_string(m_oServerParams.iTargetSampleLatency) + std::string(".txt");
113
	m_pServerLogger = new ITABufferedDataLoggerImplServer( );
Anne's avatar
Anne committed
114
	m_pServerLogger->setOutputFile( paras );
Anne's avatar
Anne committed
115

Anne's avatar
Anne committed
116
	m_pMessage->SetMessageType( CITANetAudioProtocol::NP_SERVER_OPEN );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
117
	m_pMessage->WriteBool( bOK );
Anne's avatar
Anne committed
118
	m_pMessage->WriteMessage( );
119

Anne Heimes's avatar
Anne Heimes committed
120 121 122
	m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels( ), m_oServerParams.iRingBufferSize, true );


Anne's avatar
Anne committed
123 124
	if ( bOK )
		Run( );
125

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
126
	return bOK;
Anne's avatar
Anne committed
127 128
}

Anne's avatar
Anne committed
129
bool CITANetAudioStreamingServer::LoopBody( )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
130
{
131
	bool bAskClient = false;
132
	m_pMessage->ResetMessage( );
Anne's avatar
Anne committed
133 134 135
	ITAServerLog oLog;
	oLog.uiBlockId = ++iServerBlockId;
	int iMsgType;
Anne's avatar
Anne committed
136
	// Sending Samples 
137
	unsigned int uiBlockLength = m_pInputStream->GetBlocklength( );
138 139 140 141
	int iClientRingBufferTargetFreeSamples = m_iClientRingBufferFreeSamples - (m_oServerParams.iRingBufferSize - m_oServerParams.iTargetSampleLatency);
	if (iClientRingBufferTargetFreeSamples < 0)
		iClientRingBufferTargetFreeSamples = 0;
	if (iClientRingBufferTargetFreeSamples >= uiBlockLength)
Anne's avatar
Anne committed
142 143
	{
		// Send Samples
144
		int iSendBlocks = iClientRingBufferTargetFreeSamples / uiBlockLength;
145 146
		bAskClient = true;
		
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		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
162 163
		}
		iMsgType = CITANetAudioProtocol::NP_SERVER_SENDING_SAMPLES;
164 165 166
		m_pMessage->SetMessageType(iMsgType);
		m_pMessage->WriteSampleFrame(&m_sfTempTransmitBuffer);
		m_pMessage->WriteMessage();
167
		m_iClientRingBufferFreeSamples -= iSendBlocks * uiBlockLength;
Anne's avatar
Anne committed
168
#ifdef NET_AUDIO_SHOW_TRAFFIC
169 170
		vstr::out() << "[ITANetAudioStreamingServer] Transmitted " << iSendSamples << " samples for "
			<< m_pInputStream->GetNumberOfChannels() << " channels" << std::endl;
Anne's avatar
Anne committed
171
#endif
172 173 174 175
	}
	else
		bAskClient = true;

176

Anne's avatar
Anne committed
177 178

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

181
	if ( m_pMessage->ReadMessage( 1 ) )
182
	{
Anne's avatar
Anne committed
183 184
		ITAServerLog oLog;
		oLog.uiBlockId = ++iServerBlockId;
Anne's avatar
Anne committed
185 186
		int iMsgType = m_pMessage->GetMessageType( );
		switch ( iMsgType )
187
		{
Anne's avatar
Anne committed
188
			case CITANetAudioProtocol::NP_CLIENT_SENDING_RINGBUFFER_FREE_SAMPLES:
189
			{
190
				m_iClientRingBufferFreeSamples = m_pMessage->ReadInt( );
191
				m_dLastTimeStamp = ITAClock::getDefaultClock()->getTime();
192
				bAskClient = false;
Anne's avatar
Anne committed
193 194 195 196
				break;
			}
			case CITANetAudioProtocol::NP_CLIENT_CLOSE:
			{
197 198 199 200
				m_pMessage->ResetMessage();
				m_pMessage->SetMessageType( CITANetAudioProtocol::NP_SERVER_CLOSE );
				m_pMessage->WriteMessage();

Anne's avatar
Anne committed
201
				StopGently( false );
Anne's avatar
Anne committed
202
				m_pConnection = NULL;
Anne's avatar
Anne committed
203
				Stop( );
204
				bAskClient = false;
Anne's avatar
Anne committed
205
				break;
Anne's avatar
Anne committed
206 207 208 209 210 211
			}
			default:
			{
				vstr::out( ) << "[ITANetAudioStreamingServer] Unkown protocol type : " << iMsgType << std::endl;
				break;
			}
212 213 214
		}
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.iProtocolStatus = iMsgType;
Anne's avatar
Anne committed
215 216
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
		m_pServerLogger->log( oLog );
217
	}
218 219
	else
	{
Anne Heimes's avatar
Anne Heimes committed
220
		/*
221 222
		ITAServerLog oLog;
		oLog.uiBlockId = ++iServerBlockId;
Anne Heimes's avatar
bugfix  
Anne Heimes committed
223
		// Neue Samples, bei ca 1ms warten
224 225 226
		double dTimeDiff = ITAClock::getDefaultClock()->getTime() - m_dLastTimeStamp;
		float dSamples = dTimeDiff * m_pInputStream->GetSampleRate();
		m_dLastTimeStamp = ITAClock::getDefaultClock()->getTime();
227 228 229
		int iSamples = ( int ) dSamples;
		int offset = iSamples % m_pInputStream->GetBlocklength( );
		m_iClientRingBufferFreeSamples += iSamples - offset;
230 231 232 233
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.iProtocolStatus = 555;
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock()->getTime();
		m_pServerLogger->log(oLog);
Anne Heimes's avatar
Anne Heimes committed
234
		*/
235
	}
236

237
	bAskClient = false;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	if (bAskClient)
	{
#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();
		iMsgType = CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE_SAMPLES;
		m_pMessage->SetMessageType(iMsgType);
		m_pMessage->WriteBool(true);
		m_pMessage->WriteMessage();
		oLog.iProtocolStatus = iMsgType;
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock()->getTime();
		m_pServerLogger->log(oLog);
	}


Anne's avatar
Anne committed
257
	return false;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
258 259
}

260 261
void CITANetAudioStreamingServer::SetInputStream( ITADatasource* pInStream )
{
Anne's avatar
Anne committed
262
	if ( VistaThreadLoop::IsRunning( ) )
263 264 265
		ITA_EXCEPT1( MODAL_EXCEPTION, "Streaming loop already running, can not change input stream" );

	m_pInputStream = pInStream;
Anne's avatar
Anne committed
266 267 268
	m_oServerParams.dSampleRate = m_pInputStream->GetSampleRate( );
	m_oServerParams.iBlockSize = m_pInputStream->GetBlocklength( );
	m_oServerParams.iChannels = m_pInputStream->GetNumberOfChannels( );
269 270
}

Anne's avatar
Anne committed
271
ITADatasource* CITANetAudioStreamingServer::GetInputStream( ) const
Anne's avatar
Anne committed
272 273 274 275
{
	return m_pInputStream;
}

Anne's avatar
Anne committed
276
int CITANetAudioStreamingServer::GetNetStreamBlocklength( ) const
277
{
Anne's avatar
Anne committed
278
	return m_sfTempTransmitBuffer.GetLength( );
279 280
}

Anne's avatar
Anne committed
281
int CITANetAudioStreamingServer::GetNetStreamNumberOfChannels( ) const
282
{
Anne's avatar
Anne committed
283
	return m_sfTempTransmitBuffer.channels( );
284 285
}

Anne's avatar
Anne committed
286
void CITANetAudioStreamingServer::SetAutomaticUpdateRate( )
287 288 289 290
{
	m_iUpdateStrategy = AUTO;
}

Anne's avatar
Anne committed
291
bool CITANetAudioStreamingServer::IsClientConnected( ) const
292
{
Anne's avatar
Anne committed
293
	return m_pNetAudioServer->IsConnected( );
294 295
}

Anne's avatar
Anne committed
296
std::string CITANetAudioStreamingServer::GetNetworkAddress( ) const
297
{
Anne's avatar
Anne committed
298
	return m_pNetAudioServer->GetServerAddress( );
299 300
}

Anne's avatar
Anne committed
301
int CITANetAudioStreamingServer::GetNetworkPort( ) const
302
{
Anne's avatar
Anne committed
303
	return m_pNetAudioServer->GetNetworkPort( );
304 305
}

Anne's avatar
Anne committed
306
void CITANetAudioStreamingServer::Stop( )
Anne's avatar
Anne committed
307
{
Anne's avatar
Anne committed
308
	m_pNetAudioServer->Stop( );
Anne's avatar
Anne committed
309
}