ITANetAudioStreamingClient.cpp 4.75 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>
Anne Heimes's avatar
Anne Heimes committed
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>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
11

Anne Heimes's avatar
Anne Heimes committed
12 13 14 15 16 17 18 19 20
//! 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";
Anne Heimes's avatar
Anne Heimes committed
21
		os << "\t" << "Channel";
Anne Heimes's avatar
Anne Heimes committed
22 23 24 25 26 27 28 29 30 31
		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;
Anne Heimes's avatar
Anne Heimes committed
32
		os << "\t" << iChannel;
Anne Heimes's avatar
Anne Heimes committed
33 34 35 36 37 38 39 40
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId; //!< Block identifier (audio streaming)
	double dWorldTimeStamp;
	int iProtocolStatus; //!< ... usw
	int iFreeSamples;
Anne Heimes's avatar
Anne Heimes committed
41
	int iChannel;
Anne Heimes's avatar
Anne Heimes committed
42 43 44 45 46 47

};

class ITABufferedDataLoggerImplClient : public ITABufferedDataLogger < ITAClientLog > {};


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

Anne's avatar
Anne committed
56 57 58
	m_oParams.iChannels = pParent->GetNumberOfChannels();
	m_oParams.dSampleRate = pParent->GetSampleRate();
	m_oParams.iBlockSize = pParent->GetBlocklength();
Anne Heimes's avatar
Anne Heimes committed
59 60

	std::string paras = std::string("NetAudioLogClient") + std::string("_BS") + std::to_string(pParent->GetBlocklength()) + std::string("_Ch") + std::to_string(pParent->GetNumberOfChannels()) + std::string(".txt");
Anne Heimes's avatar
Anne Heimes committed
61
	m_pClientLogger = new ITABufferedDataLoggerImplClient( );
Anne Heimes's avatar
Anne Heimes committed
62
	m_pClientLogger->setOutputFile(paras);
Anne Heimes's avatar
Anne Heimes committed
63
	iStreamingBlockId = 0;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
64 65 66 67
}

CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{
Anne Heimes's avatar
Anne Heimes committed
68
	delete m_pClientLogger;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
69 70 71 72
}

bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPort )
{
Dipl.-Ing. Jonas Stienen's avatar
Running  
Dipl.-Ing. Jonas Stienen committed
73
	if( GetIsConnected() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
74 75 76 77 78 79 80
		return false;
	
	if( !m_pClient->Connect( sAddress, iPort ) )
		ITA_EXCEPT1( INVALID_PARAMETER, "Could not connect to " + sAddress );
	
	m_pConnection = m_pClient->GetConnection();

81 82
	m_pIncomingMessage = new CITANetAudioMessage( m_pConnection );
	m_pOutgoingMessage = new CITANetAudioMessage( m_pConnection );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
83 84

	// Validate streaming parameters of server and client
85 86 87
	m_pOutgoingMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN );
	m_pOutgoingMessage->WriteStreamingParameters( m_oParams );
	m_pOutgoingMessage->WriteMessage();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
88 89 90 91 92 93 94 95

	Run();

	return true;
}

bool CITANetAudioStreamingClient::LoopBody()
{
Anne Heimes's avatar
Anne Heimes committed
96 97 98
	ITAClientLog oLog;
	oLog.uiBlockId = ++iStreamingBlockId;

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
99 100 101
	if( m_bStopIndicated )
		return true;

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	// Send message to server that (and how many) samples can be received
	m_pIncomingMessage->ResetMessage();
	if( !m_pIncomingMessage->TryReadMessage() )
	{
		int iFreeSamplesUntilAllowedReached = m_pStream->GetAllowedLatencySamples() - m_pStream->GetRingBufferAvailableSamples();
		oLog.iFreeSamples = iFreeSamplesUntilAllowedReached;
		if( iFreeSamplesUntilAllowedReached < 0 )
			iFreeSamplesUntilAllowedReached = 0;

		m_pOutgoingMessage->ResetMessage();
		m_pIncomingMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES );
		m_pOutgoingMessage->WriteInt( iFreeSamplesUntilAllowedReached );
		m_pOutgoingMessage->WriteMessage();

		return false;
	}

	int iIncomingMessageType = m_pIncomingMessage->GetMessageType();
	switch( iIncomingMessageType )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
121
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
122

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
123
	case CITANetAudioProtocol::NP_INVALID:
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
124
		// Something went wrong
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
125
		vstr::err() << "Received invalid message type" << std::endl;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
126 127
		break;

128 129 130 131
	case CITANetAudioProtocol::NP_SERVER_CLOSE:
		Disconnect();
		break;

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
132
	case CITANetAudioProtocol::NP_SERVER_SEND_SAMPLES:
133
		// Receive samples from net message and forward them to the stream ring buffer
134

135
		m_pIncomingMessage->ReadSampleFrame( &m_sfReceivingBuffer );
136
		if ( m_pStream->GetRingBufferFreeSamples( ) >= m_sfReceivingBuffer.GetLength( ) )
Anne's avatar
Anne committed
137
			m_pStream->Transmit( m_sfReceivingBuffer, m_sfReceivingBuffer.GetLength( ) );
Anne's avatar
Anne committed
138 139
		//else 
			// Fehler
140
		
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
141
		break;
Anne's avatar
Anne committed
142 143
	case CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE :
		break;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
144
	}
Anne Heimes's avatar
Anne Heimes committed
145
	oLog.iChannel = m_pStream->GetNumberOfChannels();
146
	oLog.iProtocolStatus = iIncomingMessageType;
Anne Heimes's avatar
Anne Heimes committed
147 148
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
	m_pClientLogger->log( oLog );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
149 150 151 152 153 154
	return true;
}

void CITANetAudioStreamingClient::TriggerBlockIncrement()
{
	m_oBlockIncrementEvent.SignalEvent();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
155 156 157 158 159 160
}

bool CITANetAudioStreamingClient::GetIsConnected() const
{
	return m_pClient->GetIsConnected();
}
161 162 163 164 165

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

166 167 168
	delete m_pIncomingMessage;
	delete m_pOutgoingMessage;
	
169 170 171 172
	m_pConnection = NULL;

	m_bStopIndicated = false;
}