More refactoring in net audio

parent 07358399
......@@ -48,10 +48,11 @@ set( ITADataSourcesHeader
"include/ITAFileDataSink.h"
"include/ITAFileDataSource.h"
"include/ITANetAudioStream.h"
"include/ITANetAudioConnection.h"
"include/ITANetAudioMessage.h"
"include/ITANetAudioProtocol.h"
"include/ITANetAudioSampleServer.h"
"include/ITANetAudioStreamingClient.h"
"include/ITANetAudioStreamingServer.h"
"include/ITAPeakDetector.h"
"include/ITARMSDetector.h"
"include/ITAStreamAmplifier.h"
......@@ -71,10 +72,12 @@ set( ITADataSourcesSources
"src/ITADataSourceRealization.cpp"
"src/ITAFileDataSink.cpp"
"src/ITAFileDataSource.cpp"
"src/ITANetAudioStream.cpp"
"src/ITANetAudioMessage.cpp"
"src/ITANetAudioProtocol.cpp"
"src/ITANetAudioSampleServer.cpp"
"src/ITANetAudioStreamingClient.cpp"
"src/ITANetAudioStreamingServer.cpp"
"src/ITANetAudioStream.cpp"
"src/ITAPeakDetector.cpp"
"src/ITARMSDetector.cpp"
"src/ITAStreamAmplifier.cpp"
......
......@@ -56,9 +56,10 @@ public:
static const int NP_CLIENT_CLOSE = 101;
static const int NP_SERVER_CLOSE = 200;
static const int NP_SERVER_GET_RINGBUFFER_SIZE = 201;
static const int NP_SERVER_GET_RINGBUFFER_FREE = 202;
static const int NP_SERVER_SEND_SAMPLES = 203;
static const int NP_SERVER_OPEN = 201;
static const int NP_SERVER_GET_RINGBUFFER_SIZE = 210;
static const int NP_SERVER_GET_RINGBUFFER_FREE = 211;
static const int NP_SERVER_SEND_SAMPLES = 222;
CITANetAudioProtocol();
virtual ~CITANetAudioProtocol();
......
......@@ -27,7 +27,7 @@
#include <string>
#include <vector>
class CITANetAudioStreamConnection;
class CITANetAudioStreamingClient;
//! Network audio stream
/**
......@@ -56,7 +56,7 @@ protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
private:
CITANetAudioStreamConnection* m_pNetAudioProducer;
CITANetAudioStreamingClient* m_pNetAudioProducer;
double m_dSampleRate;
ITASampleFrame m_sfOutputStreamBuffer;
......@@ -65,7 +65,7 @@ private:
int m_iWriteCursor; //!< Cursor where samples will feeded into ring buffer from net audio producer
ITASampleFrame m_sfRingBuffer;
friend class CITANetAudioStreamConnection;
friend class CITANetAudioStreamingClient;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_STREAM
......@@ -24,48 +24,37 @@
#include <ITADataSource.h>
#include <ITASampleFrame.h>
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <string>
#include <vector>
class CITANetAudioStreamConnection;
class CITANetAudioStream;
class VistaConnectionIP;
class CITANetAudioMessage;
class CITANetAudioProtocol;
//! Network audio stream
/**
* Audio streaming for a signal source that is connected via TCP/IP.
*
* \note not thread-safe
*/
class ITA_DATA_SOURCES_API CITANetAudioStream : public ITADatasource
class CITANetAudioStreamingClient : public VistaThreadLoop
{
public:
CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity );
virtual ~CITANetAudioStream();
bool Connect( const std::string& sAddress, int iPort );
bool GetIsConnected() const;
CITANetAudioStreamingClient( CITANetAudioStream* pParent );
~CITANetAudioStreamingClient();
int GetRingBufferSize() const;
unsigned int GetBlocklength() const;
unsigned int GetNumberOfChannels() const;
double GetSampleRate() const;
const float* GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* );
void IncrementBlockPointer();
protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
bool Connect( const std::string& sAddress, int iPort );
void Disconnect();
bool LoopBody();
bool GetIsConnected();
private:
CITANetAudioStreamConnection* m_pNetAudioProducer;
double m_dSampleRate;
ITASampleFrame m_sfOutputStreamBuffer;
CITANetAudioStream* m_pParent;
int m_iReadCursor; //!< Cursor where samples will be consumed from ring buffer on next block
int m_iWriteCursor; //!< Cursor where samples will feeded into ring buffer from net audio producer
ITASampleFrame m_sfRingBuffer;
VistaConnectionIP* m_pConnection;
CITANetAudioProtocol* m_pProtocol;
CITANetAudioMessage* m_pMessage;
friend class CITANetAudioStreamConnection;
ITASampleFrame m_sfReceivingBuffer;
bool m_bStopIndicated;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_CONNECTION
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2016
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#define INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#include <ITADataSourcesDefinitions.h>
#include <ITASampleFrame.h>
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <string>
#include <vector>
class VistaTCPSocket;
class CITANetAudioSampleServer;
class VistaTCPServer;
//! Realizes server functionality for network audio streaming
/**
* Can be connected to an ITADataSource as a streaming source
* or to a user-implemented sample producer, i.e. an audio sythesizer.
*/
class CITANetAudioStreamServer : public VistaThreadLoop
{
public:
CITANetAudioStreamServer( CITANetAudioSampleServer* pParent );
virtual ~CITANetAudioStreamServer();
std::string GetServerAddress() const;
int GetNetworkPort() const;
bool Start( const std::string& sAddress, int iPort );
void Disconnect(); bool IsConnected() const; bool LoopBody();
private:
VistaTCPServer* m_pServer;
VistaTCPSocket* m_pSocket;
int m_iServerPort;
std::string m_sServerAddress;
CITANetAudioSampleServer* m_pParent;
ITASampleFrame m_sfReceivingBuffer;
bool m_bStopIndicated;
int m_iClientChannels;
int m_iClientRingBufferSize;
int m_iClientBufferSize;
int m_iClientRingBufferFreeSamples;
double m_dClientSampleRate;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#include <ITANetAudioSampleServer.h>
#include <ITANetAudioProtocol.h>
#include <ITANetAudioStreamingServer.h>
// ITA includes
#include <ITADataSource.h>
......@@ -18,122 +18,6 @@
#include <cmath>
#include <cassert>
class CITANetAudioStreamServer : public VistaThreadLoop
{
public:
inline 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 )
{
};
inline ~CITANetAudioStreamServer()
{
};
inline std::string GetServerAddress() const
{
return m_sServerAddress;
};
inline int GetNetworkPort() const
{
return m_iServerPort;
};
inline bool 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();
};
inline void Disconnect()
{
m_bStopIndicated = true;
StopGently( true );
m_pSocket = NULL;
delete m_pServer;
m_pServer = NULL;
m_bStopIndicated = false;
};
inline bool IsConnected() const
{
if( !m_pSocket )
return false;
return m_pSocket->GetIsConnected();
};
inline bool 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;
};
private:
VistaTCPServer* m_pServer;
VistaTCPSocket* m_pSocket;
int m_iServerPort;
std::string m_sServerAddress;
CITANetAudioSampleServer* m_pParent;
ITASampleFrame m_sfReceivingBuffer;
bool m_bStopIndicated;
int m_iClientChannels;
int m_iClientRingBufferSize;
int m_iClientBufferSize;
int m_iClientRingBufferFreeSamples;
double m_dClientSampleRate;
};
CITANetAudioSampleServer::CITANetAudioSampleServer()
: m_pInputStream( NULL )
, m_iUpdateStrategy( AUTO )
......
......@@ -4,7 +4,7 @@
#include <ITAException.h>
#include <ITANetAudioMessage.h>
#include <ITANetAudioProtocol.h>
#include <ITANetAudioConnection.h>
#include <ITANetAudioStreamingCLient.h>
// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
......@@ -23,7 +23,7 @@ CITANetAudioStream::CITANetAudioStream( int iChannels, double dSamplingRate, int
, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
{
m_pNetAudioProducer = new CITANetAudioStreamConnection( this );
m_pNetAudioProducer = new CITANetAudioStreamingClient( this );
}
CITANetAudioStream::~CITANetAudioStream()
......
#include <ITANetAudioConnection.h>
#include <ITANetAudioStreamingCLient.h>
#include <ITANetAudioStream.h>
#include <ITANetAudioProtocol.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
CITANetAudioStreamConnection::CITANetAudioStreamConnection( CITANetAudioStream* pParent )
CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pParent )
: m_pParent( pParent )
, m_pConnection( NULL )
, m_bStopIndicated( false )
{
}
CITANetAudioStreamConnection::~CITANetAudioStreamConnection()
CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{
if( m_pConnection )
{
......@@ -20,7 +20,7 @@ CITANetAudioStreamConnection::~CITANetAudioStreamConnection()
}
}
bool CITANetAudioStreamConnection::Connect( const std::string& sAddress, int iPort )
bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPort )
{
if( m_pConnection )
ITA_EXCEPT1( MODAL_EXCEPTION, "This net stream is already connected" );
......@@ -53,7 +53,7 @@ bool CITANetAudioStreamConnection::Connect( const std::string& sAddress, int iPo
Run();
}
void CITANetAudioStreamConnection::Disconnect()
void CITANetAudioStreamingClient::Disconnect()
{
m_bStopIndicated = true;
StopGently( true );
......@@ -64,7 +64,7 @@ void CITANetAudioStreamConnection::Disconnect()
m_bStopIndicated = false;
}
bool CITANetAudioStreamConnection::LoopBody()
bool CITANetAudioStreamingClient::LoopBody()
{
if( m_bStopIndicated )
return true;
......@@ -81,7 +81,7 @@ bool CITANetAudioStreamConnection::LoopBody()
}
bool CITANetAudioStreamConnection::GetIsConnected()
bool CITANetAudioStreamingClient::GetIsConnected()
{
if( m_pConnection )
return true;
......
#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;
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment