| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2017 Edouard Griffiths, F4EXB                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 06:39:30 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <assert.h>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  | #include "dsp/samplesourcefifo.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-13 14:21:36 +01:00
										 |  |  | #include "util/timeutil.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | #include "remoteoutputworker.h"
 | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | RemoteOutputWorker::RemoteOutputWorker(SampleSourceFifo* sampleFifo, QObject* parent) : | 
					
						
							|  |  |  | 	QObject(parent), | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 	m_running(false), | 
					
						
							|  |  |  | 	m_samplesChunkSize(0), | 
					
						
							|  |  |  | 	m_sampleFifo(sampleFifo), | 
					
						
							|  |  |  | 	m_samplesCount(0), | 
					
						
							| 
									
										
										
										
											2017-06-05 01:18:35 +02:00
										 |  |  | 	m_chunkCorrection(0), | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  |     m_samplerate(0), | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     m_throttlems(REMOTEOUTPUT_THROTTLE_MS), | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  |     m_maxThrottlems(50), | 
					
						
							|  |  |  |     m_throttleToggle(false) | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | RemoteOutputWorker::~RemoteOutputWorker() | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (m_running) { | 
					
						
							|  |  |  | 		stopWork(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | void RemoteOutputWorker::startWork() | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | 	qDebug() << "RemoteOutputWorker::startWork: "; | 
					
						
							| 
									
										
										
										
											2019-12-12 18:24:50 +01:00
										 |  |  | 	m_udpSinkFEC.startSender(); | 
					
						
							| 
									
										
										
										
											2017-05-21 04:41:47 +02:00
										 |  |  |     m_maxThrottlems = 0; | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  |     m_running = true; | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | void RemoteOutputWorker::stopWork() | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | 	qDebug() << "RemoteOutputWorker::stopWork"; | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 	m_running = false; | 
					
						
							| 
									
										
										
										
											2019-12-12 18:24:50 +01:00
										 |  |  | 	m_udpSinkFEC.stopSender(); | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | void RemoteOutputWorker::setSamplerate(int samplerate) | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (samplerate != m_samplerate) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | 	    qDebug() << "RemoteOutputWorker::setSamplerate:" | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 	            << " new:" << samplerate | 
					
						
							|  |  |  | 	            << " old:" << m_samplerate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	    bool wasRunning = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (m_running) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			stopWork(); | 
					
						
							|  |  |  | 			wasRunning = true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// resize sample FIFO
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  | 		if (m_sampleFifo) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             unsigned int fifoRate = std::max( | 
					
						
							|  |  |  |                 (unsigned int) samplerate, | 
					
						
							|  |  |  |                 48000U); | 
					
						
							|  |  |  |             m_sampleFifo->resize(SampleSourceFifo::getSizePolicy(fifoRate)); | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_samplerate = samplerate; | 
					
						
							|  |  |  |         m_samplesChunkSize = (m_samplerate * m_throttlems) / 1000; | 
					
						
							| 
									
										
										
										
											2018-08-29 22:08:58 +02:00
										 |  |  |         m_udpSinkFEC.setSampleRate(m_samplerate); | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (wasRunning) { | 
					
						
							|  |  |  |             startWork(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | void RemoteOutputWorker::connectTimer(const QTimer& timer) | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | 	qDebug() << "RemoteOutputWorker::connectTimer"; | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 	connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | void RemoteOutputWorker::tick() | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (m_running) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  |         qint64 throttlems = m_elapsedTimer.restart(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (throttlems != m_throttlems) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_throttlems = throttlems; | 
					
						
							|  |  |  |             m_samplesChunkSize = (m_samplerate * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000; | 
					
						
							| 
									
										
										
										
											2017-06-05 01:18:35 +02:00
										 |  |  |             m_samplesChunkSize = m_samplesChunkSize + m_chunkCorrection > 0 ? m_samplesChunkSize + m_chunkCorrection : m_samplesChunkSize; | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  |             m_throttleToggle = !m_throttleToggle; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         SampleVector::iterator readUntil; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         SampleVector& data = m_sampleFifo->getData(); | 
					
						
							|  |  |  |         unsigned int iPart1Begin, iPart1End, iPart2Begin, iPart2End; | 
					
						
							|  |  |  |         m_sampleFifo->read(m_samplesChunkSize, iPart1Begin, iPart1End, iPart2Begin, iPart2End); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (iPart1Begin != iPart1End) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             SampleVector::iterator beginRead = data.begin() + iPart1Begin; | 
					
						
							|  |  |  |             unsigned int partSize = iPart1End - iPart1Begin; | 
					
						
							|  |  |  |             m_udpSinkFEC.write(beginRead, partSize); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         if (iPart2Begin != iPart2End) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             SampleVector::iterator beginRead = data.begin() + iPart2Begin; | 
					
						
							|  |  |  |             unsigned int partSize = iPart2End - iPart2Begin; | 
					
						
							|  |  |  |             m_udpSinkFEC.write(beginRead, partSize); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-15 00:58:56 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-04 08:43:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 09:42:33 +02:00
										 |  |  | uint32_t RemoteOutputWorker::getSamplesCount(uint64_t& ts_usecs) const | 
					
						
							| 
									
										
										
										
											2018-09-04 08:43:07 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-13 14:21:36 +01:00
										 |  |  |     ts_usecs = TimeUtil::nowus(); | 
					
						
							| 
									
										
										
										
											2018-09-04 08:43:07 +02:00
										 |  |  |     return m_samplesCount; | 
					
						
							|  |  |  | } |