2014-05-18 11:52:39 -04: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 //
2019-04-11 08:32:15 -04:00
// (at your option) any later version. //
2014-05-18 11:52:39 -04: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 <string.h>
# include <QAudioFormat>
# include <QAudioDeviceInfo>
# include <QAudioOutput>
2018-03-25 05:50:34 -04:00
# include "audiooutput.h"
# include "audiofifo.h"
# include "audionetsink.h"
2014-05-18 11:52:39 -04:00
AudioOutput : : AudioOutput ( ) :
2018-01-06 00:12:30 -05:00
m_mutex ( QMutex : : Recursive ) ,
2016-05-18 05:32:19 -04:00
m_audioOutput ( 0 ) ,
2018-03-25 05:50:34 -04:00
m_audioNetSink ( 0 ) ,
m_copyAudioToUdp ( false ) ,
2018-03-26 15:10:09 -04:00
m_udpChannelMode ( UDPChannelLeft ) ,
2019-02-13 01:53:38 -05:00
m_udpChannelCodec ( UDPCodecL16 ) ,
2016-05-18 05:32:19 -04:00
m_audioUsageCount ( 0 ) ,
2016-10-08 00:54:49 -04:00
m_onExit ( false ) ,
2014-05-18 11:52:39 -04:00
m_audioFifos ( )
{
}
AudioOutput : : ~ AudioOutput ( )
{
2018-03-24 18:50:28 -04:00
// stop();
//
// QMutexLocker mutexLocker(&m_mutex);
//
// for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
// {
// delete *it;
// }
//
// m_audioFifos.clear();
2014-05-18 11:52:39 -04:00
}
bool AudioOutput : : start ( int device , int rate )
{
2018-03-24 18:50:28 -04:00
// if (m_audioUsageCount == 0)
// {
2018-01-06 00:12:30 -05:00
QMutexLocker mutexLocker ( & m_mutex ) ;
2016-05-18 05:32:19 -04:00
QAudioDeviceInfo devInfo ;
if ( device < 0 )
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
2018-03-26 12:41:09 -04:00
qWarning ( " AudioOutput::start: using system default device %s " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
}
else
{
QList < QAudioDeviceInfo > devicesInfo = QAudioDeviceInfo : : availableDevices ( QAudio : : AudioOutput ) ;
if ( device < devicesInfo . size ( ) )
{
devInfo = devicesInfo [ device ] ;
2018-03-26 12:41:09 -04:00
qWarning ( " AudioOutput::start: using audio device #%d: %s " , device , qPrintable ( devInfo . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
}
else
{
devInfo = QAudioDeviceInfo : : defaultOutputDevice ( ) ;
2018-03-26 12:41:09 -04:00
qWarning ( " AudioOutput::start: audio device #%d does not exist. Using system default device %s " , device , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
}
}
//QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice());
m_audioFormat . setSampleRate ( rate ) ;
m_audioFormat . setChannelCount ( 2 ) ;
m_audioFormat . setSampleSize ( 16 ) ;
m_audioFormat . setCodec ( " audio/pcm " ) ;
m_audioFormat . setByteOrder ( QAudioFormat : : LittleEndian ) ;
m_audioFormat . setSampleType ( QAudioFormat : : SignedInt ) ;
if ( ! devInfo . isFormatSupported ( m_audioFormat ) )
{
m_audioFormat = devInfo . nearestFormat ( m_audioFormat ) ;
2018-03-26 12:41:09 -04:00
std : : ostringstream os ;
os < < " sampleRate: " < < m_audioFormat . sampleRate ( )
< < " channelCount: " < < m_audioFormat . channelCount ( )
< < " sampleSize: " < < m_audioFormat . sampleSize ( )
< < " codec: " < < m_audioFormat . codec ( ) . toStdString ( )
< < " byteOrder: " < < ( m_audioFormat . byteOrder ( ) = = QAudioFormat : : BigEndian ? " BE " : " LE " )
< < " sampleType: " < < ( int ) m_audioFormat . sampleType ( ) ;
qWarning ( " AudioOutput::start: format %d Hz 2xS16LE audio/pcm not supported. Using: %s " , rate , os . str ( ) . c_str ( ) ) ;
2016-05-18 05:32:19 -04:00
}
2017-08-05 20:26:34 -04:00
else
{
qInfo ( " AudioOutput::start: audio format OK " ) ;
}
2016-05-18 05:32:19 -04:00
if ( m_audioFormat . sampleSize ( ) ! = 16 )
{
2018-10-06 16:34:48 -04:00
qWarning ( " AudioOutput::start: Audio device '%s' failed " , qPrintable ( devInfo . defaultOutputDevice ( ) . deviceName ( ) ) ) ;
2016-05-18 05:32:19 -04:00
return false ;
}
m_audioOutput = new QAudioOutput ( devInfo , m_audioFormat ) ;
2018-03-27 03:04:10 -04:00
m_audioNetSink = new AudioNetSink ( 0 , m_audioFormat . sampleRate ( ) , false ) ;
2016-05-18 05:32:19 -04:00
QIODevice : : open ( QIODevice : : ReadOnly ) ;
m_audioOutput - > start ( this ) ;
if ( m_audioOutput - > state ( ) ! = QAudio : : ActiveState )
{
qWarning ( " AudioOutput::start: cannot start " ) ;
}
2018-03-24 18:50:28 -04:00
// }
//
// m_audioUsageCount++;
2015-08-23 18:51:27 -04:00
2014-05-18 11:52:39 -04:00
return true ;
}
void AudioOutput : : stop ( )
{
2016-10-07 23:59:59 -04:00
qDebug ( " AudioOutput::stop " ) ;
2018-03-24 18:50:28 -04:00
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioOutput - > stop ( ) ;
QIODevice : : close ( ) ;
2018-03-25 05:50:34 -04:00
delete m_audioNetSink ;
m_audioNetSink = 0 ;
2018-03-24 18:50:28 -04:00
delete m_audioOutput ;
// if (m_audioUsageCount > 0)
// {
// m_audioUsageCount--;
//
// if (m_audioUsageCount == 0)
// {
// QMutexLocker mutexLocker(&m_mutex);
// QIODevice::close();
//
// if (!m_onExit) {
// delete m_audioOutput;
// }
// }
// }
2014-05-18 11:52:39 -04:00
}
void AudioOutput : : addFifo ( AudioFifo * audioFifo )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . push_back ( audioFifo ) ;
}
void AudioOutput : : removeFifo ( AudioFifo * audioFifo )
{
QMutexLocker mutexLocker ( & m_mutex ) ;
m_audioFifos . remove ( audioFifo ) ;
}
2015-08-19 21:38:31 -04:00
/*
2014-05-18 11:52:39 -04:00
bool AudioOutput : : open ( OpenMode mode )
{
Q_UNUSED ( mode ) ;
return false ;
2015-08-19 21:38:31 -04:00
} */
2014-05-18 11:52:39 -04:00
2018-03-25 05:50:34 -04:00
void AudioOutput : : setUdpDestination ( const QString & address , uint16_t port )
{
if ( m_audioNetSink ) {
m_audioNetSink - > setDestination ( address , port ) ;
}
}
void AudioOutput : : setUdpCopyToUDP ( bool copyToUDP )
{
m_copyAudioToUdp = copyToUDP ;
}
2018-03-26 15:10:09 -04:00
void AudioOutput : : setUdpUseRTP ( bool useRTP )
2018-03-25 05:50:34 -04:00
{
if ( m_audioNetSink ) {
2018-03-26 15:10:09 -04:00
m_audioNetSink - > selectType ( useRTP ? AudioNetSink : : SinkRTP : AudioNetSink : : SinkUDP ) ;
2018-03-25 05:50:34 -04:00
}
}
2018-03-26 15:10:09 -04:00
void AudioOutput : : setUdpChannelMode ( UDPChannelMode udpChannelMode )
2018-03-26 18:09:52 -04:00
{
m_udpChannelMode = udpChannelMode ;
}
2019-02-13 01:53:38 -05:00
void AudioOutput : : setUdpChannelFormat ( UDPChannelCodec udpChannelCodec , bool stereo , int sampleRate )
2018-03-25 05:50:34 -04:00
{
2019-02-13 01:53:38 -05:00
m_udpChannelCodec = udpChannelCodec ;
2018-03-25 05:50:34 -04:00
if ( m_audioNetSink ) {
2019-02-13 01:53:38 -05:00
m_audioNetSink - > setParameters ( ( AudioNetSink : : Codec ) m_udpChannelCodec , stereo , sampleRate ) ;
2018-03-25 05:50:34 -04:00
}
2018-03-26 15:10:09 -04:00
}
2018-03-25 05:50:34 -04:00
2019-02-14 11:21:14 -05:00
void AudioOutput : : setUdpDecimation ( uint32_t decimation )
{
if ( m_audioNetSink ) {
m_audioNetSink - > setDecimation ( decimation ) ;
}
}
2014-05-18 11:52:39 -04:00
qint64 AudioOutput : : readData ( char * data , qint64 maxLen )
{
2016-06-29 04:00:38 -04:00
//qDebug("AudioOutput::readData: %lld", maxLen);
2016-12-25 14:04:19 -05:00
// Study this mutex on OSX, for now deadlocks possible
// Removed as it may indeed cause lockups and is in fact useless.
//#ifndef __APPLE__
// QMutexLocker mutexLocker(&m_mutex);
//#endif
2014-05-18 11:52:39 -04:00
2018-03-27 08:54:48 -04:00
unsigned int samplesPerBuffer = maxLen / 4 ;
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
if ( samplesPerBuffer = = 0 )
2015-08-19 21:38:31 -04:00
{
2014-11-21 10:41:58 -05:00
return 0 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
2018-03-27 08:54:48 -04:00
if ( m_mixBuffer . size ( ) < samplesPerBuffer * 2 )
2015-08-19 21:38:31 -04:00
{
2018-03-27 08:54:48 -04:00
m_mixBuffer . resize ( samplesPerBuffer * 2 ) ; // allocate 2 qint32 per sample (stereo)
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
if ( m_mixBuffer . size ( ) ! = samplesPerBuffer * 2 )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
return 0 ;
2015-08-19 21:38:31 -04:00
}
2014-05-18 11:52:39 -04:00
}
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
memset ( & m_mixBuffer [ 0 ] , 0x00 , 2 * samplesPerBuffer * sizeof ( m_mixBuffer [ 0 ] ) ) ; // start with silence
2014-05-18 11:52:39 -04:00
// sum up a block from all fifos
2015-08-19 21:38:31 -04:00
2018-03-23 22:50:09 -04:00
for ( std : : list < AudioFifo * > : : iterator it = m_audioFifos . begin ( ) ; it ! = m_audioFifos . end ( ) ; + + it )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
// use outputBuffer as temp - yes, one memcpy could be saved
2018-09-12 09:30:53 -04:00
unsigned int samples = ( * it ) - > read ( ( quint8 * ) data , samplesPerBuffer ) ;
2015-08-19 21:38:31 -04:00
const qint16 * src = ( const qint16 * ) data ;
2014-05-18 11:52:39 -04:00
std : : vector < qint32 > : : iterator dst = m_mixBuffer . begin ( ) ;
2015-08-19 21:38:31 -04:00
2016-07-31 00:05:32 -04:00
// if (samples != framesPerBuffer)
// {
// qDebug("AudioOutput::readData: read %d samples vs %d requested", samples, framesPerBuffer);
// }
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
for ( unsigned int i = 0 ; i < samples ; i + + )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
* dst + = * src ;
+ + src ;
+ + dst ;
* dst + = * src ;
+ + src ;
+ + dst ;
}
2016-06-29 04:00:38 -04:00
}
2014-05-18 11:52:39 -04:00
// convert to int16
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//std::vector<qint32>::const_iterator src = m_mixBuffer.begin(); // Valgrind optim
2015-08-19 21:38:31 -04:00
qint16 * dst = ( qint16 * ) data ;
2018-03-26 16:58:17 -04:00
qint32 sl , sr ;
2015-08-19 21:38:31 -04:00
2018-03-27 08:54:48 -04:00
for ( unsigned int i = 0 ; i < samplesPerBuffer ; i + + )
2015-08-19 21:38:31 -04:00
{
2014-05-18 11:52:39 -04:00
// left channel
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//s = *src++; // Valgrind optim
2018-03-26 16:58:17 -04:00
sl = m_mixBuffer [ 2 * i ] ;
2015-08-19 21:38:31 -04:00
2018-03-26 16:58:17 -04:00
if ( sl < - 32768 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sl = - 32768 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
else if ( sl > 32767 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sl = 32767 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
* dst + + = sl ;
2015-08-19 21:38:31 -04:00
2014-05-18 11:52:39 -04:00
// right channel
2015-08-19 21:38:31 -04:00
2015-08-28 21:09:00 -04:00
//s = *src++; // Valgrind optim
2018-03-26 16:58:17 -04:00
sr = m_mixBuffer [ 2 * i + 1 ] ;
2015-08-19 21:38:31 -04:00
2018-03-26 16:58:17 -04:00
if ( sr < - 32768 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sr = - 32768 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
else if ( sr > 32767 )
2015-08-19 21:38:31 -04:00
{
2018-03-26 16:58:17 -04:00
sr = 32767 ;
2015-08-19 21:38:31 -04:00
}
2018-03-26 16:58:17 -04:00
* dst + + = sr ;
if ( ( m_copyAudioToUdp ) & & ( m_audioNetSink ) )
{
switch ( m_udpChannelMode )
{
case UDPChannelStereo :
m_audioNetSink - > write ( sl , sr ) ;
break ;
case UDPChannelMixed :
m_audioNetSink - > write ( ( sl + sr ) / 2 ) ;
break ;
case UDPChannelRight :
m_audioNetSink - > write ( sr ) ;
break ;
case UDPChannelLeft :
default :
m_audioNetSink - > write ( sl ) ;
break ;
}
}
2014-05-18 11:52:39 -04:00
}
2018-03-27 08:54:48 -04:00
return samplesPerBuffer * 4 ;
2014-05-18 11:52:39 -04:00
}
qint64 AudioOutput : : writeData ( const char * data , qint64 len )
{
Q_UNUSED ( data ) ;
Q_UNUSED ( len ) ;
return 0 ;
}