ITANetAudioStreamingServer.cpp 5.02 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <ITAException.h>
#include <ITAStreamInfo.h>

// 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>

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

CITANetAudioStreamingServer::CITANetAudioStreamingServer()
	: m_pInputStream( NULL )
	, m_iUpdateStrategy( AUTO )
25
	, m_pConnection( NULL )
Anne's avatar
Anne committed
26 27 28 29
{
	m_pNetAudioServer = new CITANetAudioServer();
}

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

Anne's avatar
Anne committed
35
	// TODO: vorr�ckgabe noch anfangen zu senden (Samples)
36
	if( !m_pNetAudioServer->Start( sAddress, iPort ) ) // blocking
37 38
		return false;

39
	m_pConnection = m_pNetAudioServer->GetConnection();
40

41 42
	m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag() );
	m_pMessage->ResetMessage();
43
	m_pMessage->SetConnection( m_pConnection );
44
	m_pMessage->ReadMessage(); // blocking
45

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
46 47
	assert( m_pMessage->GetMessageType() == CITANetAudioProtocol::NP_CLIENT_OPEN );
	CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters();
48

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
49
	bool bOK = false;
Anne's avatar
Anne committed
50
	if (m_pInputStream->GetNumberOfChannels() == oClientParams.iChannels &&
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
51
		m_pInputStream->GetSampleRate() == oClientParams.dSampleRate &&
Anne's avatar
Anne committed
52 53
		m_pInputStream->GetBlocklength() == oClientParams.iBlockSize)
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
54
		bOK = true;
Anne's avatar
Anne committed
55 56 57 58
		std::cout << " Alle Daten Stimmen: \nAnzahl Channel: " << oClientParams.iChannels << std::endl;
		std::cout << "SampleRate: " << oClientParams.dSampleRate << std::endl;
		std::cout << "Blockgroesse: " << oClientParams.iBlockSize << std::endl;
	}
59

60
	m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_OPEN );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
61
	m_pMessage->WriteBool( bOK );
62
	m_pMessage->WriteAnswer();
63

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
64
	Run();
65

66
	return true;
Anne's avatar
Anne committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

bool CITANetAudioStreamingServer::IsClientConnected() const
{
	return m_pNetAudioServer->IsConnected();
}

std::string CITANetAudioStreamingServer::GetNetworkAddress() const
{
	return m_pNetAudioServer->GetServerAddress();
}

int CITANetAudioStreamingServer::GetNetworkPort() const
{
	return m_pNetAudioServer->GetNetworkPort();
}

void CITANetAudioStreamingServer::Stop() 
{
86
	m_pNetAudioServer->Stop();
Anne's avatar
Anne committed
87 88 89 90
}

void CITANetAudioStreamingServer::SetInputStream( ITADatasource* pInStream )
{
91 92 93
	if( VistaThreadLoop::IsRunning() )
		ITA_EXCEPT1( MODAL_EXCEPTION, "Streaming loop already running, can not change input stream" );

Anne's avatar
Anne committed
94
	m_pInputStream = pInStream;
95 96
	m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels(), m_pInputStream->GetBlocklength(), true );

Anne's avatar
Anne committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

int CITANetAudioStreamingServer::GetNetStreamBlocklength() const
{
	return m_sfTempTransmitBuffer.GetLength();
}

int CITANetAudioStreamingServer::GetNetStreamNumberOfChannels() const
{
	return m_sfTempTransmitBuffer.channels();
}

void CITANetAudioStreamingServer::SetAutomaticUpdateRate()
{
	m_iUpdateStrategy = AUTO;
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
114 115
bool CITANetAudioStreamingServer::LoopBody()
{
116 117 118 119 120 121 122
	m_pMessage->ResetMessage();
	m_pMessage->SetConnection( m_pConnection );
	m_pMessage->ReadMessage(); // blocking

	switch( m_pMessage->GetMessageType() )
	{
	case CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES:
123 124
	{
		int iFreeSamples = m_pMessage->ReadInt();
Anne's avatar
Anne committed
125 126
		// std::cout << "Freie Samples: " << iFreeSamples << std::endl;
		// std::cout << "Laenge InputStram: " << m_pInputStream->GetBlocklength() << std::endl;
127
		if( iFreeSamples >= m_pInputStream->GetBlocklength() )
128
		{
Anne's avatar
Anne committed
129
			// Send Samples
130 131 132 133 134 135
			for( int i = 0; i < m_pInputStream->GetNumberOfChannels(); i++ )
			{
				ITAStreamInfo oStreamInfo;
				const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
				m_sfTempTransmitBuffer[ i ].write( pfData, m_pInputStream->GetBlocklength() );
			}
136
			m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_SEND_SAMPLES );
Anne's avatar
Anne committed
137
			m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
138 139
		}
		else
140
		{
Anne's avatar
Anne committed
141
			// Waiting for Trigger
142
			m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_WAITING_FOR_TRIGGER );
Anne's avatar
Anne committed
143 144
			m_pMessage->WriteAnswer();
			break;
145 146 147
		}

		m_pMessage->WriteAnswer();
Anne's avatar
Anne committed
148 149 150

		m_pInputStream->IncrementBlockPointer();
		float fTimeOut = m_pInputStream->GetBlocklength() / m_pInputStream->GetSampleRate();
Anne's avatar
Anne committed
151
		//VistaTimeUtils::Sleep( (int) ( 1 * 100 ) );
152 153
		break;
	}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
154 155 156 157 158 159 160
	case CITANetAudioProtocol::NP_CLIENT_CLOSE:
		m_pMessage->WriteAnswer();
		m_pConnection = NULL;
		StopGently( true );
		Stop();

		return false;
161 162
	}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
163 164 165
	return true;
}

Anne's avatar
Anne committed
166 167 168 169 170 171 172 173 174
ITADatasource* CITANetAudioStreamingServer::GetInputStream() const
{
	return m_pInputStream;
}

int CITANetAudioStreamingServer::Transmit(const ITASampleFrame& sfNewSamples, int iNumSamples)
{
	return 0;
}