ITANetAudioStreamingClient.cpp 6.21 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Running  
Dipl.-Ing. Jonas Stienen committed
1 2 3 4
#include <ITANetAudioStreamingClient.h>

#include <ITANetAudioClient.h>
#include <ITANetAudioMessage.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
5
#include <ITANetAudioStream.h>
6 7
#include <ITADataLog.h>
#include <ITAClock.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
8 9

#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
10
#include <VistaBase/VistaStreamUtils.h>
11
#include <VistaBase/VistaTimeUtils.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
//! Audio streaming log item
struct ITAClientLog : public ITALogDataBase
{
	inline static std::ostream& outputDesc( std::ostream& os )
	{
		os << "BlockId";
		os << "\t" << "WorldTimeStamp";
		os << "\t" << "ProtocolStatus";
		os << "\t" << "FreeSamples";
		os << "\t" << "Channel";
		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 << "\t" << iChannel;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId; //!< Block identifier (audio streaming)
	double dWorldTimeStamp;
	int iProtocolStatus; //!< ... usw
	int iFreeSamples;
	int iChannel;

};

class ITABufferedDataLoggerImplClient : public ITABufferedDataLogger < ITAClientLog > {};


Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
49
CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pParent )
50
	: m_pStream( pParent )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
51 52
	, m_pConnection( NULL )
	, m_bStopIndicated( false )
53
	, m_bStopped( false )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
54
{
Dipl.-Ing. Jonas Stienen's avatar
Running  
Dipl.-Ing. Jonas Stienen committed
55
	m_pClient = new CITANetAudioClient();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
56

Anne's avatar
Anne committed
57
	m_oParams.iChannels = pParent->GetNumberOfChannels();
58
	m_oParams.dSampleRate = pParent->GetSampleRate( );
59 60 61
	m_oParams.iBlockSize = pParent->GetBlocklength();
	m_oParams.iRingBufferSize = pParent->GetRingBufferSize();
	m_oParams.iTargetSampleLatency = pParent->GetAllowedLatencySamples();
62

Anne Heimes's avatar
Anne Heimes committed
63
	std::string paras = std::string("NetAudioLogClient") + std::string("_BS") + std::to_string(pParent->GetBlocklength()) + std::string("_Ch") + std::to_string(pParent->GetNumberOfChannels()) + std::string("_tl") + std::to_string(pParent->GetAllowedLatencySamples()) + std::string(".txt");
64 65 66
	m_pClientLogger = new ITABufferedDataLoggerImplClient( );
	m_pClientLogger->setOutputFile(paras);
	iStreamingBlockId = 0;
67
	m_pMessage = new CITANetAudioMessage( VistaSerializingToolset::SWAPS_MULTIBYTE_VALUES );
68
	m_sfReceivingBuffer.init(m_oParams.iChannels, m_oParams.iRingBufferSize, false);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
69 70 71 72
}

CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{
73 74 75 76 77 78 79 80
	if( GetIsConnected() )
		Disconnect();

	StopGently( true );

	delete m_pClientLogger;
	delete m_pClient;
	delete m_pMessage;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
81 82 83 84
}

bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPort )
{
Dipl.-Ing. Jonas Stienen's avatar
Running  
Dipl.-Ing. Jonas Stienen committed
85
	if( GetIsConnected() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
86 87 88 89 90 91 92
		return false;
	
	if( !m_pClient->Connect( sAddress, iPort ) )
		ITA_EXCEPT1( INVALID_PARAMETER, "Could not connect to " + sAddress );
	
	m_pConnection = m_pClient->GetConnection();

93
	m_pMessage->ResetMessage();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
94 95 96 97
	m_pMessage->SetConnection( m_pConnection );

	// Validate streaming parameters of server and client
	m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN );
Anne's avatar
Anne committed
98
	m_pMessage->WriteStreamingParameters( m_oParams );
99 100
	m_pMessage->WriteMessage( );
	m_pMessage->ResetMessage( );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
101

102 103 104
	while ( !m_pMessage->ReadMessage( 0 ) );
	
	assert( m_pMessage->GetMessageType( ) == CITANetAudioProtocol::NP_SERVER_OPEN );
105 106
	CITANetAudioProtocol::StreamingParameters oServerParas = m_pMessage->ReadStreamingParameters();
	m_oParams.dTimeIntervalSendInfos = oServerParas.dTimeIntervalSendInfos;
107
	
108
	if (!(oServerParas == m_oParams))
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
109
		ITA_EXCEPT1( INVALID_PARAMETER, "Streaming server declined connection, detected streaming parameter mismatch." );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
110 111 112 113 114 115 116 117

	Run();

	return true;
}

bool CITANetAudioStreamingClient::LoopBody()
{
118
	if( !GetIsConnected() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
119 120
		return true;

121 122 123 124 125 126 127 128
	ITAClientLog oLog;
	oLog.uiBlockId = ++iStreamingBlockId;

	if( m_bStopIndicated && !m_bStopped )
	{
		m_pMessage->ResetMessage();
		m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_CLOSE );
		m_pMessage->WriteMessage();
129 130 131 132 133 134 135 136 137 138

		while( true )
		{
			m_pMessage->ResetMessage();
			m_pMessage->ReadMessage( 0 );
			int iMsgType = m_pMessage->GetMessageType();
			if( iMsgType == CITANetAudioProtocol::NP_SERVER_CLOSE )
				break;
		}

139 140 141 142 143 144 145 146 147
		m_bStopped = true;
		m_pMessage->SetConnection( NULL );

		while( GetIsConnected() )
			VistaTimeUtils::Sleep( 100 );

		return true;
	}
	
Anne's avatar
Anne committed
148

149 150
	// Read answer (blocking)
	m_pMessage->ResetMessage( );
151
	if( m_pMessage->ReadMessage( 1 ) )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
152
	{
Jonas Stienen's avatar
Jonas Stienen committed
153 154 155 156 157 158 159
		int iMsgType = m_pMessage->GetMessageType();
		switch( iMsgType )
		{
		case CITANetAudioProtocol::NP_SERVER_SENDING_SAMPLES:
			m_pMessage->ReadSampleFrame( &m_sfReceivingBuffer );
			if( m_pStream->GetRingBufferFreeSamples() >= m_sfReceivingBuffer.GetLength() )
				m_pStream->Transmit( m_sfReceivingBuffer, m_sfReceivingBuffer.GetLength() );
160
#ifdef NET_AUDIO_SHOW_TRAFFIC
Jonas Stienen's avatar
Jonas Stienen committed
161
			vstr::out() << "[ITANetAudioStreamingClient] Recived " << m_sfReceivingBuffer.GetLength() << " samples" << std::endl;
162
#endif
Jonas Stienen's avatar
Jonas Stienen committed
163 164 165 166 167 168 169 170 171 172 173 174 175
			break;
		case CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE_SAMPLES:
			m_pMessage->ReadBool();
			m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_SENDING_RINGBUFFER_FREE_SAMPLES );
			m_pMessage->WriteInt( m_pStream->GetRingBufferFreeSamples() );
			m_pMessage->WriteMessage();
			break;
		case CITANetAudioProtocol::NP_SERVER_CLOSE:
			Disconnect();
			break;
		default:
			vstr::out() << "[ITANetAudioStreamingServer] Unkown protocol type : " << iMsgType << std::endl;
			break;
176
		}
Jonas Stienen's avatar
Jonas Stienen committed
177

178 179 180 181 182
		oLog.iChannel = m_pStream->GetNumberOfChannels();
		oLog.iProtocolStatus = iMsgType;
		oLog.iFreeSamples = m_pStream->GetRingBufferFreeSamples();
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
		m_pClientLogger->log( oLog );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
183
	}
184 185
	else 
	{
186 187 188 189 190 191 192 193
		if ((m_dLastAckknowlengementTimeStamp + m_oParams.dTimeIntervalSendInfos) < ITAClock::getDefaultClock()->getTime() )
		{
			// sende mal freie samples
			m_pMessage->SetMessageType(CITANetAudioProtocol::NP_CLIENT_SENDING_RINGBUFFER_FREE_SAMPLES);
			m_pMessage->WriteInt(m_pStream->GetRingBufferFreeSamples());
			m_pMessage->WriteMessage();
			m_dLastAckknowlengementTimeStamp = ITAClock::getDefaultClock()->getTime();
		}
194
	}
195
	return false;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
196 197
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
198 199 200 201
bool CITANetAudioStreamingClient::GetIsConnected() const
{
	return m_pClient->GetIsConnected();
}
202 203 204 205 206

void CITANetAudioStreamingClient::Disconnect()
{
	m_bStopIndicated = true;

207 208 209
	while( !m_bStopped )
		VistaTimeUtils::Sleep( 100 );
	
210
	m_pConnection = NULL;
211
	m_pClient->Disconnect();
212
	m_bStopIndicated = false;
213
	m_bStopped = false;
214
}