ITANetAudioStreamingClient.cpp 2.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
6
7
8
9
#include <ITANetAudioStream.h>

#include <VistaInterProcComm/Connections/VistaConnectionIP.h>

CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pParent )
Dipl.-Ing. Jonas Stienen's avatar
Running    
Dipl.-Ing. Jonas Stienen committed
10
	: m_pStream( pParent )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
11
12
13
	, m_pConnection( NULL )
	, m_bStopIndicated( false )
{
Dipl.-Ing. Jonas Stienen's avatar
Running    
Dipl.-Ing. Jonas Stienen committed
14
	m_pClient = new CITANetAudioClient();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
15
16
17
18

	m_oClientParams.iChannels = pParent->GetNumberOfChannels();
	m_oClientParams.dSampleRate = pParent->GetSampleRate();
	m_oClientParams.iBlockSize = pParent->GetBlocklength();
19
20

	m_pMessage = new CITANetAudioMessage( VistaSerializingToolset::SWAPS_MULTIBYTE_VALUES );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
21
22
23
24
25
26
27
28
29
30
31
32
33
}

CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{
	if( m_pConnection )
	{
		m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_CLOSE );
		m_pMessage->WriteAnswer();
	}
}

bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPort )
{
Dipl.-Ing. Jonas Stienen's avatar
Running    
Dipl.-Ing. Jonas Stienen committed
34
	if( GetIsConnected() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
35
36
37
38
39
40
41
		return false;
	
	if( !m_pClient->Connect( sAddress, iPort ) )
		ITA_EXCEPT1( INVALID_PARAMETER, "Could not connect to " + sAddress );
	
	m_pConnection = m_pClient->GetConnection();

42
	m_pMessage->ResetMessage();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
43
44
45
46
	m_pMessage->SetConnection( m_pConnection );

	// Validate streaming parameters of server and client
	m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN );
47
48
	//m_pMessage->WriteStreamingParameters( m_oClientParams ); // Not yet
	m_pMessage->WriteInt( 42 );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
49
50
51
	m_pMessage->WriteMessage();

	m_pMessage->ReadAnswer();
52
	assert( m_pMessage->GetAnswerType() == CITANetAudioProtocol::NP_SERVER_OPEN );
53
	int i42 = m_pMessage->ReadInt();
54
	
55
	/* Not yet
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
56
57
58
59
60
	CITANetAudioProtocol::StreamingParameters oServerParams = m_pMessage->ReadStreamingParameters();
	if( oServerParams == m_oClientParams )
		m_oServerParams = oServerParams;
	else
		ITA_EXCEPT1( INVALID_PARAMETER, "Streaming parameters of network audio server and client do not match." );
61
	*/
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
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

	Run();

	return true;
}

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

	delete m_pConnection;
	m_pConnection = NULL;

	m_bStopIndicated = false;
}

bool CITANetAudioStreamingClient::LoopBody()
{
	if( m_bStopIndicated )
		return true;

	// Receive message
	m_pMessage->ReadMessage();
	switch( m_pMessage->GetMessageType() )
	{
	case CITANetAudioProtocol::NP_INVALID:
		break;
	case CITANetAudioProtocol::NP_SERVER_SEND_SAMPLES:
		/*
		int iNumSamples = m_pMessage->ReadSamples( m_sfReceivingBuffer );
		m_pParent->Transmit( m_sfReceivingBuffer, iNumSamples );
		int iFreeSamples = m_pParent->GetRingBufferFreeSamples();
		m_pMessage->WriteFreeRingBufferSamples( iFreeSamples );
		m_pMessage->WriteAnswer();
		*/
		break;
	}
}

bool CITANetAudioStreamingClient::GetIsConnected() const
{
	return m_pClient->GetIsConnected();
}