defuze.me  Client
audiooutputdevicethread.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "audioio.hpp"
00012 #include "audiooutputdevicethread.hpp"
00013 #include <QDebug>
00014 
00015 using namespace Audio;
00016 
00017 AudioOutputDeviceThread::AudioOutputDeviceThread(QAudioDeviceInfo device) : input(0)
00018 {
00019     _audioDevice = device;
00020     audioOutput = 0;
00021     _isStarted = false;
00022 }
00023 
00024 AudioOutputDeviceThread::~AudioOutputDeviceThread()
00025 {
00026     quit();
00027     wait();
00028     if (audioOutput)
00029         audioOutput->stop();
00030 }
00031 
00032 bool    AudioOutputDeviceThread::isStarted()
00033 {
00034     return _isStarted;
00035 }
00036 
00037 void    AudioOutputDeviceThread::changeFormat(QAudioFormat format)
00038 {
00039     qDebug() << "Set format";
00040     _format = format;
00041     audioOutput = new QAudioOutput(_audioDevice, _format);
00042     connect(audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
00043     startThread();
00044 }
00045 
00046 void    AudioOutputDeviceThread::stateChanged(QAudio::State state)
00047 {
00048     qDebug() << "state changed: " << state;
00049     if (state == QAudio::IdleState)
00050     {
00051         // If audio output goes underrun (no available data) retry later!
00052         QTimer::singleShot(100, this, SLOT(startAudio()));
00053     }
00054 }
00055 
00056 QAudio::State   AudioOutputDeviceThread::state()
00057 {
00058     if (audioOutput)
00059         return audioOutput->state();
00060     return QAudio::StoppedState;
00061 }
00062 
00063 void    AudioOutputDeviceThread::startThread()
00064 {
00065     if (input)
00066     {
00067         qDebug() << "Start thread";
00068         start();
00069         QObject::moveToThread(this);
00070     }
00071 }
00072 
00073 void    AudioOutputDeviceThread::setInput(AudioIO *i)
00074 {
00075     qDebug() << "setInput : " << i->getName();
00076     input = i;
00077 }
00078 
00079 void    AudioOutputDeviceThread::startAudio()
00080 {
00081     qDebug() << "StartAudio!";
00082     if (isStarted() && state() == QAudio::SuspendedState)
00083     {
00084         audioOutput->resume();
00085         qDebug() << "Device resume";
00086     }
00087     else if (isStarted() && state() != QAudio::ActiveState)
00088     {
00089         audioOutput->start(input);
00090         qDebug() << "Device start : " << input->isOpen();
00091     }
00092     else
00093     {
00094         qDebug() << "Device not started !!!";
00095     }
00096 }
00097 
00098 void    AudioOutputDeviceThread::stopAudio()
00099 {
00100     if (isStarted())
00101     {
00102         audioOutput->stop();
00103         qDebug() << "Device stop";
00104     }
00105 }
00106 
00107 
00108 void    AudioOutputDeviceThread::pauseAudio()
00109 {
00110     if (isStarted())
00111     {
00112         audioOutput->suspend();
00113         qDebug() << "Device suspend";
00114     }
00115 }
00116 
00117 void  AudioOutputDeviceThread::run()
00118 {
00119     audioOutput = new QAudioOutput(_audioDevice, _format);
00120     connect(audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
00121     _isStarted = true;
00122     exec();
00123 }