Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

ITANetAudioStream.cpp 10.5 KB
Newer Older
Anne Heimes's avatar
Anne Heimes committed
1
2
3
4
5
6
7
8
9
10
11
#include <ITANetAudioStream.h>

#include <ITANetAudioStreamingClient.h>

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

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

// STL includes
Anne Heimes's avatar
Anne Heimes committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
#include <cmath>
#include <iostream>

//! Audio streaming log item
struct ITAStreamLog : public ITALogDataBase
{
	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;
		os << "\t" << iStreamingStatus;
		os << "\t" << iFreeSamples;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId; //!< Block identifier (audio streaming)
	double dWorldTimeStamp;
	double dStreamingTimeCode;
	int iStreamingStatus; //!< ... usw
	int iFreeSamples;

};

//! Network streaming log item
struct ITANetLog : public ITALogDataBase
{
	inline static std::ostream& outputDesc( std::ostream& os )
	{
		os << "BlockId";
		os << "\t" << "WorldTimeStamp";
		os << "\t" << "Bufferstatus";
		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;
		os << "\t" << iBufferStatus;
		os << "\t" << iFreeSamples;
		os << "\t" << iNumSamplesTransmitted;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId;
	double dWorldTimeStamp;
	int iBufferStatus;
	int iFreeSamples;
	int iNumSamplesTransmitted;
};

class ITABufferedDataLoggerImplStream : public ITABufferedDataLogger < ITAStreamLog > {};
class ITABufferedDataLoggerImplNet : public ITABufferedDataLogger < ITANetLog > {};


CITANetAudioStream::CITANetAudioStream( int iChannels, double dSamplingRate, int iBufferSize, int iRingBufferCapacity )
	: m_sfOutputStreamBuffer( iChannels, iBufferSize, true )
	, m_dSampleRate( dSamplingRate )
	, m_sfRingBuffer( iChannels, iRingBufferCapacity, true )
	, m_bRingBufferFull( false )
	, m_iStreamingStatus( INVALID )
	, m_dLastStreamingTimeCode( 0.0f )
Anne Heimes's avatar
Anne Heimes committed
95
	, m_iTargetSampleLatency( iRingBufferCapacity )
Anne Heimes's avatar
Anne Heimes committed
96
97
98
99
100
101
102
103
104
{
	m_bRingBufferFull = false;
	if( iBufferSize > iRingBufferCapacity )
		ITA_EXCEPT1( INVALID_PARAMETER, "Ring buffer capacity can not be smaller than buffer size." );

	m_pNetAudioStreamingClient = new CITANetAudioStreamingClient( this );
	m_iReadCursor = 0;
	m_iWriteCursor = 0; // always ahead, i.e. iWriteCursor >= iReadCursor if unwrapped

105
	m_iStreamingStatus = STOPPED;
106

107
	// Logging
Anne Heimes's avatar
Anne Heimes committed
108
109
110
111
112
113
114
115
116
117
118
119
	m_pStreamLogger = new ITABufferedDataLoggerImplStream();
	m_pStreamLogger->setOutputFile( "NetAudioLogStream.txt" );
	iAudioStreamingBlockID = 0;

	m_pNetLogger = new ITABufferedDataLoggerImplNet();
	m_pNetLogger->setOutputFile( "NetAudioLogNet.txt" );
	iNetStreamingBlockID = 0;
}

CITANetAudioStream::~CITANetAudioStream()
{
	delete m_pNetLogger;
Anne's avatar
Anne committed
120
121
	delete m_pStreamLogger;
	delete m_pNetAudioStreamingClient;
Anne Heimes's avatar
Anne Heimes committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
170
171
172
173
174
175
176
177
178
179
180
}

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

bool CITANetAudioStream::GetIsConnected() const
{
	return m_pNetAudioStreamingClient->GetIsConnected();
}

void CITANetAudioStream::SetAllowedLatencySeconds( float fLatencySeconds )
{
	SetAllowedLatencySamples( (int) std::floor( GetSampleRate() * fLatencySeconds ) );
}

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

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

void CITANetAudioStream::SetAllowedLatencySamples( int iLatencySamples )
{
	if( iLatencySamples < GetMinimumLatencySamples() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Can not set latency lower than the minimum possible" );

	if( iLatencySamples > GetMaximumLatencySamples() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Can not set latency greater than the maximum possible" );

	m_iTargetSampleLatency = iLatencySamples;
}

float CITANetAudioStream::GetAllowedLatencySeconds() const
{
	return float( m_iTargetSampleLatency / GetSampleRate() );
}

int CITANetAudioStream::GetAllowedLatencySamples() const
{
	return m_iTargetSampleLatency;
}

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

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

181
182
183
184
185
void CITANetAudioStream::SetLatencyForRealtime()
{
	SetAllowedLatencySamples( GetMinimumLatencySamples() );
}

Anne Heimes's avatar
Anne Heimes committed
186
187
188
189
190
const float* CITANetAudioStream::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* pInfo )
{
	if( !GetIsConnected() )
	{
		m_sfOutputStreamBuffer[ uiChannel ].Zero( );
191
		m_iStreamingStatus = STOPPED;
Anne Heimes's avatar
Anne Heimes committed
192
193
194
195
196
197
198
	}
	else
	{
		if( GetIsRingBufferEmpty() )
		{
			m_sfOutputStreamBuffer[ uiChannel ].Zero();
			m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
199
#if NET_AUDIO_SHOW_TRAFFIC
200
			//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
201
#endif
Anne Heimes's avatar
Anne Heimes committed
202
203
204
205
206
207
208
209
		}
		else
		{
			if( GetRingBufferAvailableSamples() < int( GetBlocklength() ) )
			{
				// @todo: fade out
				m_sfRingBuffer[ uiChannel ].Zero();
				m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
210
#if NET_AUDIO_SHOW_TRAFFIC
211
				//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
212
#endif
Anne Heimes's avatar
Anne Heimes committed
213
214
215
216
217
218
			}
			else
			{
				// Normal behaviour (if everything is OK with ring buffer status)
				m_sfRingBuffer[ uiChannel ].cyclic_read( m_sfOutputStreamBuffer[ uiChannel ].GetData(), GetBlocklength(), m_iReadCursor );
				m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
219
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
220
				vstr::out() << "[ Stream ] Streaming" << std::endl;
Anne's avatar
Anne committed
221
#endif
Anne Heimes's avatar
Anne Heimes committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
			}
		}		
	}

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

	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( );
236
237
238
239
	if ( !GetIsConnected( ) )
	{
		m_iStreamingStatus = STOPPED;
	} else if ( iSavedSample >= int( GetBlocklength( ) ) )
Anne Heimes's avatar
Anne Heimes committed
240
241
242
243
	{
		//es wurden Samples abgespielt
		m_iReadCursor = ( m_iReadCursor + m_sfOutputStreamBuffer.GetLength() ) % m_sfRingBuffer.GetLength();
		m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
244
#if NET_AUDIO_SHOW_TRAFFIC
245
		//vstr::out() << "[ Stream ] Streaming" << std::endl;
Anne's avatar
Anne committed
246
#endif
Anne Heimes's avatar
Anne Heimes committed
247
248
249
250
	}
	else if ( GetIsRingBufferEmpty( ) )
	{
		m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
251
#if NET_AUDIO_SHOW_TRAFFIC
252
		//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
253
#endif
Anne Heimes's avatar
Anne Heimes committed
254
255
256
	}
	else
	{
257
		m_iStreamingStatus = BUFFER_UNDERRUN;
Anne's avatar
Anne committed
258
#if NET_AUDIO_SHOW_TRAFFIC
259
		//vstr::out() << "[ Stream ] Buffer underrun" << std::endl;
Anne's avatar
Anne committed
260
#endif
Anne Heimes's avatar
Anne Heimes committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
		m_iReadCursor = m_iWriteCursor;
	}
	m_bRingBufferFull = false;

	ITAStreamLog oLog;
	oLog.iStreamingStatus = m_iStreamingStatus;
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock()->getTime();
	oLog.dStreamingTimeCode = m_dLastStreamingTimeCode;
	oLog.uiBlockId = ++iAudioStreamingBlockID;
	oLog.iFreeSamples = GetRingBufferFreeSamples();
	m_pStreamLogger->log( oLog );
	
	m_pNetAudioStreamingClient->TriggerBlockIncrement();
}

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

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

	if ( ( m_iWriteCursor == m_iReadCursor ) && m_bRingBufferFull )
	{
		// BufferFull
289
		m_iStreamingStatus = BUFFER_OVERRUN;
Anne Heimes's avatar
Anne Heimes committed
290
		oLog.iBufferStatus = 1;
Anne's avatar
Anne committed
291
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
292
		vstr::out() << "[ NetAudio ] Buffer overrun" << std::endl;
Anne's avatar
Anne committed
293
#endif
Anne Heimes's avatar
Anne Heimes committed
294
295
296
297
298
	}
	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;
299
300

		m_iStreamingStatus = BUFFER_OVERRUN;
Anne Heimes's avatar
Anne Heimes committed
301
302
303
304
305
306
307
308
309
		m_iWriteCursor = m_iReadCursor;
		oLog.iBufferStatus = 2;
	}
	else
	{
		// write samples into ring buffer
		m_sfRingBuffer.cyclic_write( sfNewSamples, iNumSamples, 0, iCurrentWriteCursor );
		m_bRingBufferFull = false;
		oLog.iBufferStatus = 1;
310
		m_iStreamingStatus = STREAMING;
Anne's avatar
Anne committed
311
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
312
		vstr::out() << "[ NetAudio ] Buffer write" << std::endl;
Anne's avatar
Anne committed
313
#endif
Anne Heimes's avatar
Anne Heimes committed
314
315
316
317
318
319
320

		// set write curser
		m_iWriteCursor = ( m_iWriteCursor + iNumSamples ) % GetRingBufferSize( );
		if ( m_iWriteCursor == m_iReadCursor )
		{
			m_bRingBufferFull = true;
			oLog.iBufferStatus = 1;
321
322

			m_iStreamingStatus = BUFFER_OVERRUN;
Anne's avatar
Anne committed
323
#if NET_AUDIO_SHOW_TRAFFIC
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
324
			vstr::out() << "[ NetAudio ] Buffer overrun" << std::endl;
Anne's avatar
Anne committed
325
#endif
Anne Heimes's avatar
Anne Heimes committed
326
327
		}
	}
328
	oLog.iBufferStatus = m_iStreamingStatus;
Anne Heimes's avatar
Anne Heimes committed
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
	oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
	oLog.uiBlockId = ++iAudioStreamingBlockID;
	oLog.iFreeSamples = GetRingBufferFreeSamples( );
	oLog.iNumSamplesTransmitted = iNumSamples;
	m_pNetLogger->log( oLog );
	
	return GetRingBufferFreeSamples();
}

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

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

	int iFreeSamples = GetRingBufferSize() - ( ( m_iWriteCursor - m_iReadCursor + GetRingBufferSize() ) % GetRingBufferSize() );
	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;
}