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