ITANetAudioStreamingServer.cpp 7.56 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;
Anne's avatar
Anne committed
62 63
}

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

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

Anne's avatar
Anne committed
72
	m_pConnection = m_pNetAudioServer->GetConnection( );
73

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

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

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
82
	bool bOK = false;
Anne's avatar
Anne committed
83
	m_oServerParams.iRingBufferSize = oClientParams.iRingBufferSize;
Anne's avatar
Anne committed
84
	m_iClientRingBufferFreeSamples = m_oServerParams.iRingBufferSize;
Anne's avatar
Anne committed
85
	if ( m_oServerParams == oClientParams )
Anne's avatar
Anne committed
86
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
87
		bOK = true;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
88
#ifdef NET_AUDIO_SHOW_TRAFFIC
89
		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
90 91 92
	}
	else
	{
93
		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
94
#endif
Anne's avatar
Anne committed
95
	}
96 97 98

	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
99
	m_pServerLogger->setOutputFile( paras );
Anne's avatar
Anne committed
100

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

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


Anne's avatar
Anne committed
108 109
	if ( bOK )
		Run( );
110

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

Anne's avatar
Anne committed
114
bool CITANetAudioStreamingServer::LoopBody( )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
115
{
116
	m_pMessage->ResetMessage( );
Anne's avatar
Anne committed
117 118 119
	ITAServerLog oLog;
	oLog.uiBlockId = ++iServerBlockId;
	int iMsgType;
Anne's avatar
Anne committed
120
	// Sending Samples 
Anne Heimes's avatar
Anne Heimes committed
121
	if ( m_iClientRingBufferFreeSamples >= int( m_pInputStream->GetBlocklength() ) )
Anne's avatar
Anne committed
122 123
	{
		// Send Samples
Anne Heimes's avatar
Anne Heimes committed
124
		for ( int i = 0; i < int( m_pInputStream->GetNumberOfChannels( ) ); i++ )
Anne's avatar
Anne committed
125
		{
Anne Heimes's avatar
Anne Heimes committed
126 127 128 129 130 131 132 133 134 135 136 137
			ITAStreamInfo oStreamInfo;
			oStreamInfo.nSamples = m_sfTempTransmitBuffer.GetLength( );
			const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
			if ( pfData != 0 )
				m_sfTempTransmitBuffer[ i ].write( pfData, m_sfTempTransmitBuffer.GetLength( ) );
		}
		m_pInputStream->IncrementBlockPointer( );
		iMsgType = CITANetAudioProtocol::NP_SERVER_SENDING_SAMPLES;
		m_pMessage->SetMessageType( iMsgType );
		m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
		m_pMessage->WriteMessage( );
		m_iClientRingBufferFreeSamples -= m_sfTempTransmitBuffer.GetLength( );
Anne's avatar
Anne committed
138
#ifdef NET_AUDIO_SHOW_TRAFFIC
Anne Heimes's avatar
Anne Heimes committed
139 140
		vstr::out( ) << "[ITANetAudioStreamingServer] Transmitted " << m_sfTempTransmitBuffer.GetLength( ) << " samples for "
			<< m_pInputStream->GetNumberOfChannels( ) << " channels" << std::endl;
Anne's avatar
Anne committed
141 142 143 144 145
#endif
	}
	else
	{
		// Waiting for Trigger
Anne's avatar
Anne committed
146 147
		iMsgType = CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE_SAMPLES;
		m_pMessage->SetMessageType( iMsgType );
Anne's avatar
Anne committed
148 149 150 151 152 153
		m_pMessage->WriteMessage( );

#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
	}
154 155 156

	oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
	oLog.iProtocolStatus = iMsgType;
Anne's avatar
Anne committed
157 158
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
	m_pServerLogger->log( oLog );
Anne's avatar
Anne committed
159 160

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

163
	if ( m_pMessage->ReadMessage( 1 ) )
164
	{
Anne's avatar
Anne committed
165 166
		ITAServerLog oLog;
		oLog.uiBlockId = ++iServerBlockId;
Anne's avatar
Anne committed
167 168
		int iMsgType = m_pMessage->GetMessageType( );
		switch ( iMsgType )
169
		{
Anne's avatar
Anne committed
170
			case CITANetAudioProtocol::NP_CLIENT_SENDING_RINGBUFFER_FREE_SAMPLES:
171
			{
Anne's avatar
Anne committed
172
				m_iClientRingBufferFreeSamples = m_pMessage->ReadInt( );	
Anne's avatar
Anne committed
173 174 175 176
				break;
			}
			case CITANetAudioProtocol::NP_CLIENT_CLOSE:
			{
Anne Heimes's avatar
Anne Heimes committed
177 178
				m_pMessage->SetMessageType( CITANetAudioProtocol::NP_SERVER_CLOSE );
				m_pMessage->WriteMessage( );
Anne's avatar
Anne committed
179
				StopGently( false );
Anne's avatar
Anne committed
180
				m_pConnection = NULL;
Anne's avatar
Anne committed
181
				Stop( );
Anne's avatar
Anne committed
182
				break;
Anne's avatar
Anne committed
183 184 185 186 187 188
			}
			default:
			{
				vstr::out( ) << "[ITANetAudioStreamingServer] Unkown protocol type : " << iMsgType << std::endl;
				break;
			}
189 190 191
		}
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.iProtocolStatus = iMsgType;
Anne's avatar
Anne committed
192 193
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
		m_pServerLogger->log( oLog );
194
	}
195 196


Anne's avatar
Anne committed
197
	return false;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
198 199
}

200 201
void CITANetAudioStreamingServer::SetInputStream( ITADatasource* pInStream )
{
Anne's avatar
Anne committed
202
	if ( VistaThreadLoop::IsRunning( ) )
203 204 205
		ITA_EXCEPT1( MODAL_EXCEPTION, "Streaming loop already running, can not change input stream" );

	m_pInputStream = pInStream;
Anne's avatar
Anne committed
206 207 208
	m_oServerParams.dSampleRate = m_pInputStream->GetSampleRate( );
	m_oServerParams.iBlockSize = m_pInputStream->GetBlocklength( );
	m_oServerParams.iChannels = m_pInputStream->GetNumberOfChannels( );
209 210
}

Anne's avatar
Anne committed
211
ITADatasource* CITANetAudioStreamingServer::GetInputStream( ) const
Anne's avatar
Anne committed
212 213 214 215
{
	return m_pInputStream;
}

Anne's avatar
Anne committed
216
int CITANetAudioStreamingServer::GetNetStreamBlocklength( ) const
217
{
Anne's avatar
Anne committed
218
	return m_sfTempTransmitBuffer.GetLength( );
219 220
}

Anne's avatar
Anne committed
221
int CITANetAudioStreamingServer::GetNetStreamNumberOfChannels( ) const
222
{
Anne's avatar
Anne committed
223
	return m_sfTempTransmitBuffer.channels( );
224 225
}

Anne's avatar
Anne committed
226
void CITANetAudioStreamingServer::SetAutomaticUpdateRate( )
227 228 229 230
{
	m_iUpdateStrategy = AUTO;
}

Anne's avatar
Anne committed
231
bool CITANetAudioStreamingServer::IsClientConnected( ) const
232
{
Anne's avatar
Anne committed
233
	return m_pNetAudioServer->IsConnected( );
234 235
}

Anne's avatar
Anne committed
236
std::string CITANetAudioStreamingServer::GetNetworkAddress( ) const
237
{
Anne's avatar
Anne committed
238
	return m_pNetAudioServer->GetServerAddress( );
239 240
}

Anne's avatar
Anne committed
241
int CITANetAudioStreamingServer::GetNetworkPort( ) const
242
{
Anne's avatar
Anne committed
243
	return m_pNetAudioServer->GetNetworkPort( );
244 245
}

Anne's avatar
Anne committed
246
void CITANetAudioStreamingServer::Stop( )
Anne's avatar
Anne committed
247
{
Anne's avatar
Anne committed
248
	delete m_pServerLogger;
Anne's avatar
Anne committed
249
	m_pNetAudioServer->Stop( );
Anne's avatar
Anne committed
250
}