2018-01-28 19:59:03 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2018 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "audionetsink.h"
|
2018-01-28 23:19:59 -05:00
|
|
|
#include "util/rtpsink.h"
|
2018-01-28 19:59:03 -05:00
|
|
|
|
2018-03-07 18:16:24 -05:00
|
|
|
#include <QUdpSocket>
|
|
|
|
|
2018-01-28 19:59:03 -05:00
|
|
|
const int AudioNetSink::m_udpBlockSize = 512;
|
|
|
|
|
2018-03-27 03:04:10 -04:00
|
|
|
AudioNetSink::AudioNetSink(QObject *parent) :
|
2018-01-28 19:59:03 -05:00
|
|
|
m_type(SinkUDP),
|
2018-03-07 18:16:24 -05:00
|
|
|
m_rtpBufferAudio(0),
|
|
|
|
m_bufferIndex(0),
|
|
|
|
m_port(9998)
|
2018-01-28 19:59:03 -05:00
|
|
|
{
|
2018-06-21 13:28:11 -04:00
|
|
|
memset(m_data, 0, 65536);
|
2018-03-07 18:16:24 -05:00
|
|
|
m_udpSocket = new QUdpSocket(parent);
|
2018-03-27 03:04:10 -04:00
|
|
|
}
|
2018-03-27 02:13:06 -04:00
|
|
|
|
2018-03-27 03:04:10 -04:00
|
|
|
AudioNetSink::AudioNetSink(QObject *parent, int sampleRate, bool stereo) :
|
|
|
|
m_type(SinkUDP),
|
|
|
|
m_rtpBufferAudio(0),
|
|
|
|
m_bufferIndex(0),
|
|
|
|
m_port(9998)
|
|
|
|
{
|
2018-06-21 13:28:11 -04:00
|
|
|
memset(m_data, 0, 65536);
|
2018-03-27 03:04:10 -04:00
|
|
|
m_udpSocket = new QUdpSocket(parent);
|
|
|
|
m_rtpBufferAudio = new RTPSink(m_udpSocket, sampleRate, stereo);
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioNetSink::~AudioNetSink()
|
|
|
|
{
|
2018-01-30 18:40:54 -05:00
|
|
|
if (m_rtpBufferAudio) {
|
|
|
|
delete m_rtpBufferAudio;
|
|
|
|
}
|
2018-03-07 18:16:24 -05:00
|
|
|
|
|
|
|
m_udpSocket->deleteLater(); // this thread is not the owner thread (was moved)
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:16:24 -05:00
|
|
|
bool AudioNetSink::isRTPCapable() const
|
|
|
|
{
|
2018-03-27 02:13:06 -04:00
|
|
|
return m_rtpBufferAudio && m_rtpBufferAudio->isValid();
|
2018-03-07 18:16:24 -05:00
|
|
|
}
|
2018-02-20 13:18:23 -05:00
|
|
|
|
2018-01-28 19:59:03 -05:00
|
|
|
bool AudioNetSink::selectType(SinkType type)
|
|
|
|
{
|
|
|
|
if (type == SinkUDP)
|
|
|
|
{
|
|
|
|
m_type = SinkUDP;
|
|
|
|
}
|
2018-06-21 13:28:11 -04:00
|
|
|
else // this is SinkRTP
|
2018-01-28 19:59:03 -05:00
|
|
|
{
|
|
|
|
m_type = SinkRTP;
|
|
|
|
}
|
2018-06-21 13:28:11 -04:00
|
|
|
|
|
|
|
return true;
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioNetSink::setDestination(const QString& address, uint16_t port)
|
|
|
|
{
|
2018-03-07 18:16:24 -05:00
|
|
|
m_address.setAddress(const_cast<QString&>(address));
|
|
|
|
m_port = port;
|
|
|
|
|
2018-01-30 18:40:54 -05:00
|
|
|
if (m_rtpBufferAudio) {
|
|
|
|
m_rtpBufferAudio->setDestination(address, port);
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioNetSink::addDestination(const QString& address, uint16_t port)
|
|
|
|
{
|
2018-01-30 18:40:54 -05:00
|
|
|
if (m_rtpBufferAudio) {
|
|
|
|
m_rtpBufferAudio->addDestination(address, port);
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioNetSink::deleteDestination(const QString& address, uint16_t port)
|
|
|
|
{
|
2018-01-30 18:40:54 -05:00
|
|
|
if (m_rtpBufferAudio) {
|
|
|
|
m_rtpBufferAudio->deleteDestination(address, port);
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 18:09:52 -04:00
|
|
|
void AudioNetSink::setParameters(bool stereo, int sampleRate)
|
|
|
|
{
|
2018-03-27 02:13:06 -04:00
|
|
|
if (m_rtpBufferAudio) {
|
|
|
|
m_rtpBufferAudio->setPayloadInformation(stereo ? RTPSink::PayloadL16Stereo : RTPSink::PayloadL16Mono, sampleRate);
|
|
|
|
}
|
2018-03-26 18:09:52 -04:00
|
|
|
}
|
|
|
|
|
2018-01-28 19:59:03 -05:00
|
|
|
void AudioNetSink::write(qint16 sample)
|
|
|
|
{
|
2018-03-07 18:16:24 -05:00
|
|
|
if (m_type == SinkUDP)
|
|
|
|
{
|
|
|
|
if (m_bufferIndex >= m_udpBlockSize)
|
|
|
|
{
|
|
|
|
m_udpSocket->writeDatagram((const char*)m_data, (qint64 ) m_udpBlockSize, m_address, m_port);
|
|
|
|
m_bufferIndex = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qint16 *p = (qint16*) &m_data[m_bufferIndex];
|
|
|
|
*p = sample;
|
|
|
|
m_bufferIndex += sizeof(qint16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_type == SinkRTP)
|
|
|
|
{
|
2018-01-30 18:40:54 -05:00
|
|
|
m_rtpBufferAudio->write((uint8_t *) &sample);
|
2018-01-28 19:59:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:58:17 -04:00
|
|
|
void AudioNetSink::write(qint16 lSample, qint16 rSample)
|
|
|
|
{
|
|
|
|
if (m_type == SinkUDP)
|
|
|
|
{
|
|
|
|
if (m_bufferIndex >= m_udpBlockSize)
|
|
|
|
{
|
|
|
|
m_udpSocket->writeDatagram((const char*)m_data, (qint64 ) m_udpBlockSize, m_address, m_port);
|
|
|
|
m_bufferIndex = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qint16 *p = (qint16*) &m_data[m_bufferIndex];
|
|
|
|
*p = lSample;
|
|
|
|
m_bufferIndex += sizeof(qint16);
|
|
|
|
p = (qint16*) &m_data[m_bufferIndex];
|
|
|
|
*p = rSample;
|
|
|
|
m_bufferIndex += sizeof(qint16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_type == SinkRTP)
|
|
|
|
{
|
2018-03-29 19:19:02 -04:00
|
|
|
m_rtpBufferAudio->write((uint8_t *) &lSample, (uint8_t *) &rSample);
|
2018-03-26 16:58:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 02:59:17 -05:00
|
|
|
void AudioNetSink::write(AudioSample* samples, uint32_t numSamples)
|
|
|
|
{
|
|
|
|
if (m_type == SinkUDP)
|
|
|
|
{
|
|
|
|
int samplesIndex = 0;
|
|
|
|
|
|
|
|
if (m_bufferIndex + numSamples*sizeof(AudioSample) >= m_udpBlockSize) // fill remainder of buffer and send it
|
|
|
|
{
|
|
|
|
memcpy(&m_data[m_bufferIndex], &samples[samplesIndex], m_udpBlockSize - m_bufferIndex); // fill remainder of buffer
|
|
|
|
m_udpSocket->writeDatagram((const char*)m_data, (qint64 ) m_udpBlockSize, m_address, m_port);
|
|
|
|
m_bufferIndex = 0;
|
|
|
|
samplesIndex += (m_udpBlockSize - m_bufferIndex) / sizeof(AudioSample);
|
|
|
|
numSamples -= (m_udpBlockSize - m_bufferIndex) / sizeof(AudioSample);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (numSamples > m_udpBlockSize/sizeof(AudioSample)) // send directly from input without buffering
|
|
|
|
{
|
|
|
|
m_udpSocket->writeDatagram((const char*)&samples[samplesIndex], (qint64 ) m_udpBlockSize, m_address, m_port);
|
|
|
|
samplesIndex += m_udpBlockSize/sizeof(AudioSample);
|
|
|
|
numSamples -= m_udpBlockSize/sizeof(AudioSample);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&m_data[m_bufferIndex], &samples[samplesIndex], numSamples*sizeof(AudioSample));
|
|
|
|
}
|
|
|
|
else if (m_type == SinkRTP)
|
|
|
|
{
|
2018-03-09 23:49:18 -05:00
|
|
|
m_rtpBufferAudio->write((uint8_t *) samples, numSamples*2); // 2 x 16 bit sample
|
2018-03-08 02:59:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:23:47 -05:00
|
|
|
void AudioNetSink::moveToThread(QThread *thread)
|
|
|
|
{
|
2018-03-07 18:16:24 -05:00
|
|
|
m_udpSocket->moveToThread(thread);
|
2018-03-05 20:23:47 -05:00
|
|
|
}
|
2018-01-28 19:59:03 -05:00
|
|
|
|