ITANetAudioStreamingServer.cpp 3.76 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
6 7
#include <ITADataSource.h>
#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
{
Anne's avatar
Anne committed
32
	// TODO: vorr�ckgabe noch anfangen zu senden (Samples)
33
	if( !m_pNetAudioServer->Start( sAddress, iPort ) ) // blocking
34 35
		return false;

36
	m_pConnection = m_pNetAudioServer->GetConnection();
37

38 39
	m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag() );
	m_pMessage->ResetMessage();
40
	m_pMessage->SetConnection( m_pConnection );
41
	m_pMessage->ReadMessage(); // blocking
42

43 44 45
	int nMT = m_pMessage->GetMessageType();
	assert( nMT == CITANetAudioProtocol::NP_CLIENT_OPEN );
	int i42 = m_pMessage->ReadInt();
46

47
	//CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters();
48

49 50 51
	m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_OPEN );
	m_pMessage->WriteInt( 2 * 42 );
	m_pMessage->WriteAnswer();
52

53 54
	if( m_pInputStream )
		Run();
55

56
	return true;
Anne's avatar
Anne committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}

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();
}
Anne's avatar
Anne committed
73 74 75 76
bool CITANetAudioStreamingServer::LoopBody()
{
	return true;
}
Anne's avatar
Anne committed
77 78 79

void CITANetAudioStreamingServer::Stop() 
{
80
	m_pNetAudioServer->Stop();
Anne's avatar
Anne committed
81 82 83 84
}

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

Anne's avatar
Anne committed
88
	m_pInputStream = pInStream;
89 90 91 92
	m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels(), m_pInputStream->GetBlocklength(), true );

	if( m_pConnection )
		Run();
Anne's avatar
Anne committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

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
110 111
bool CITANetAudioStreamingServer::LoopBody()
{
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	m_pMessage->ResetMessage();
	m_pMessage->SetConnection( m_pConnection );
	m_pMessage->ReadMessage(); // blocking

	switch( m_pMessage->GetMessageType() )
	{
	case CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES:
		if( m_pInputStream )
		{
			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() );
			}
		}

		//m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
		m_pMessage->WriteAnswer();
		break;
	}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
134 135 136
	return true;
}

Anne's avatar
Anne committed
137 138 139 140 141 142 143 144 145
ITADatasource* CITANetAudioStreamingServer::GetInputStream() const
{
	return m_pInputStream;
}

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