1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Get rid of static methods and bugs

This commit is contained in:
Nils Fenner 2015-10-20 03:46:44 +02:00
parent 54547af7cf
commit b7cf4df13f
No known key found for this signature in database
GPG Key ID: 9591A163FF9BE04C
7 changed files with 66 additions and 156 deletions

View File

@ -92,29 +92,19 @@ void Audio::startAudioThread()
moveToThread(audioThread);
}
float Audio::getOutputVolume()
{
return getInstance().GetOutputVolume();
}
/**
Returns the current output volume, between 0 and 1
*/
qreal Audio::GetOutputVolume()
qreal Audio::getOutputVolume()
{
QMutexLocker locker(&audioOutLock);
return outputVolume;
}
void Audio::setOutputVolume(float volume)
{
getInstance().SetOutputVolume(volume);
}
/**
The volume must be between 0 and 1
*/
void Audio::SetOutputVolume(qreal volume)
void Audio::setOutputVolume(qreal volume)
{
QMutexLocker locker(&audioOutLock);
outputVolume = volume;
@ -139,33 +129,24 @@ void Audio::SetOutputVolume(qreal volume)
/**
The volume must be between 0 and 2
*/
void Audio::setInputVolume(float volume)
{
getInstance().SetInputVolume(volume);
}
void Audio::SetInputVolume(qreal volume)
void Audio::setInputVolume(qreal volume)
{
QMutexLocker locker(&audioInLock);
inputVolume = volume;
}
void Audio::suscribeInput()
{
getInstance().SubscribeInput();
}
/**
@brief Subscribe to capture sound from the opened input device.
If the input device is not open, it will be opened before capturing.
*/
void Audio::SubscribeInput()
void Audio::subscribeInput()
{
qDebug() << "subscribing input" << inputSubscriptions;
if (!inputSubscriptions++)
{
OpenInput(Settings::getInstance().getInDev());
openInput(Settings::getInstance().getInDev());
openOutput(Settings::getInstance().getOutDev());
#if (!FIX_SND_PCM_PREPARE_BUG)
if (alInDev)
@ -175,16 +156,6 @@ void Audio::SubscribeInput()
}
#endif
}
QTimer::singleShot(1, [=]()
{
OpenOutput(Settings::getInstance().getOutDev());
});
}
void Audio::unsuscribeInput()
{
getInstance().UnsubscribeInput();
}
/**
@ -192,7 +163,7 @@ void Audio::unsuscribeInput()
If the input device has no more subscriptions, it will be closed.
*/
void Audio::UnsubscribeInput()
void Audio::unsubscribeInput()
{
qDebug() << "unsubscribing input" << inputSubscriptions;
if (inputSubscriptions > 0)
@ -200,29 +171,16 @@ void Audio::UnsubscribeInput()
else if(inputSubscriptions < 0)
inputSubscriptions = 0;
if (!inputSubscriptions)
{
if (!inputSubscriptions) {
closeOutput();
#if (!FIX_SND_PCM_PREPARE_BUG)
if (alInDev)
{
qDebug() << "stopping capture";
alcCaptureStop(alInDev);
}
#endif
closeInput();
}
}
void Audio::openInput(const QString& inDevDescr)
{
getInstance().OpenInput(inDevDescr);
}
/**
Open an input device, use before suscribing
*/
void Audio::OpenInput(const QString& inDevDescr)
void Audio::openInput(const QString& inDevDescr)
{
QMutexLocker lock(&audioInLock);
auto* tmp = alInDev;
@ -261,15 +219,10 @@ void Audio::OpenInput(const QString& inDevDescr)
}
}
bool Audio::openOutput(const QString& outDevDescr)
{
return getInstance().OpenOutput(outDevDescr);
}
/**
Open an output device
*/
bool Audio::OpenOutput(const QString &outDevDescr)
bool Audio::openOutput(const QString &outDevDescr)
{
QMutexLocker lock(&audioOutLock);
auto* tmp = alOutDev;
@ -314,20 +267,20 @@ bool Audio::OpenOutput(const QString &outDevDescr)
return true;
}
void Audio::closeInput()
{
getInstance().CloseInput();
}
/**
Close an input device, please don't use unless everyone's unsuscribed
*/
void Audio::CloseInput()
void Audio::closeInput()
{
qDebug() << "Closing input";
QMutexLocker locker(&audioInLock);
if (alInDev)
{
#if (!FIX_SND_PCM_PREPARE_BUG)
qDebug() << "stopping capture";
alcCaptureStop(alInDev);
#endif
if (alcCaptureCloseDevice(alInDev) == ALC_TRUE)
{
alInDev = nullptr;
@ -340,15 +293,10 @@ void Audio::CloseInput()
}
}
void Audio::closeOutput()
{
getInstance().CloseOutput();
}
/**
Close an output device
*/
void Audio::CloseOutput()
void Audio::closeOutput()
{
qDebug() << "Closing output";
QMutexLocker locker(&audioOutLock);
@ -371,15 +319,10 @@ void Audio::CloseOutput()
}
}
void Audio::playMono16Sound(const QByteArray& data)
{
getInstance().PlayMono16Sound(data);
}
/**
Play a 44100Hz mono 16bit PCM sound
*/
void Audio::PlayMono16Sound(const QByteArray& data)
void Audio::playMono16Sound(const QByteArray& data)
{
QMutexLocker lock(&audioOutLock);
@ -430,20 +373,15 @@ void Audio::playGroupAudioQueued(Tox*,int group, int peer, const int16_t* data,
emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer);
}
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate)
{
getInstance().PlayGroupAudio(group, peer, data, samples, channels, sample_rate);
}
/**
Must be called from the audio thread, plays a group call's received audio
*/
void Audio::PlayGroupAudio(int group, int peer, const int16_t* data,
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate)
{
assert(QThread::currentThread() == audioThread);
QMutexLocker lock(&audioOutLock);
ToxGroupCall& call = Core::groupCalls[group];
@ -507,43 +445,28 @@ void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, u
alSourcePlay(alSource);
}
bool Audio::isInputReady()
{
return getInstance().IsInputReady();
}
/**
Returns true if the input device is open and suscribed to
*/
bool Audio::IsInputReady()
bool Audio::isInputReady()
{
QMutexLocker locker(&audioInLock);
return alInDev && inputSubscriptions;
}
bool Audio::isOutputClosed()
{
return getInstance().IsOutputClosed();
}
/**
Returns true if the output device is open
*/
bool Audio::IsOutputClosed()
bool Audio::isOutputClosed()
{
QMutexLocker locker(&audioOutLock);
return alOutDev;
}
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
{
return getInstance().TryCaptureSamples(buf, framesize);
}
/**
Does nothing and return false on failure
*/
bool Audio::TryCaptureSamples(uint8_t* buf, int framesize)
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
{
QMutexLocker lock(&audioInLock);

View File

@ -52,42 +52,24 @@ public:
public:
void startAudioThread();
static float getOutputVolume();
qreal GetOutputVolume();
static void setOutputVolume(float volume);
void SetOutputVolume(qreal volume);
qreal getOutputVolume();
void setOutputVolume(qreal volume);
static void setInputVolume(float volume);
void SetInputVolume(qreal volume);
void setInputVolume(qreal volume);
static void suscribeInput();
void SubscribeInput();
static void unsuscribeInput();
void UnsubscribeInput();
void subscribeInput();
void unsubscribeInput();
void openInput(const QString& inDevDescr);
bool openOutput(const QString& outDevDescr);
static void openInput(const QString& inDevDescr);
void OpenInput(const QString& inDevDescr);
static bool openOutput(const QString& outDevDescr);
bool OpenOutput(const QString& outDevDescr);
static void closeInput();
void CloseInput();
static void closeOutput();
void CloseOutput();
bool isInputReady();
bool isOutputClosed();
static bool isInputReady();
bool IsInputReady();
static bool isOutputClosed();
bool IsOutputClosed();
static void playMono16Sound(const QByteArray& data);
void PlayMono16Sound(const QByteArray& data);
static bool tryCaptureSamples(uint8_t* buf, int framesize);
bool TryCaptureSamples(uint8_t* buf, int framesize);
void playMono16Sound(const QByteArray& data);
bool tryCaptureSamples(uint8_t* buf, int framesize);
static void playGroupAudioQueued(Tox*, int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate, void*);
void PlayGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate);
#ifdef QTOX_FILTER_AUDIO
static void getEchoesToFilter(AudioFilterer* filter, int framesize);
@ -95,6 +77,8 @@ public:
#endif
public slots:
void closeInput();
void closeOutput();
void playGroupAudio(int group, int peer, const int16_t* data,
unsigned samples, uint8_t channels, unsigned sample_rate);

View File

@ -125,8 +125,9 @@ Core::~Core()
delete[] videobuf;
videobuf=nullptr;
Audio::closeInput();
Audio::closeOutput();
Audio& audio = Audio::getInstance();
audio.closeInput();
audio.closeOutput();
}
Core* Core::getInstance()

View File

@ -70,7 +70,7 @@ void Core::prepareCall(uint32_t friendId, int32_t callId, ToxAv* toxav, bool vid
qWarning() << QString("Error starting call %1: toxav_prepare_transmission failed with %2").arg(callId).arg(r);
// Audio
Audio::suscribeInput();
Audio::getInstance().subscribeInput();
// Go
calls[callId].active = true;
@ -248,7 +248,7 @@ void Core::cleanupCall(int32_t callId)
}
}
Audio::unsuscribeInput();
Audio::getInstance().unsubscribeInput();
toxav_kill_transmission(Core::getInstance()->toxav, callId);
if (!anyActiveCalls())
@ -278,7 +278,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav)
if (!calls[callId].active)
return;
if (calls[callId].muteMic || !Audio::isInputReady())
if (calls[callId].muteMic || !Audio::getInstance().isInputReady())
{
calls[callId].sendAudioTimer->start();
return;
@ -288,7 +288,7 @@ void Core::sendCallAudio(int32_t callId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize];
if (Audio::tryCaptureSamples(buf, framesize))
if (Audio::getInstance().tryCaptureSamples(buf, framesize))
{
#ifdef QTOX_FILTER_AUDIO
if (Settings::getInstance().getFilterAudio())
@ -602,7 +602,7 @@ void Core::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, un
ALint state;
alGetSourcei(alSource, AL_SOURCE_STATE, &state);
alSourcef(alSource, AL_GAIN, Audio::getOutputVolume());
alSourcef(alSource, AL_GAIN, Audio::getInstance().getOutputVolume());
if (state != AL_PLAYING)
{
alSourcePlay(alSource);
@ -628,7 +628,7 @@ void Core::joinGroupCall(int groupId)
groupCalls[groupId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT;
// Audio
Audio::suscribeInput();
Audio::getInstance().subscribeInput();
// Go
Core* core = Core::getInstance();
@ -651,7 +651,7 @@ void Core::leaveGroupCall(int groupId)
for (ALuint source : groupCalls[groupId].alSources)
alDeleteSources(1, &source);
groupCalls[groupId].alSources.clear();
Audio::unsuscribeInput();
Audio::getInstance().unsubscribeInput();
delete groupCalls[groupId].sendAudioTimer;
}
@ -660,7 +660,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
if (!groupCalls[groupId].active)
return;
if (groupCalls[groupId].muteMic || !Audio::isInputReady())
if (groupCalls[groupId].muteMic || !Audio::getInstance().isInputReady())
{
groupCalls[groupId].sendAudioTimer->start();
return;
@ -670,7 +670,7 @@ void Core::sendGroupCallAudio(int groupId, ToxAv* toxav)
const int bufsize = framesize * 2 * av_DefaultSettings.audio_channels;
uint8_t buf[bufsize];
if (Audio::tryCaptureSamples(buf, framesize))
if (Audio::getInstance().tryCaptureSamples(buf, framesize))
{
if (toxav_group_send_audio(toxav_get_tox(toxav), groupId, (int16_t*)buf,
framesize, av_DefaultSettings.audio_channels, av_DefaultSettings.audio_sample_rate) < 0)

View File

@ -89,7 +89,7 @@ void AVForm::showEvent(QShowEvent*)
getAudioInDevices();
createVideoSurface();
getVideoDevices();
Audio::suscribeInput();
Audio::getInstance().subscribeInput();
}
void AVForm::onVideoModesIndexChanged(int index)
@ -220,7 +220,7 @@ void AVForm::hideEvent(QHideEvent *)
killVideoSurface();
}
videoDeviceList.clear();
Audio::unsuscribeInput();
Audio::getInstance().unsubscribeInput();
}
void AVForm::getVideoDevices()
@ -314,19 +314,21 @@ void AVForm::getAudioOutDevices()
void AVForm::onInDevChanged(const QString &deviceDescriptor)
{
Settings::getInstance().setInDev(deviceDescriptor);
Audio::openInput(deviceDescriptor);
Audio::unsuscribeInput();
Audio::suscribeInput();
Audio& audio = Audio::getInstance();
audio.openInput(deviceDescriptor);
audio.unsubscribeInput();
audio.subscribeInput();
}
void AVForm::onOutDevChanged(const QString& deviceDescriptor)
{
Settings::getInstance().setInDev(deviceDescriptor);
Audio::openOutput(deviceDescriptor);
Settings::getInstance().setOutDev(deviceDescriptor);
Audio::unsuscribeInput();
Audio::suscribeInput();
Audio& audio = Audio::getInstance();
audio.openOutput(deviceDescriptor);
audio.unsubscribeInput();
audio.subscribeInput();
}
void AVForm::onFilterAudioToggled(bool filterAudio)
@ -336,13 +338,13 @@ void AVForm::onFilterAudioToggled(bool filterAudio)
void AVForm::on_playbackSlider_valueChanged(int value)
{
Audio::setOutputVolume(value / 100.0);
Audio::getInstance().setOutputVolume(value / 100.0);
bodyUI->playbackMax->setText(QString::number(value));
}
void AVForm::on_microphoneSlider_valueChanged(int value)
{
Audio::setInputVolume(value / 100.0);
Audio::getInstance().setInputVolume(value / 100.0);
bodyUI->microphoneMax->setText(QString::number(value));
}

View File

@ -65,7 +65,7 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*)
uint8_t buff[bufsize];
memset(buff, 0, bufsize);
if (Audio::tryCaptureSamples(buff, framesize))
if (Audio::getInstance().tryCaptureSamples(buff, framesize))
{
double max = 0;
int16_t* buffReal = reinterpret_cast<int16_t*>(&buff[0]);
@ -88,13 +88,13 @@ void MicFeedbackWidget::timerEvent(QTimerEvent*)
void MicFeedbackWidget::showEvent(QShowEvent*)
{
Audio::suscribeInput();
Audio::getInstance().subscribeInput();
timerId = startTimer(60);
}
void MicFeedbackWidget::hideEvent(QHideEvent*)
{
Audio::unsuscribeInput();
Audio::getInstance().unsubscribeInput();
if (timerId != 0)
{

View File

@ -1249,7 +1249,7 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool notify)
sndFile.close();
}
Audio::playMono16Sound(sndData);
Audio::getInstance().playMono16Sound(sndData);
}
}
@ -1271,7 +1271,7 @@ void Widget::playRingtone()
sndFile1.close();
}
Audio::playMono16Sound(sndData1);
Audio::getInstance().playMono16Sound(sndData1);
}
void Widget::onFriendRequestReceived(const QString& userId, const QString& message)