ITANetAudioStreamingServer.cpp 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <ITANetAudioStreamingServer.h>
#include <ITANetAudioSampleServer.h>
#include <ITANetAudioProtocol.h>

// ITA includes
#include <ITADataSource.h>
#include <ITAException.h>
#include <ITAStreamInfo.h>

// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/IPNet/VistaIPAddress.h>

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

22
CITANetAudioStreamingServer::CITANetAudioStreamingServer( CITANetAudioSampleServer* pParent )
23 24 25 26 27 28 29 30 31 32 33 34
	: m_pParent( pParent )
	, m_bStopIndicated( false )
	, m_pServer( NULL )
	, m_pSocket( NULL )
	, m_iClientRingBufferSize( -1 )
	, m_iClientBufferSize( -1 )
	, m_iClientRingBufferFreeSamples( 0 )
	, m_dClientSampleRate( -1 )
	, m_iServerPort( -1 )
{
};

35
CITANetAudioStreamingServer::~CITANetAudioStreamingServer()
36 37 38
{
}

39
std::string CITANetAudioStreamingServer::GetServerAddress() const
40 41 42 43
{
	return m_sServerAddress;
}

44
int CITANetAudioStreamingServer::GetNetworkPort() const
45 46 47 48
{
	return m_iServerPort;
}

49
bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort )
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
	if( m_pServer )
		ITA_EXCEPT1( MODAL_EXCEPTION, "This net sample server is already started" );

	m_pServer = new VistaTCPServer( sAddress, iPort, 1 );
	m_sServerAddress = sAddress;
	m_iServerPort = iPort;

	m_pSocket = m_pServer->GetNextClient();

	long nIncomingBytes = m_pSocket->WaitForIncomingData( 0 );
	int iBytesReceived = m_pSocket->ReceiveRaw( &m_iClientChannels, sizeof( int ) );
	iBytesReceived = m_pSocket->ReceiveRaw( &m_dClientSampleRate, sizeof( double ) );
	iBytesReceived = m_pSocket->ReceiveRaw( &m_iClientBufferSize, sizeof( int ) );
	iBytesReceived = m_pSocket->ReceiveRaw( &m_iClientRingBufferSize, sizeof( int ) );
	m_iClientRingBufferFreeSamples = m_iClientRingBufferFreeSamples;

	int iMessageID = 1;
	m_pSocket->SendRaw( &iMessageID, sizeof( int ) );

	Run();
}

73
void CITANetAudioStreamingServer::Disconnect()
74 75 76 77 78 79 80 81 82 83 84 85
{
	m_bStopIndicated = true;
	StopGently( true );

	m_pSocket = NULL;

	delete m_pServer;
	m_pServer = NULL;

	m_bStopIndicated = false;
}

86
bool CITANetAudioStreamingServer::IsConnected() const
87 88 89 90 91 92 93
{
	if( !m_pSocket )
		return false;

	return m_pSocket->GetIsConnected();
}

94
bool CITANetAudioStreamingServer::LoopBody()
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
	if( m_bStopIndicated )
		return true;

	if( m_pSocket->GetIsConnected() == false )
	{
		StopGently( true );
		return false;
	}

	ITAStreamInfo oStreamInfo;

	ITADatasource* pIn = m_pParent->GetInputStream();
	for( int iChannelIndex = 0; iChannelIndex < int( m_pParent->GetInputStream()->GetNumberOfChannels() ); iChannelIndex++ )
	{
		const float* pfData = pIn->GetBlockPointer( iChannelIndex, &oStreamInfo );
		int iNumSamples = pIn->GetBlocklength();
		m_pSocket->SendRaw( pfData, iNumSamples * sizeof( float ) );
	}

	return true;
}