ITANetAudioStream.cpp 11.2 KB
Newer Older
Anne Heimes's avatar
Anne Heimes committed
1
#include <ITANetAudioStream.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
2 3

#include "ITANetAudioStreamingClient.h"
Anne Heimes's avatar
Anne Heimes committed
4 5 6 7 8 9 10

// ITA includes
#include <ITAException.h>
#include <ITADataLog.h>
#include <ITAStreamInfo.h>
#include <ITAClock.h>

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
11 12 13 14
// Vista includes
#include <VistaBase/VistaStreamUtils.h>

// STL includes
Anne Heimes's avatar
Anne Heimes committed
15
#include <cmath>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
16
#include <iomanip>
Anne Heimes's avatar
Anne Heimes committed
17 18
#include <iostream>

19

Jonas Stienen's avatar
Jonas Stienen committed
20 21
//! Audio streaming log item
struct ITAAudioStreamLog : public ITALogDataBase
Anne Heimes's avatar
Anne Heimes committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
	inline static std::ostream& outputDesc( std::ostream& os )
	{
		os << "BlockId";
		os << "\t" << "WorldTimeStamp";
		os << "\t" << "StreamingTimeCode";
		os << "\t" << "StreamingStatus";
		os << "\t" << "FreeSamples";
		os << std::endl;
		return os;
	};

	inline std::ostream& outputData( std::ostream& os ) const
	{
		os << uiBlockId;
		os << "\t" << std::setprecision( 12 ) << dWorldTimeStamp;
		os << "\t" << std::setprecision( 12 ) << dStreamingTimeCode;
39
		os << "\t" << sStreamingStatus;
Anne Heimes's avatar
Anne Heimes committed
40 41 42 43 44 45 46 47
		os << "\t" << iFreeSamples;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId; //!< Block identifier (audio streaming)
	double dWorldTimeStamp;
	double dStreamingTimeCode;
48
	std::string sStreamingStatus;
Anne Heimes's avatar
Anne Heimes committed
49 50 51 52 53
	int iFreeSamples;

};

//! Network streaming log item
Jonas Stienen's avatar
Jonas Stienen committed
54
struct ITANetworkStreamLog : public ITALogDataBase
Anne Heimes's avatar
Anne Heimes committed
55 56 57 58 59
{
	inline static std::ostream& outputDesc( std::ostream& os )
	{
		os << "BlockId";
		os << "\t" << "WorldTimeStamp";
60
		os << "\t" << "BufferStatus";
Anne Heimes's avatar
Anne Heimes committed
61 62 63 64 65 66 67 68 69 70
		os << "\t" << "FreeSamples";
		os << "\t" << "NumSamplesTransmitted";
		os << std::endl;
		return os;
	};
	
	inline std::ostream& outputData( std::ostream& os ) const
	{
		os << uiBlockId;
		os << "\t" << std::setprecision( 12 ) << dWorldTimeStamp;
71
		os << "\t" << sBufferStatus;
Anne Heimes's avatar
Anne Heimes committed
72 73 74 75 76 77 78 79
		os << "\t" << iFreeSamples;
		os << "\t" << iNumSamplesTransmitted;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId;
	double dWorldTimeStamp;
80
	std::string sBufferStatus;
Anne Heimes's avatar
Anne Heimes committed
81 82 83 84
	int iFreeSamples;
	int iNumSamplesTransmitted;
};

Jonas Stienen's avatar
Jonas Stienen committed
85 86
class ITABufferedDataLoggerImplStream : public ITABufferedDataLogger < ITAAudioStreamLog > {};
class ITABufferedDataLoggerImplNet : public ITABufferedDataLogger < ITANetworkStreamLog > {};
Anne Heimes's avatar
Anne Heimes committed
87 88


89
CITANetAudioStream::CITANetAudioStream(int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity /* = 2048 */)
Anne Heimes's avatar
Anne Heimes committed
90 91
	: m_sfOutputStreamBuffer( iChannels, iBufferSize, true )
	, m_dSampleRate( dSamplingRate )
92
	, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
Anne Heimes's avatar
Anne Heimes committed
93 94 95
	, m_bRingBufferFull( false )
	, m_iStreamingStatus( INVALID )
	, m_dLastStreamingTimeCode( 0.0f )
96 97 98 99
	, m_iReadCursor( 0 )
	, m_iWriteCursor( 0 ) // always ahead, i.e. iWriteCursor >= iReadCursor if unwrapped
	, m_iAudioStreamingBlockID( 0 )
	, m_iNetStreamingBlockID( 0 )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
100
	, m_bDebuggingEnabled( false )
Anne Heimes's avatar
Anne Heimes committed
101
{
102
	if( iBufferSize > iRingBufferCapacity )
103
		ITA_EXCEPT1( INVALID_PARAMETER, "Ring buffer capacity can not be smaller than Target Sample Latency." );
Anne Heimes's avatar
Anne Heimes committed
104 105

	m_pNetAudioStreamingClient = new CITANetAudioStreamingClient( this );
106

107
	// Logging
Jonas Stienen's avatar
Jonas Stienen committed
108 109
	m_pAudioStreamLogger = new ITABufferedDataLoggerImplStream();
	m_pNetworkStreamLogger = new ITABufferedDataLoggerImplNet();
110

Jonas Stienen's avatar
Jonas Stienen committed
111
	SetNetAudioStreamingLoggerBaseName( "ITANetAudioStream" );
112 113

	 m_iStreamingStatus = STOPPED;
Anne Heimes's avatar
Anne Heimes committed
114 115 116 117
}

CITANetAudioStream::~CITANetAudioStream()
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
118
	if( GetIsDebuggingEnabled() == false )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
119 120 121 122 123
	{
		m_pAudioStreamLogger->setOutputFile( "" ); // Disables file storing
		m_pNetworkStreamLogger->setOutputFile( "" ); // Disables file storing
	}

Jonas Stienen's avatar
Jonas Stienen committed
124 125
	delete m_pAudioStreamLogger;
	delete m_pNetworkStreamLogger;
Anne's avatar
Anne committed
126
	delete m_pNetAudioStreamingClient;
Anne Heimes's avatar
Anne Heimes committed
127 128 129 130 131 132 133
}

bool CITANetAudioStream::Connect( const std::string& sAddress, int iPort )
{
	return m_pNetAudioStreamingClient->Connect( sAddress, iPort );
}

134 135 136 137 138
void CITANetAudioStream::Disconnect()
{
	m_pNetAudioStreamingClient->Disconnect();
}

Anne Heimes's avatar
Anne Heimes committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
bool CITANetAudioStream::GetIsConnected() const
{
	return m_pNetAudioStreamingClient->GetIsConnected();
}

float CITANetAudioStream::GetMaximumLatencySeconds() const
{
	return float( GetMaximumLatencySamples() / GetSampleRate() );
}

float CITANetAudioStream::GetMinimumLatencySeconds() const
{
	return float( GetMinimumLatencySamples() / GetSampleRate() );
}

int CITANetAudioStream::GetMinimumLatencySamples() const
{
	// At least one block
	return GetBlocklength();
}

int CITANetAudioStream::GetMaximumLatencySamples() const
{
	return GetRingBufferSize();
}

const float* CITANetAudioStream::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* pInfo )
{
	if( !GetIsConnected() )
	{
		m_sfOutputStreamBuffer[ uiChannel ].Zero( );
Anne's avatar
Anne committed
170 171
		if (uiChannel == 0 )
			m_iStreamingStatus = STOPPED;
Anne Heimes's avatar
Anne Heimes committed
172 173 174 175 176 177
	}
	else
	{
		if( GetIsRingBufferEmpty() )
		{
			m_sfOutputStreamBuffer[ uiChannel ].Zero();
Anne's avatar
Anne committed
178 179
			if (uiChannel == 0 )
				m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
180
#if NET_AUDIO_SHOW_TRAFFIC
181
			//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
182
#endif
Anne Heimes's avatar
Anne Heimes committed
183 184 185 186 187 188 189
		}
		else
		{
			if( GetRingBufferAvailableSamples() < int( GetBlocklength() ) )
			{
				// @todo: fade out
				m_sfRingBuffer[ uiChannel ].Zero();
Anne's avatar
Anne committed
190 191
				if (uiChannel == 0 )
					m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
192
#if NET_AUDIO_SHOW_TRAFFIC
193
				//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
194
#endif
Anne Heimes's avatar
Anne Heimes committed
195 196 197 198 199
			}
			else
			{
				// Normal behaviour (if everything is OK with ring buffer status)
				m_sfRingBuffer[ uiChannel ].cyclic_read( m_sfOutputStreamBuffer[ uiChannel ].GetData(), GetBlocklength(), m_iReadCursor );
Anne's avatar
Anne committed
200 201
				if ( uiChannel == 0 )
					m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
202
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
203
				vstr::out() << "[ Stream ] Streaming" << std::endl;
Anne's avatar
Anne committed
204
#endif
Anne Heimes's avatar
Anne Heimes committed
205 206 207 208 209 210 211
			}
		}		
	}

	if( uiChannel == 0 )
		m_dLastStreamingTimeCode = pInfo->dTimecode;

Anne's avatar
Anne committed
212

Anne Heimes's avatar
Anne Heimes committed
213 214 215 216 217 218 219
	return m_sfOutputStreamBuffer[uiChannel].GetData();
}

void CITANetAudioStream::IncrementBlockPointer()
{
	// Increment read cursor by one audio block and wrap around if exceeding ring buffer
	int iSavedSample = GetRingBufferSize( ) - GetRingBufferFreeSamples( );
220 221
	if ( !GetIsConnected( ) )
	{
Anne's avatar
Anne committed
222
		//m_iStreamingStatus = STOPPED;
223
	} else if ( iSavedSample >= int( GetBlocklength( ) ) )
Anne Heimes's avatar
Anne Heimes committed
224 225 226
	{
		//es wurden Samples abgespielt
		m_iReadCursor = ( m_iReadCursor + m_sfOutputStreamBuffer.GetLength() ) % m_sfRingBuffer.GetLength();
Anne's avatar
Anne committed
227
		//m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
228
#if NET_AUDIO_SHOW_TRAFFIC
229
		//vstr::out() << "[ Stream ] Streaming" << std::endl;
Anne's avatar
Anne committed
230
#endif
Anne Heimes's avatar
Anne Heimes committed
231 232 233
	}
	else if ( GetIsRingBufferEmpty( ) )
	{
Anne's avatar
Anne committed
234
		//m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
235
#if NET_AUDIO_SHOW_TRAFFIC
236
		//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
237
#endif
Anne Heimes's avatar
Anne Heimes committed
238 239 240
	}
	else
	{
Anne's avatar
Anne committed
241
		//m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
242
#if NET_AUDIO_SHOW_TRAFFIC
243
		//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
244
#endif
Anne Heimes's avatar
Anne Heimes committed
245 246 247 248
		m_iReadCursor = m_iWriteCursor;
	}
	m_bRingBufferFull = false;

Jonas Stienen's avatar
Jonas Stienen committed
249
	ITAAudioStreamLog oLog;	
250
	oLog.sStreamingStatus = GetStreamingStatusString( m_iStreamingStatus );
Anne Heimes's avatar
Anne Heimes committed
251 252
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock()->getTime();
	oLog.dStreamingTimeCode = m_dLastStreamingTimeCode;
253
	oLog.uiBlockId = ++m_iAudioStreamingBlockID;
254
	oLog.iFreeSamples = GetRingBufferFreeSamples( );
Jonas Stienen's avatar
Jonas Stienen committed
255
	m_pAudioStreamLogger->log( oLog );
Anne Heimes's avatar
Anne Heimes committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269
}

int CITANetAudioStream::Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples )
{
	// Take local copies (concurrent access)
	int iCurrentReadCursor = m_iReadCursor;
	int iCurrentWriteCursor = m_iWriteCursor;

	if( iCurrentWriteCursor < iCurrentReadCursor )
		iCurrentWriteCursor += GetRingBufferSize(); // Unwrap, because write cursor always ahead

	if ( ( m_iWriteCursor == m_iReadCursor ) && m_bRingBufferFull )
	{
		// BufferFull
270
		m_iStreamingStatus = BUFFER_OVERRUN;
Anne's avatar
Anne committed
271
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
272
		vstr::out() << "[ NetAudio ] Buffer overrun" << std::endl;
Anne's avatar
Anne committed
273
#endif
Anne Heimes's avatar
Anne Heimes committed
274 275 276 277 278
	}
	else if( GetRingBufferFreeSamples() < iNumSamples )
	{
		// @todo: only partly write
		//std::cerr << "BUFFER_OVERRUN! Would partly write samples because ring buffer will be full then." << std::endl;
279 280

		m_iStreamingStatus = BUFFER_OVERRUN;
Anne Heimes's avatar
Anne Heimes committed
281 282 283 284 285 286 287
		m_iWriteCursor = m_iReadCursor;
	}
	else
	{
		// write samples into ring buffer
		m_sfRingBuffer.cyclic_write( sfNewSamples, iNumSamples, 0, iCurrentWriteCursor );
		m_bRingBufferFull = false;
288
		m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
289
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
290
		vstr::out() << "[ NetAudio ] Buffer write" << std::endl;
Anne's avatar
Anne committed
291
#endif
Anne Heimes's avatar
Anne Heimes committed
292 293 294 295 296 297

		// set write curser
		m_iWriteCursor = ( m_iWriteCursor + iNumSamples ) % GetRingBufferSize( );
		if ( m_iWriteCursor == m_iReadCursor )
		{
			m_bRingBufferFull = true;
Anne's avatar
Anne committed
298
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
299
			vstr::out() << "[ NetAudio ] Buffer overrun" << std::endl;
Anne's avatar
Anne committed
300
#endif
Anne Heimes's avatar
Anne Heimes committed
301 302
		}
	}
303 304

	ITANetworkStreamLog oLog;
305
	oLog.sBufferStatus = GetStreamingStatusString( m_iStreamingStatus );
Anne Heimes's avatar
Anne Heimes committed
306
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
307 308
	oLog.uiBlockId = ++m_iAudioStreamingBlockID;
	oLog.iFreeSamples = GetRingBufferFreeSamples();
Anne Heimes's avatar
Anne Heimes committed
309
	oLog.iNumSamplesTransmitted = iNumSamples;
Jonas Stienen's avatar
Jonas Stienen committed
310
	m_pNetworkStreamLogger->log( oLog );
Anne Heimes's avatar
Anne Heimes committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324
	
	return GetRingBufferFreeSamples();
}

int CITANetAudioStream::GetRingBufferAvailableSamples() const
{
	return GetRingBufferSize() - GetRingBufferFreeSamples();
}

int CITANetAudioStream::GetRingBufferFreeSamples() const
{
	if( m_bRingBufferFull )
		return 0;

325
	int iFreeSamples = GetRingBufferSize() - ( ( m_iWriteCursor - m_iReadCursor + GetRingBufferSize() ) % GetRingBufferSize() );
Anne Heimes's avatar
Anne Heimes committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	assert( iFreeSamples > 0 );
	return iFreeSamples;
}

int CITANetAudioStream::GetRingBufferSize() const
{
	return m_sfRingBuffer.GetLength();
}

bool CITANetAudioStream::GetIsRingBufferFull() const
{
	return m_bRingBufferFull;
}

bool CITANetAudioStream::GetIsRingBufferEmpty() const
{
	return ( !m_bRingBufferFull && m_iReadCursor == m_iWriteCursor );
}

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;
}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

std::string CITANetAudioStream::GetStreamingStatusString( int iStreamingStatus )
{
	if( iStreamingStatus == CITANetAudioStream::INVALID )
		return "INVALID";
	if( iStreamingStatus == CITANetAudioStream::STOPPED )
		return "STOPPED";
	if( iStreamingStatus == CITANetAudioStream::CONNECTED )
		return "CONNECTED";
	if( iStreamingStatus == CITANetAudioStream::STREAMING )
		return "STREAMING";
	if( iStreamingStatus == CITANetAudioStream::BUFFER_UNDERRUN )
		return "BUFFER_UNDERRUN";
	if( iStreamingStatus == CITANetAudioStream::BUFFER_OVERRUN )
		return "BUFFER_OVERRUN";

	return "UNKOWN";
}

std::string CITANetAudioStream::GetNetAudioStreamLoggerBaseName() const
{
	return m_sNetAudioStreamLoggerBaseName;
}

void CITANetAudioStream::SetNetAudioStreamingLoggerBaseName( const std::string& sBaseName )
{
	m_sNetAudioStreamLoggerBaseName = sBaseName;
	m_pAudioStreamLogger->setOutputFile( GetNetAudioStreamLoggerBaseName() + "_AudioStream.log" );
	m_pNetworkStreamLogger->setOutputFile( GetNetAudioStreamLoggerBaseName() + "_NetworkStream.log" );
	m_pNetAudioStreamingClient->SetClientLoggerBaseName( sBaseName );
}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
390

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
391
void CITANetAudioStream::SetDebuggingEnabled( bool bEnabled )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
392
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
393
	m_bDebuggingEnabled = bEnabled;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
394
	m_pNetAudioStreamingClient->SetDebuggingEnabled( bEnabled );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
395 396
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
397
bool CITANetAudioStream::GetIsDebuggingEnabled() const
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
398
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
399
	return m_bDebuggingEnabled;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
400
}