| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-18 06:36:53 +01:00
										 |  |  | // Copyright (C) 2017-2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com>          //
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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-08-15 20:23:49 +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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | #include "udpsourceudphandler.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2017-09-12 13:54:32 +02:00
										 |  |  | #include <stdint.h>
 | 
					
						
							| 
									
										
										
										
											2018-02-24 10:29:27 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | #include "udpsourcemsg.h"
 | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | MESSAGE_CLASS_DEFINITION(UDPSourceUDPHandler::MsgUDPAddressAndPort, Message) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | UDPSourceUDPHandler::UDPSourceUDPHandler() : | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |     m_dataSocket(nullptr), | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     m_dataAddress(QHostAddress::LocalHost), | 
					
						
							|  |  |  |     m_remoteAddress(QHostAddress::LocalHost), | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |     m_multicastAddress(QStringLiteral("224.0.0.1")), | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     m_dataPort(9999), | 
					
						
							| 
									
										
										
										
											2017-08-18 12:31:17 +02:00
										 |  |  |     m_remotePort(0), | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     m_dataConnected(false), | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |     m_multicast(false), | 
					
						
							| 
									
										
										
										
											2017-08-18 12:31:17 +02:00
										 |  |  |     m_udpDumpIndex(0), | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  |     m_nbUDPFrames(m_minNbUDPFrames), | 
					
						
							|  |  |  |     m_nbAllocatedUDPFrames(m_minNbUDPFrames), | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     m_writeFrameIndex(0), | 
					
						
							| 
									
										
										
										
											2017-08-17 00:38:08 +02:00
										 |  |  |     m_readFrameIndex(m_minNbUDPFrames/2), | 
					
						
							| 
									
										
										
										
											2017-08-16 03:33:05 +02:00
										 |  |  |     m_readIndex(0), | 
					
						
							| 
									
										
										
										
											2017-08-17 00:38:08 +02:00
										 |  |  |     m_rwDelta(m_minNbUDPFrames/2), | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  |     m_d(0), | 
					
						
							| 
									
										
										
										
											2017-08-25 02:21:34 +02:00
										 |  |  |     m_autoRWBalance(true), | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |     m_feedbackMessageQueue(nullptr) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  |     m_udpBuf = new udpBlk_t[m_minNbUDPFrames]; | 
					
						
							| 
									
										
										
										
											2018-02-24 10:29:27 +01:00
										 |  |  |     std::fill(m_udpDump, m_udpDump + m_udpBlockSize + 8192, 0); | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  |     connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages())); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | UDPSourceUDPHandler::~UDPSourceUDPHandler() | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |     stop(); | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  |     delete[] m_udpBuf; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::start() | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |     qDebug("UDPSourceUDPHandler::start"); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 18:51:48 +02:00
										 |  |  |     if (!m_dataSocket) { | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |         m_dataSocket = new QUdpSocket(this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_dataConnected) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |         if (m_dataSocket->bind(m_multicast ? QHostAddress::AnyIPv4 : m_dataAddress, m_dataPort, QUdpSocket::ShareAddress)) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |             qDebug("UDPSourceUDPHandler::start: bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(),  m_dataPort); | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (m_multicast) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (m_dataSocket->joinMulticastGroup(m_multicastAddress)) { | 
					
						
							|  |  |  |                     qDebug("UDPSourceUDPHandler::start: joined multicast group %s", qPrintable(m_multicastAddress.toString())); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     qDebug("UDPSourceUDPHandler::start: failed joining multicast group %s", qPrintable(m_multicastAddress.toString())); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 02:48:59 +02:00
										 |  |  |             connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); // , Qt::QueuedConnection gets stuck since Qt 5.8.0
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |             m_dataConnected = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |             qWarning("UDPSourceUDPHandler::start: cannot bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(),  m_dataPort); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |             m_dataConnected = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::stop() | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |     qDebug("UDPSourceUDPHandler::stop"); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_dataConnected) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_dataConnected = false; | 
					
						
							|  |  |  |         disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_dataSocket) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         delete m_dataSocket; | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |         m_dataSocket = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::dataReadyRead() | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     while (m_dataSocket->hasPendingDatagrams() && m_dataConnected) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qint64 pendingDataSize = m_dataSocket->pendingDatagramSize(); | 
					
						
							| 
									
										
										
										
											2017-08-18 12:31:17 +02:00
										 |  |  |         qint64 bytesRead = m_dataSocket->readDatagram(&m_udpDump[m_udpDumpIndex], pendingDataSize, &m_remoteAddress, &m_remotePort); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:31:17 +02:00
										 |  |  |         if (bytesRead < 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |             qWarning("UDPSourceUDPHandler::dataReadyRead: UDP read error"); | 
					
						
							| 
									
										
										
										
											2017-08-18 12:31:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int udpDumpSize = m_udpDumpIndex + bytesRead; | 
					
						
							|  |  |  |             int udpDumpPtr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             while (udpDumpSize >= m_udpBlockSize) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 moveData(&m_udpDump[udpDumpPtr]); | 
					
						
							|  |  |  |                 udpDumpPtr += m_udpBlockSize; | 
					
						
							|  |  |  |                 udpDumpSize -= m_udpBlockSize; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (udpDumpSize > 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 memcpy(m_udpDump, &m_udpDump[udpDumpPtr], udpDumpSize); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             m_udpDumpIndex = udpDumpSize; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::moveData(char *blk) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     memcpy(m_udpBuf[m_writeFrameIndex], blk, m_udpBlockSize); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     if (m_writeFrameIndex < m_nbUDPFrames - 1) { | 
					
						
							|  |  |  |         m_writeFrameIndex++; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |         m_writeFrameIndex = 0; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::readSample(qint16 &t) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
 | 
					
						
							| 
									
										
										
										
											2017-08-25 01:16:58 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         t = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |         memcpy(&t, &m_udpBuf[m_readFrameIndex][m_readIndex], sizeof(qint16)); | 
					
						
							|  |  |  |         advanceReadPointer((int) sizeof(qint16)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::readSample(AudioSample &a) | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         a.l = 0; | 
					
						
							|  |  |  |         a.r = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         memcpy(&a, &m_udpBuf[m_readFrameIndex][m_readIndex], sizeof(AudioSample)); | 
					
						
							|  |  |  |         advanceReadPointer((int) sizeof(AudioSample)); | 
					
						
							| 
									
										
										
										
											2017-08-25 01:16:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::readSample(Sample &s) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
 | 
					
						
							| 
									
										
										
										
											2017-08-25 01:16:58 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         s.m_real = 0; | 
					
						
							|  |  |  |         s.m_imag = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  |         Sample *u = (Sample *) &m_udpBuf[m_readFrameIndex][m_readIndex]; | 
					
						
							|  |  |  |         s = *u; | 
					
						
							| 
									
										
										
										
											2017-08-25 01:16:58 +02:00
										 |  |  |         advanceReadPointer((int) sizeof(Sample)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::advanceReadPointer(int nbBytes) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_readIndex < m_udpBlockSize - 2*nbBytes) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_readIndex += nbBytes; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_readIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  |         if (m_readFrameIndex < m_nbUDPFrames - 1) | 
					
						
							| 
									
										
										
										
											2017-08-16 03:33:05 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |             m_readFrameIndex++; | 
					
						
							| 
									
										
										
										
											2017-08-16 03:33:05 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |             m_rwDelta = m_writeFrameIndex; // raw R/W delta estimate
 | 
					
						
							| 
									
										
										
										
											2018-02-24 10:29:27 +01:00
										 |  |  |             int nbUDPFrames2 = m_nbUDPFrames/2; | 
					
						
							|  |  |  |             float d = (m_rwDelta - nbUDPFrames2)/(float) m_nbUDPFrames; | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |             //qDebug("UDPSourceUDPHandler::advanceReadPointer: w: %02d d: %f", m_writeIndex, d);
 | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ((d < -0.45) || (d > 0.45)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 resetReadIndex(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 float dd = d - m_d; // derivative
 | 
					
						
							| 
									
										
										
										
											2017-08-16 12:42:07 +02:00
										 |  |  |                 float c = (d / 15.0) + (dd / 20.0); // damping and scaling
 | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  |                 c = c < -0.05 ? -0.05 : c > 0.05 ? 0.05 : c; // limit
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  |                 UDPSourceMessages::MsgSampleRateCorrection *msg = UDPSourceMessages::MsgSampleRateCorrection::create(c, d); | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-25 02:21:34 +02:00
										 |  |  |                 if (m_autoRWBalance && m_feedbackMessageQueue) { | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  |                     m_feedbackMessageQueue->push(msg); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 m_readFrameIndex = 0; | 
					
						
							|  |  |  |                 m_d = d; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  | void UDPSourceUDPHandler::configureUDPLink(const QString& address, quint16 port, const QString& multicastAddress, bool multicastJoin) | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |     Message* msg = MsgUDPAddressAndPort::create(address, port, multicastAddress, multicastJoin); | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  |     m_inputMessageQueue.push(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  | void UDPSourceUDPHandler::applyUDPLink(const QString& address, quint16 port, const QString& multicastAddress, bool multicastJoin) | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-26 18:51:48 +02:00
										 |  |  |     qDebug() << "UDPSourceUDPHandler::applyUDPLink: " | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |         << " address: " << address | 
					
						
							|  |  |  |         << " port: " << port | 
					
						
							|  |  |  |         << " multicastAddress: " << multicastAddress | 
					
						
							|  |  |  |         << " multicastJoin: " << multicastJoin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     bool addressOK = m_dataAddress.setAddress(address); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!addressOK) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-26 18:51:48 +02:00
										 |  |  |         qWarning("UDPSourceUDPHandler::applyUDPLink: invalid address %s. Set to localhost.", address.toStdString().c_str()); | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |         m_dataAddress = QHostAddress::LocalHost; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |     m_multicast = multicastJoin; | 
					
						
							|  |  |  |     addressOK = m_multicastAddress.setAddress(multicastAddress); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!addressOK) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-26 18:51:48 +02:00
										 |  |  |         qWarning("UDPSourceUDPHandler::applyUDPLink: invalid multicast address %s. disabling multicast.", address.toStdString().c_str()); | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |         m_multicast = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     stop(); | 
					
						
							|  |  |  |     m_dataPort = port; | 
					
						
							|  |  |  |     resetReadIndex(); | 
					
						
							|  |  |  |     start(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::resetReadIndex() | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     m_readFrameIndex = (m_writeFrameIndex + (m_nbUDPFrames/2)) % m_nbUDPFrames; | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  |     m_rwDelta = m_nbUDPFrames/2; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  |     m_readIndex = 0; | 
					
						
							| 
									
										
										
										
											2017-08-16 11:35:47 +02:00
										 |  |  |     m_d = 0.0f; | 
					
						
							| 
									
										
										
										
											2017-08-15 20:23:49 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::resizeBuffer(float sampleRate) | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     int halfNbFrames = std::max((sampleRate / 375.0), (m_minNbUDPFrames / 2.0)); | 
					
						
							| 
									
										
										
										
											2018-09-11 23:22:59 +02:00
										 |  |  |     qDebug("UDPSourceUDPHandler::resizeBuffer: nb_frames: %d", 2*halfNbFrames); | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (2*halfNbFrames > m_nbAllocatedUDPFrames) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         delete[] m_udpBuf; | 
					
						
							|  |  |  |         m_udpBuf = new udpBlk_t[2*halfNbFrames]; | 
					
						
							|  |  |  |         m_nbAllocatedUDPFrames = 2*halfNbFrames; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_nbUDPFrames = 2*halfNbFrames; | 
					
						
							| 
									
										
										
										
											2018-04-19 00:43:29 +02:00
										 |  |  |     m_writeFrameIndex = 0; | 
					
						
							| 
									
										
										
										
											2017-08-17 01:03:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resetReadIndex(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | void UDPSourceUDPHandler::handleMessages() | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Message* message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((message = m_inputMessageQueue.pop()) != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (handleMessage(*message)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             delete message; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  | bool UDPSourceUDPHandler::handleMessage(const Message& cmd) | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  |     if (UDPSourceUDPHandler::MsgUDPAddressAndPort::match(cmd)) | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-11 22:36:16 +02:00
										 |  |  |         UDPSourceUDPHandler::MsgUDPAddressAndPort& notif = (UDPSourceUDPHandler::MsgUDPAddressAndPort&) cmd; | 
					
						
							| 
									
										
										
										
											2020-08-24 19:56:04 +02:00
										 |  |  |         applyUDPLink(notif.getAddress(), notif.getPort(), notif.getMulticastAddress(), notif.getMulticastJoin()); | 
					
						
							| 
									
										
										
										
											2017-08-23 02:17:54 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |