| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
 | 
					
						
							|  |  |  | // written by Christian Daniel                                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | #include "samplesinkfifo.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | void SampleSinkFifo::create(uint s) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	m_size = 0; | 
					
						
							|  |  |  | 	m_fill = 0; | 
					
						
							|  |  |  | 	m_head = 0; | 
					
						
							|  |  |  | 	m_tail = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_data.resize(s); | 
					
						
							|  |  |  | 	m_size = m_data.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(m_size != s) | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qCritical("SampleSinkFifo: out of memory"); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | SampleSinkFifo::SampleSinkFifo(QObject* parent) : | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	QObject(parent), | 
					
						
							|  |  |  | 	m_data() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_suppressed = -1; | 
					
						
							|  |  |  | 	m_size = 0; | 
					
						
							|  |  |  | 	m_fill = 0; | 
					
						
							|  |  |  | 	m_head = 0; | 
					
						
							|  |  |  | 	m_tail = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | SampleSinkFifo::SampleSinkFifo(int size, QObject* parent) : | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	QObject(parent), | 
					
						
							|  |  |  | 	m_data() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_suppressed = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	create(size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | SampleSinkFifo::~SampleSinkFifo() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_size = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | bool SampleSinkFifo::setSize(int size) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	create(size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return m_data.size() == (uint)size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | uint SampleSinkFifo::write(const quint8* data, uint count) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 	uint total; | 
					
						
							|  |  |  | 	uint remaining; | 
					
						
							|  |  |  | 	uint len; | 
					
						
							|  |  |  | 	const Sample* begin = (const Sample*)data; | 
					
						
							|  |  |  | 	count /= 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total = MIN(count, m_size - m_fill); | 
					
						
							|  |  |  | 	if(total < count) { | 
					
						
							|  |  |  | 		if(m_suppressed < 0) { | 
					
						
							|  |  |  | 			m_suppressed = 0; | 
					
						
							|  |  |  | 			m_msgRateTimer.start(); | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 			qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			if(m_msgRateTimer.elapsed() > 2500) { | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 				qCritical("SampleSinkFifo: %u messages dropped", m_suppressed); | 
					
						
							|  |  |  | 				qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 				m_suppressed = -1; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				m_suppressed++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	remaining = total; | 
					
						
							|  |  |  | 	while(remaining > 0) { | 
					
						
							|  |  |  | 		len = MIN(remaining, m_size - m_tail); | 
					
						
							|  |  |  | 		std::copy(begin, begin + len, m_data.begin() + m_tail); | 
					
						
							|  |  |  | 		m_tail += len; | 
					
						
							|  |  |  | 		m_tail %= m_size; | 
					
						
							|  |  |  | 		m_fill += len; | 
					
						
							|  |  |  | 		begin += len; | 
					
						
							|  |  |  | 		remaining -= len; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(m_fill > 0) | 
					
						
							|  |  |  | 		emit dataReady(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return total; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | uint SampleSinkFifo::write(SampleVector::const_iterator begin, SampleVector::const_iterator end) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 	uint count = end - begin; | 
					
						
							|  |  |  | 	uint total; | 
					
						
							|  |  |  | 	uint remaining; | 
					
						
							|  |  |  | 	uint len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total = MIN(count, m_size - m_fill); | 
					
						
							|  |  |  | 	if(total < count) { | 
					
						
							|  |  |  | 		if(m_suppressed < 0) { | 
					
						
							|  |  |  | 			m_suppressed = 0; | 
					
						
							|  |  |  | 			m_msgRateTimer.start(); | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 			qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			if(m_msgRateTimer.elapsed() > 2500) { | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 				qCritical("SampleSinkFifo: %u messages dropped", m_suppressed); | 
					
						
							|  |  |  | 				qCritical("SampleSinkFifo: overflow - dropping %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 				m_suppressed = -1; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				m_suppressed++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	remaining = total; | 
					
						
							|  |  |  | 	while(remaining > 0) { | 
					
						
							|  |  |  | 		len = MIN(remaining, m_size - m_tail); | 
					
						
							|  |  |  | 		std::copy(begin, begin + len, m_data.begin() + m_tail); | 
					
						
							|  |  |  | 		m_tail += len; | 
					
						
							|  |  |  | 		m_tail %= m_size; | 
					
						
							|  |  |  | 		m_fill += len; | 
					
						
							|  |  |  | 		begin += len; | 
					
						
							|  |  |  | 		remaining -= len; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(m_fill > 0) | 
					
						
							|  |  |  | 		emit dataReady(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return total; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | uint SampleSinkFifo::read(SampleVector::iterator begin, SampleVector::iterator end) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 	uint count = end - begin; | 
					
						
							|  |  |  | 	uint total; | 
					
						
							|  |  |  | 	uint remaining; | 
					
						
							|  |  |  | 	uint len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total = MIN(count, m_fill); | 
					
						
							|  |  |  | 	if(total < count) | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qCritical("SampleSinkFifo: underflow - missing %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	remaining = total; | 
					
						
							|  |  |  | 	while(remaining > 0) { | 
					
						
							|  |  |  | 		len = MIN(remaining, m_size - m_head); | 
					
						
							|  |  |  | 		std::copy(m_data.begin() + m_head, m_data.begin() + m_head + len, begin); | 
					
						
							|  |  |  | 		m_head += len; | 
					
						
							|  |  |  | 		m_head %= m_size; | 
					
						
							|  |  |  | 		m_fill -= len; | 
					
						
							|  |  |  | 		begin += len; | 
					
						
							|  |  |  | 		remaining -= len; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return total; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | uint SampleSinkFifo::readBegin(uint count, | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	SampleVector::iterator* part1Begin, SampleVector::iterator* part1End, | 
					
						
							|  |  |  | 	SampleVector::iterator* part2Begin, SampleVector::iterator* part2End) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 	uint total; | 
					
						
							|  |  |  | 	uint remaining; | 
					
						
							|  |  |  | 	uint len; | 
					
						
							|  |  |  | 	uint head = m_head; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total = MIN(count, m_fill); | 
					
						
							|  |  |  | 	if(total < count) | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qCritical("SampleSinkFifo: underflow - missing %u samples", count - total); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	remaining = total; | 
					
						
							|  |  |  | 	if(remaining > 0) { | 
					
						
							|  |  |  | 		len = MIN(remaining, m_size - head); | 
					
						
							|  |  |  | 		*part1Begin = m_data.begin() + head; | 
					
						
							|  |  |  | 		*part1End = m_data.begin() + head + len; | 
					
						
							|  |  |  | 		head += len; | 
					
						
							|  |  |  | 		head %= m_size; | 
					
						
							|  |  |  | 		remaining -= len; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		*part1Begin = m_data.end(); | 
					
						
							|  |  |  | 		*part1End = m_data.end(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(remaining > 0) { | 
					
						
							|  |  |  | 		len = MIN(remaining, m_size - head); | 
					
						
							|  |  |  | 		*part2Begin = m_data.begin() + head; | 
					
						
							|  |  |  | 		*part2End = m_data.begin() + head + len; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		*part2Begin = m_data.end(); | 
					
						
							|  |  |  | 		*part2End = m_data.end(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return total; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | uint SampleSinkFifo::readCommit(uint count) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(count > m_fill) { | 
					
						
							| 
									
										
										
										
											2016-10-08 10:35:07 +02:00
										 |  |  | 		qCritical("SampleSinkFifo: cannot commit more than available samples"); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		count = m_fill; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m_head = (m_head + count) % m_size; | 
					
						
							|  |  |  | 	m_fill -= count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return count; | 
					
						
							|  |  |  | } |