Commit f4c7c40d authored by Anne Heimes's avatar Anne Heimes

bugfixes protocol 2 running server

parent a839b645
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2017
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#define INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#include <ITADataSourcesDefinitions.h>
#include <ITANetAudioProtocol.h>
#include <string>
#include <vector>
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2017
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#define INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#include <ITADataSourcesDefinitions.h>
#include <ITANetAudioProtocol.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <ITANetAudioProtocol.h>
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <ITASampleFrame.h>
class ITADatasource;
class CITANetAudioMessage;
class CITANetAudioProtocol;
class CITANetAudioServer;
class CITANetAudioMessage;
class VistaTCPSocket;
class ITABufferedDataLoggerImplServer;
//! Network audio sample server (for connecting a net audio stream)
/**
* Audio sample transmitter for a networked signal source that can connect via TCP/IP.
*
* \sa CITANetAudioStream
* \note not thread-safe
*/
class ITA_DATA_SOURCES_API CITANetAudioStreamingServer : public VistaThreadLoop
{
public:
enum UpdateStrategy
{
AUTO = 1, //!< Automatic update rate based on sample rate and block length of client (default)
ADAPTIVE, //!< Adaptive update rate, adjusts for drifting clocks
CONSTANT, //!< Set a user-defined update rate (may cause forced pausing of sample feeding or dropouts on client side)
};
CITANetAudioStreamingServer();
virtual ~CITANetAudioStreamingServer() {};
bool Start( const std::string& sAddress, int iPort );
bool IsClientConnected() const;
std::string GetNetworkAddress() const;
int GetNetworkPort() const;
bool LoopBody();
void Stop();
void SetInputStream( ITADatasource* pInStream );
int GetNetStreamBlocklength() const;
int GetNetStreamNumberOfChannels() const;
double GetNetStreamSampleRate() const;
void SetAutomaticUpdateRate();
protected:
ITADatasource* GetInputStream() const;
private:
CITANetAudioServer* m_pNetAudioServer;
ITASampleFrame m_sfTempTransmitBuffer;
ITADatasource* m_pInputStream;
VistaConnectionIP* m_pConnection;
CITANetAudioProtocol::StreamingParameters m_oServerParams;
CITANetAudioMessage* m_pMessage;
int iServerBlockId;
ITABufferedDataLoggerImplServer* m_pServerLogger;
int m_iUpdateStrategy;
int m_iClientRingBufferFreeSamples;
friend class CITANetAudioServer;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
#include <fstream>
#include <ITANetAudioProtocol.h>
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <ITASampleFrame.h>
class ITADatasource;
class CITANetAudioMessage;
class CITANetAudioProtocol;
class CITANetAudioServer;
class CITANetAudioMessage;
class VistaTCPSocket;
class ITABufferedDataLoggerImplServer;
//! Network audio sample server (for connecting a net audio stream)
/**
* Audio sample transmitter for a networked signal source that can connect via TCP/IP.
*
* \sa CITANetAudioStream
* \note not thread-safe
*/
class ITA_DATA_SOURCES_API CITANetAudioStreamingServer : public VistaThreadLoop
{
public:
enum UpdateStrategy
{
AUTO = 1, //!< Automatic update rate based on sample rate and block length of client (default)
ADAPTIVE, //!< Adaptive update rate, adjusts for drifting clocks
CONSTANT, //!< Set a user-defined update rate (may cause forced pausing of sample feeding or dropouts on client side)
};
CITANetAudioStreamingServer();
virtual ~CITANetAudioStreamingServer() {};
bool Start( const std::string& sAddress, int iPort );
bool IsClientConnected() const;
std::string GetNetworkAddress() const;
int GetNetworkPort() const;
bool LoopBody();
void Stop();
void SetInputStream( ITADatasource* pInStream );
int GetNetStreamBlocklength() const;
int GetNetStreamNumberOfChannels() const;
double GetNetStreamSampleRate() const;
void SetAutomaticUpdateRate();
protected:
ITADatasource* GetInputStream() const;
private:
CITANetAudioServer* m_pNetAudioServer;
ITASampleFrame m_sfTempTransmitBuffer;
ITADatasource* m_pInputStream;
VistaConnectionIP* m_pConnection;
CITANetAudioProtocol::StreamingParameters m_oServerParams;
CITANetAudioMessage* m_pMessage;
int iServerBlockId;
ITABufferedDataLoggerImplServer* m_pServerLogger;
int m_iUpdateStrategy;
int m_iClientRingBufferFreeSamples;
int m_iMaxSendBlocks;
friend class CITANetAudioServer;
};
#endif // INCLUDE_WATCHER_ITA_NET_AUDIO_STREAMING_SERVER
......@@ -59,6 +59,7 @@ CITANetAudioStreamingServer::CITANetAudioStreamingServer( )
, m_pNetAudioServer( new CITANetAudioServer( ) )
{
iServerBlockId = 0;
m_iMaxSendBlocks = 20;
m_iClientRingBufferFreeSamples = 0;
}
......@@ -114,47 +115,74 @@ bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort
bool CITANetAudioStreamingServer::LoopBody( )
{
bool bAskClient = false;
m_pMessage->ResetMessage( );
ITAServerLog oLog;
oLog.uiBlockId = ++iServerBlockId;
int iMsgType;
// Sending Samples
if ( m_iClientRingBufferFreeSamples >= int( m_pInputStream->GetBlocklength() ) )
unsigned int uiBlockLength = m_pInputStream->GetBlocklength( );
if ( m_iClientRingBufferFreeSamples >= uiBlockLength )
{
// Send Samples
for ( int i = 0; i < int( m_pInputStream->GetNumberOfChannels( ) ); i++ )
// Sende max 10 * Blocklnge aufeinmal
int iSendBlocks;
if ( m_iClientRingBufferFreeSamples > m_iMaxSendBlocks * uiBlockLength )
iSendBlocks = m_iMaxSendBlocks;
else
{
ITAStreamInfo oStreamInfo;
oStreamInfo.nSamples = m_iClientRingBufferFreeSamples;
const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
if ( pfData != 0 )
m_sfTempTransmitBuffer[ i ].write( pfData, m_iClientRingBufferFreeSamples );
iSendBlocks = m_iClientRingBufferFreeSamples / uiBlockLength;
bAskClient = true;
}
if ( m_sfTempTransmitBuffer.GetLength( ) != iSendBlocks * uiBlockLength )
m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels( ), iSendBlocks * uiBlockLength, false );
for ( int j = 0; j < iSendBlocks; j++ )
{
for ( int i = 0; i < int( m_pInputStream->GetNumberOfChannels( ) ); i++ )
{
ITAStreamInfo oStreamInfo;
oStreamInfo.nSamples = uiBlockLength;
const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
if ( pfData != 0 )
m_sfTempTransmitBuffer[ i ].write( pfData, uiBlockLength, j * uiBlockLength );
}
m_pInputStream->IncrementBlockPointer( );
}
m_pInputStream->IncrementBlockPointer( );
iMsgType = CITANetAudioProtocol::NP_SERVER_SENDING_SAMPLES;
m_pMessage->SetMessageType( iMsgType );
m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
m_pMessage->WriteMessage( );
m_iClientRingBufferFreeSamples -= m_iClientRingBufferFreeSamples;
m_iClientRingBufferFreeSamples -= iSendBlocks * uiBlockLength;
#ifdef NET_AUDIO_SHOW_TRAFFIC
vstr::out( ) << "[ITANetAudioStreamingServer] Transmitted " << m_sfTempTransmitBuffer.GetLength( ) << " samples for "
vstr::out( ) << "[ITANetAudioStreamingServer] Transmitted " << iSendSamples << " samples for "
<< m_pInputStream->GetNumberOfChannels( ) << " channels" << std::endl;
#endif
}
else
bAskClient = true;
if ( bAskClient )
{
// Waiting for Trigger
#ifdef NET_AUDIO_SHOW_TRAFFIC
vstr::out( ) << "[ITANetAudioStreamingServer] Not enough free samples in client buffer, requesting a trigger when more free samples available" << std::endl;
#endif
ITAServerLog oLog;
oLog.uiBlockId = ++iServerBlockId;
m_pMessage->ResetMessage( );
iMsgType = CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE_SAMPLES;
m_pMessage->SetMessageType( iMsgType );
m_pMessage->WriteBool( true );
m_pMessage->WriteMessage( );
oLog.iProtocolStatus = iMsgType;
#ifdef NET_AUDIO_SHOW_TRAFFIC
vstr::out( ) << "[ITANetAudioStreamingServer] Not enough free samples in client buffer, requesting a trigger when more free samples available" << std::endl;
#endif
oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
m_pServerLogger->log( oLog );
}
oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
m_pServerLogger->log( oLog );
// Try to Empfange Daten
m_pMessage->ResetMessage( );
......@@ -235,6 +263,8 @@ std::string CITANetAudioStreamingServer::GetNetworkAddress( ) const
return m_pNetAudioServer->GetServerAddress( );
}
int CITANetAudioStreamingServer::GetNetworkPort( ) const
{
return m_pNetAudioServer->GetNetworkPort( );
......
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