ITANetAudioStreamingServer.cpp 2.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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>

CITANetAudioStreamServer::CITANetAudioStreamServer( CITANetAudioSampleServer* pParent )
	: 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 )
{
};

CITANetAudioStreamServer::~CITANetAudioStreamServer()
{
}

std::string CITANetAudioStreamServer::GetServerAddress() const
{
	return m_sServerAddress;
}

int CITANetAudioStreamServer::GetNetworkPort() const
{
	return m_iServerPort;
}

bool CITANetAudioStreamServer::Start( const std::string& sAddress, int iPort )
{
	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();
}

void CITANetAudioStreamServer::Disconnect()
{
	m_bStopIndicated = true;
	StopGently( true );

	m_pSocket = NULL;

	delete m_pServer;
	m_pServer = NULL;

	m_bStopIndicated = false;
}

bool CITANetAudioStreamServer::IsConnected() const
{
	if( !m_pSocket )
		return false;

	return m_pSocket->GetIsConnected();
}

bool CITANetAudioStreamServer::LoopBody()
{
	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;
}