2013-10-04 08:13:09 -04:00
# include <QToolButton>
2012-02-25 07:18:40 -05:00
# include <QPropertyAnimation>
# include <QIcon>
2014-01-01 19:56:46 -05:00
# include "AudioChatWidgetHolder.h"
# include <gui/audiodevicehelper.h>
2012-02-26 11:10:27 -05:00
# include "interface/rsvoip.h"
2012-10-21 17:38:55 -04:00
# include "gui/SoundManager.h"
2013-10-20 12:59:29 -04:00
# include "util/HandleRichText.h"
# include "gui/common/StatusDefs.h"
2014-01-01 19:56:46 -05:00
# include "gui/chat/ChatWidget.h"
2013-10-20 12:59:29 -04:00
# include <retroshare/rsstatus.h>
2012-02-25 07:18:40 -05:00
2013-10-03 18:00:16 -04:00
# define CALL_START ": / images / call-start-22.png"
2013-10-20 12:59:29 -04:00
# define CALL_STOP ": / images / call-stop-22.png"
# define CALL_HOLD ": / images / call-hold-22.png"
2013-10-03 18:00:16 -04:00
2014-01-01 19:56:46 -05:00
AudioChatWidgetHolder : : AudioChatWidgetHolder ( ChatWidget * chatWidget )
: QObject ( ) , ChatWidgetHolder ( chatWidget )
2012-02-25 07:18:40 -05:00
{
2013-10-04 08:13:09 -04:00
audioListenToggleButton = new QToolButton ;
2012-02-25 07:18:40 -05:00
audioListenToggleButton - > setMinimumSize ( QSize ( 28 , 28 ) ) ;
audioListenToggleButton - > setMaximumSize ( QSize ( 28 , 28 ) ) ;
2012-10-21 17:38:55 -04:00
audioListenToggleButton - > setText ( QString ( ) ) ;
2012-10-17 08:08:30 -04:00
audioListenToggleButton - > setToolTip ( tr ( " Mute yourself " ) ) ;
2012-02-25 07:18:40 -05:00
2014-01-01 19:56:46 -05:00
std : : cerr < < " ****** VOIPLugin: Creating new AudioChatWidgetHolder !! " < < std : : endl ;
2012-02-25 07:18:40 -05:00
QIcon icon ;
2013-10-20 12:59:29 -04:00
icon . addPixmap ( QPixmap ( " :/images/audio-volume-muted-22.png " ) ) ;
icon . addPixmap ( QPixmap ( " :/images/audio-volume-medium-22.png " ) , QIcon : : Normal , QIcon : : On ) ;
icon . addPixmap ( QPixmap ( " :/images/audio-volume-medium-22.png " ) , QIcon : : Disabled , QIcon : : On ) ;
icon . addPixmap ( QPixmap ( " :/images/audio-volume-medium-22.png " ) , QIcon : : Active , QIcon : : On ) ;
icon . addPixmap ( QPixmap ( " :/images/audio-volume-medium-22.png " ) , QIcon : : Selected , QIcon : : On ) ;
2012-02-25 07:18:40 -05:00
audioListenToggleButton - > setIcon ( icon ) ;
audioListenToggleButton - > setIconSize ( QSize ( 22 , 22 ) ) ;
2013-10-04 08:13:09 -04:00
audioListenToggleButton - > setAutoRaise ( true ) ;
audioListenToggleButton - > setCheckable ( true ) ;
2012-02-25 07:18:40 -05:00
2013-10-04 08:13:09 -04:00
audioMuteCaptureToggleButton = new QToolButton ;
2012-02-25 07:18:40 -05:00
audioMuteCaptureToggleButton - > setMinimumSize ( QSize ( 28 , 28 ) ) ;
audioMuteCaptureToggleButton - > setMaximumSize ( QSize ( 28 , 28 ) ) ;
2012-10-21 17:38:55 -04:00
audioMuteCaptureToggleButton - > setText ( QString ( ) ) ;
2013-10-03 18:00:16 -04:00
audioMuteCaptureToggleButton - > setToolTip ( tr ( " Start Call " ) ) ;
2012-02-25 07:18:40 -05:00
QIcon icon2 ;
2013-10-04 08:13:09 -04:00
icon2 . addPixmap ( QPixmap ( " :/images/call-start-22.png " ) ) ;
2013-10-20 12:59:29 -04:00
icon2 . addPixmap ( QPixmap ( " :/images/call-hold-22.png " ) , QIcon : : Normal , QIcon : : On ) ;
icon2 . addPixmap ( QPixmap ( " :/images/call-hold-22.png " ) , QIcon : : Disabled , QIcon : : On ) ;
icon2 . addPixmap ( QPixmap ( " :/images/call-hold-22.png " ) , QIcon : : Active , QIcon : : On ) ;
icon2 . addPixmap ( QPixmap ( " :/images/call-hold-22.png " ) , QIcon : : Selected , QIcon : : On ) ;
2012-02-25 07:18:40 -05:00
audioMuteCaptureToggleButton - > setIcon ( icon2 ) ;
audioMuteCaptureToggleButton - > setIconSize ( QSize ( 22 , 22 ) ) ;
2013-10-04 08:13:09 -04:00
audioMuteCaptureToggleButton - > setAutoRaise ( true ) ;
audioMuteCaptureToggleButton - > setCheckable ( true ) ;
2013-10-20 12:59:29 -04:00
hangupButton = new QToolButton ;
hangupButton - > setIcon ( QIcon ( " :/images/call-stop-22.png " ) ) ;
hangupButton - > setIconSize ( QSize ( 22 , 22 ) ) ;
hangupButton - > setMinimumSize ( QSize ( 28 , 28 ) ) ;
hangupButton - > setMaximumSize ( QSize ( 28 , 28 ) ) ;
hangupButton - > setCheckable ( false ) ;
hangupButton - > setAutoRaise ( true ) ;
hangupButton - > setText ( QString ( ) ) ;
hangupButton - > setToolTip ( tr ( " Hangup Call " ) ) ;
2012-02-25 07:18:40 -05:00
connect ( audioListenToggleButton , SIGNAL ( clicked ( ) ) , this , SLOT ( toggleAudioListen ( ) ) ) ;
connect ( audioMuteCaptureToggleButton , SIGNAL ( clicked ( ) ) , this , SLOT ( toggleAudioMuteCapture ( ) ) ) ;
2013-10-20 12:59:29 -04:00
connect ( hangupButton , SIGNAL ( clicked ( ) ) , this , SLOT ( hangupCall ( ) ) ) ;
2012-02-25 07:18:40 -05:00
2014-01-01 19:56:46 -05:00
mChatWidget - > addChatBarWidget ( audioListenToggleButton ) ;
mChatWidget - > addChatBarWidget ( audioMuteCaptureToggleButton ) ;
mChatWidget - > addChatBarWidget ( hangupButton ) ;
2012-02-25 07:18:40 -05:00
outputProcessor = NULL ;
outputDevice = NULL ;
inputProcessor = NULL ;
inputDevice = NULL ;
}
2014-01-01 19:56:46 -05:00
AudioChatWidgetHolder : : ~ AudioChatWidgetHolder ( )
2014-01-01 17:25:49 -05:00
{
2014-01-01 19:56:46 -05:00
if ( inputDevice ! = NULL )
inputDevice - > stop ( ) ;
2014-01-01 17:25:49 -05:00
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : toggleAudioListen ( )
2012-02-25 07:18:40 -05:00
{
std : : cerr < < " ******** VOIPLugin: Toggling audio listen! " < < std : : endl ;
2012-10-21 17:38:55 -04:00
if ( audioListenToggleButton - > isChecked ( ) ) {
2013-10-20 12:59:29 -04:00
audioListenToggleButton - > setToolTip ( tr ( " Mute yourself " ) ) ;
2012-02-25 07:18:40 -05:00
} else {
2013-10-20 12:59:29 -04:00
audioListenToggleButton - > setToolTip ( tr ( " Unmute yourself " ) ) ;
2012-02-26 11:10:27 -05:00
//audioListenToggleButton->setChecked(false);
2012-02-25 07:18:40 -05:00
/*if (outputDevice) {
outputDevice - > stop ( ) ;
} */
}
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : hangupCall ( )
2013-10-20 12:59:29 -04:00
{
std : : cerr < < " ******** VOIPLugin: Hangup call! " < < std : : endl ;
disconnect ( inputProcessor , SIGNAL ( networkPacketReady ( ) ) , this , SLOT ( sendAudioData ( ) ) ) ;
if ( inputDevice ) {
inputDevice - > stop ( ) ;
}
if ( outputDevice ) {
outputDevice - > stop ( ) ;
}
audioListenToggleButton - > setChecked ( false ) ;
audioMuteCaptureToggleButton - > setChecked ( false ) ;
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : toggleAudioMuteCapture ( )
2012-02-25 07:18:40 -05:00
{
std : : cerr < < " ******** VOIPLugin: Toggling audio mute capture! " < < std : : endl ;
if ( audioMuteCaptureToggleButton - > isChecked ( ) ) {
//activate audio output
audioListenToggleButton - > setChecked ( true ) ;
2013-10-20 12:59:29 -04:00
audioMuteCaptureToggleButton - > setToolTip ( tr ( " Hold Call " ) ) ;
2012-02-25 07:18:40 -05:00
//activate audio input
if ( ! inputProcessor ) {
inputProcessor = new QtSpeex : : SpeexInputProcessor ( ) ;
if ( outputProcessor ) {
connect ( outputProcessor , SIGNAL ( playingFrame ( QByteArray * ) ) , inputProcessor , SLOT ( addEchoFrame ( QByteArray * ) ) ) ;
}
inputProcessor - > open ( QIODevice : : WriteOnly | QIODevice : : Unbuffered ) ;
}
if ( ! inputDevice ) {
inputDevice = AudioDeviceHelper : : getPreferedInputDevice ( ) ;
}
connect ( inputProcessor , SIGNAL ( networkPacketReady ( ) ) , this , SLOT ( sendAudioData ( ) ) ) ;
inputDevice - > start ( inputProcessor ) ;
2013-10-20 12:59:29 -04:00
2014-01-01 19:56:46 -05:00
if ( mChatWidget ) {
mChatWidget - > addChatMsg ( true , tr ( " VoIP Status " ) , QDateTime : : currentDateTime ( ) , QDateTime : : currentDateTime ( ) , tr ( " Outgoing Call is started... " ) , ChatWidget : : MSGTYPE_SYSTEM ) ;
2013-10-20 12:59:29 -04:00
}
2012-02-25 07:18:40 -05:00
} else {
disconnect ( inputProcessor , SIGNAL ( networkPacketReady ( ) ) , this , SLOT ( sendAudioData ( ) ) ) ;
if ( inputDevice ) {
inputDevice - > stop ( ) ;
}
2013-10-20 12:59:29 -04:00
audioMuteCaptureToggleButton - > setToolTip ( tr ( " Resume Call " ) ) ;
2012-02-25 07:18:40 -05:00
}
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : addAudioData ( const QString name , QByteArray * array )
2012-02-25 07:18:40 -05:00
{
2013-10-03 18:00:16 -04:00
if ( ! audioMuteCaptureToggleButton - > isChecked ( ) ) {
2012-02-25 07:18:40 -05:00
//launch an animation. Don't launch it if already animating
2013-10-03 18:00:16 -04:00
if ( ! audioMuteCaptureToggleButton - > graphicsEffect ( ) | |
( audioMuteCaptureToggleButton - > graphicsEffect ( ) - > inherits ( " QGraphicsOpacityEffect " ) & &
( ( QGraphicsOpacityEffect * ) audioMuteCaptureToggleButton - > graphicsEffect ( ) ) - > opacity ( ) = = 1 )
2012-02-25 07:18:40 -05:00
) {
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect ( audioListenToggleButton ) ;
2013-10-03 18:00:16 -04:00
audioMuteCaptureToggleButton - > setGraphicsEffect ( effect ) ;
2012-02-25 07:18:40 -05:00
QPropertyAnimation * anim = new QPropertyAnimation ( effect , " opacity " ) ;
anim - > setStartValue ( 1 ) ;
anim - > setKeyValueAt ( 0.5 , 0 ) ;
anim - > setEndValue ( 1 ) ;
anim - > setDuration ( 400 ) ;
anim - > start ( ) ;
}
2012-10-21 17:38:55 -04:00
// soundManager->play(VOIP_SOUND_INCOMING_CALL);
2013-10-03 18:00:16 -04:00
audioMuteCaptureToggleButton - > setToolTip ( tr ( " Answer " ) ) ;
2012-02-25 07:18:40 -05:00
//TODO make a toaster and a sound for the incoming call
return ;
}
if ( ! outputDevice ) {
outputDevice = AudioDeviceHelper : : getDefaultOutputDevice ( ) ;
}
if ( ! outputProcessor ) {
//start output audio device
outputProcessor = new QtSpeex : : SpeexOutputProcessor ( ) ;
if ( inputProcessor ) {
connect ( outputProcessor , SIGNAL ( playingFrame ( QByteArray * ) ) , inputProcessor , SLOT ( addEchoFrame ( QByteArray * ) ) ) ;
}
outputProcessor - > open ( QIODevice : : ReadOnly | QIODevice : : Unbuffered ) ;
outputDevice - > start ( outputProcessor ) ;
}
if ( outputDevice & & outputDevice - > error ( ) ! = QAudio : : NoError ) {
std : : cerr < < " Restarting output device. Error before reset " < < outputDevice - > error ( ) < < " buffer size : " < < outputDevice - > bufferSize ( ) < < std : : endl ;
outputDevice - > stop ( ) ;
outputDevice - > reset ( ) ;
if ( outputDevice - > error ( ) = = QAudio : : UnderrunError )
outputDevice - > setBufferSize ( 20 ) ;
outputDevice - > start ( outputProcessor ) ;
}
outputProcessor - > putNetworkPacket ( name , * array ) ;
//check the input device for errors
if ( inputDevice & & inputDevice - > error ( ) ! = QAudio : : NoError ) {
std : : cerr < < " Restarting input device. Error before reset " < < inputDevice - > error ( ) < < std : : endl ;
inputDevice - > stop ( ) ;
inputDevice - > reset ( ) ;
inputDevice - > start ( inputProcessor ) ;
}
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : sendAudioData ( )
{
2012-02-25 07:18:40 -05:00
while ( inputProcessor & & inputProcessor - > hasPendingPackets ( ) ) {
QByteArray qbarray = inputProcessor - > getNetworkPacket ( ) ;
2012-02-26 11:10:27 -05:00
RsVoipDataChunk chunk ;
chunk . size = qbarray . size ( ) ;
2012-02-26 15:31:16 -05:00
chunk . data = ( void * ) qbarray . constData ( ) ;
2014-01-01 19:56:46 -05:00
rsVoip - > sendVoipData ( mChatWidget - > getPeerId ( ) , chunk ) ;
2012-02-25 07:18:40 -05:00
}
}
2014-01-01 19:56:46 -05:00
void AudioChatWidgetHolder : : updateStatus ( int status )
2012-02-25 07:18:40 -05:00
{
audioListenToggleButton - > setEnabled ( true ) ;
audioMuteCaptureToggleButton - > setEnabled ( true ) ;
2014-01-01 19:56:46 -05:00
hangupButton - > setEnabled ( true ) ;
2013-10-20 12:59:29 -04:00
switch ( status ) {
2014-01-01 19:56:46 -05:00
case RS_STATUS_OFFLINE :
audioListenToggleButton - > setEnabled ( false ) ;
audioMuteCaptureToggleButton - > setEnabled ( false ) ;
hangupButton - > setEnabled ( false ) ;
2013-10-20 12:59:29 -04:00
break ;
2014-01-01 19:56:46 -05:00
}
2012-02-25 07:18:40 -05:00
}