Commit 861fde06 authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Adding net audio protocol files (in progress)

parent afdc0dad
......@@ -48,6 +48,7 @@ set( ITADataSourcesHeader
"include/ITAFileDataSink.h"
"include/ITAFileDataSource.h"
"include/ITANetAudioStream.h"
"include/ITANetAudioProtocol.h"
"include/ITANetAudioSampleServer.h"
"include/ITAPeakDetector.h"
"include/ITARMSDetector.h"
......
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2016
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_PROTOCOL
#define INCLUDE_WATCHER_ITA_NET_AUDIO_PROTOCOL
#include <ITADataSourcesDefinitions.h>
// ITA includes
#include <ITAException.h>
#include <ITASampleBuffer.h>
#include <ITASampleFrame.h>
// Vista includes
#include <VistaBase/VistaBaseTypes.h>
#include <VistaInterProcComm/Connections/VistaByteBufferSerializer.h>
#include <VistaInterProcComm/Connections/VistaByteBufferDeSerializer.h>
// STL includes
#include <string>
#include <vector>
// Forward declarations
class VistaConnectionIP;
//! Network audio protocol
/**
* Declaration of message types
*/
struct ITA_DATA_SOURCES_API CITANetAudioProtocol
{
static const int NET_AUDIO_VERSION = 1;
static const int NP_INVALID = -1;
static const int NP_GET_VERSION_INFO = 1;
static const int NP_CLIENT_OPEN = 100;
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;
};
/** Network audio messages
*
* Messages consist of a message part and an answer part, each read or written
* separately. Messages have a two-int-header (SIZE, MSGTYPE), and
* answers have a two-int header (SIZE; ANSWERTYPE)
*/
class ITA_DATA_SOURCES_API CITANetAudioMessage
{
public:
CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers );
void ResetMessage();
void SetConnection( VistaConnectionIP* );
void WriteMessage();
void ReadMessage();
void WriteAnswer();
void ReadAnswer();
int GetIncomingMessageSize() const;
int GetOutgoingMessageSize() const;
bool GetOutgoingMessageHasData() const;
// --= Serializing functions =--
// Basics
void WriteInt( const int );
void WriteBool( const bool );
void WriteDouble( const double );
void WriteException( const ITAException& );
void WriteFloat( const float );
void WriteString( const std::string& );
void WriteIntVector( const std::vector< int > );
void WriteFloatVector( const std::vector< float > );
// --= Reader =--
std::string ReadString();
int ReadInt();
bool ReadBool();
ITAException ReadException();
float ReadFloat();
double ReadDouble();
std::vector< int > ReadIntVector();
std::vector< float > ReadFloatVector();
VistaConnectionIP* GetConnection() const;
void ClearConnection();
private:
int m_nMessageType;
int m_nMessageId;
int m_nAnswerType;
VistaByteBufferSerializer m_oOutgoing;
VistaByteBufferSerializer m_oIncoming;
std::vector< VistaType::byte > m_vecIncomingBuffer;
VistaConnectionIP* m_pConnection;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_PROTOCOL
#include <ITANetAudioStream.h>
// ITA includes
#include <ITAException.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>
class CITANetAudioStreamConnection : public VistaThreadLoop
{
public:
enum MessageType
{
NET_MESSAGE_NONE = 0,
NET_MESSAGE_OPEN,
NET_MESSAGE_CLOSE,
NET_MESSAGE_SAMPLES,
};
inline CITANetAudioStreamConnection( CITANetAudioStream* pParent )
: m_pParent( pParent )
, m_pConnection( NULL )
, m_bStopIndicated( false )
{
};
inline bool Connect( const std::string& sAddress, int iPort )
{
if( m_pConnection )
ITA_EXCEPT1( MODAL_EXCEPTION, "This net stream is already connected" );
// Attempt to connect and check parameters
m_pConnection = new VistaConnectionIP( VistaConnectionIP::CT_TCP, sAddress, iPort );
if( !m_pConnection->GetIsConnected() )
{
delete m_pConnection;
m_pConnection = NULL;
return false;
}
int iMessageType = NET_MESSAGE_OPEN;
m_pConnection->Send( &iMessageType, sizeof( int ) );
int iNumChannels = ( int ) m_pParent->GetNumberOfChannels();
m_pConnection->Send( &iNumChannels, sizeof( int ) );
double dSampleRate = m_pParent->GetSampleRate();
m_pConnection->Send( &dSampleRate, sizeof( double ) );
int iBlockLength = ( int ) m_pParent->GetBlocklength();
m_pConnection->Send( &iBlockLength, sizeof( int ) );
int iRingBufferSize = ( int ) m_pParent->GetRingBufferSize();
m_pConnection->Send( &iRingBufferSize, sizeof( int ) );
m_pConnection->WaitForSendFinish();
int iServerMessageType;
m_pConnection->Receive( &iServerMessageType, sizeof( int ) );
Run();
};
inline void Disconnect()
{
m_bStopIndicated = true;
StopGently( true );
delete m_pConnection;
m_pConnection = NULL;
m_bStopIndicated = false;
};
inline ~CITANetAudioStreamConnection()
{
if( m_pConnection )
{
int iMessageType = NET_MESSAGE_CLOSE;
m_pConnection->Send( &iMessageType, sizeof( int ) );
}
};
inline bool LoopBody()
{
if( m_bStopIndicated )
return true;
// Receive messages
while( true )
{
m_pConnection->Receive( NULL, 0 ); // @todo: receive messages and react
int iNumSamples = 12;
if( true )
m_pParent->Transmit( m_sfReceivingBuffer, iNumSamples );
}
};
private:
VistaConnectionIP* m_pConnection;
CITANetAudioStream* m_pParent;
ITASampleFrame m_sfReceivingBuffer;
bool m_bStopIndicated;
};
CITANetAudioStream::CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity )
: m_sfOutputStreamBuffer( iChannels, iBufferSize, true )
, m_dSampleRate( dSamplingRate )
, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
{
m_pNetAudioProducer = new CITANetAudioStreamConnection( this );
}
bool CITANetAudioStream::Connect( const std::string& sAddress, int iPort )
{
return m_pNetAudioProducer->Connect( sAddress, iPort );
}
CITANetAudioStream::~CITANetAudioStream()
{
delete m_pNetAudioProducer;
}
const float* CITANetAudioStream::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* )
{
// @todo: implement cyclic read from ring buffer
return m_sfOutputStreamBuffer[ uiChannel ].GetData();
}
void CITANetAudioStream::IncrementBlockPointer()
{
// Increment read cursor by one audio block and wrap around if exceeding ring buffer
m_iReadCursor = ( m_iReadCursor + m_sfOutputStreamBuffer.GetLength() ) % m_sfRingBuffer.GetLength();
}
int CITANetAudioStream::Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples )
{
ITA_EXCEPT0( NOT_IMPLEMENTED );
}
int CITANetAudioStream::GetRingBufferSize() const
{
return m_sfRingBuffer.GetLength();
}
unsigned int CITANetAudioStream::GetBlocklength() const
{
return ( unsigned int ) m_sfOutputStreamBuffer.GetLength();
}
unsigned int CITANetAudioStream::GetNumberOfChannels() const
{
return ( unsigned int ) m_sfOutputStreamBuffer.channels();
}
double CITANetAudioStream::GetSampleRate() const
{
return m_dSampleRate;
}
#include <ITANetAudioSampleServer.h>
#include <ITANetAudioProtocol.h>
// ITA includes
#include <ITADataSource.h>
......@@ -20,14 +21,6 @@
class CITANetAudioStreamServer : public VistaThreadLoop
{
public:
enum MessageType
{
NET_MESSAGE_NONE = 0,
NET_MESSAGE_OPEN,
NET_MESSAGE_CLOSE,
NET_MESSAGE_SAMPLES,
};
inline CITANetAudioStreamServer( CITANetAudioSampleServer* pParent )
: m_pParent( pParent )
, m_bStopIndicated( false )
......
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