Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PulseAudio: Cork audio input when it isn't needed #171

Merged
merged 1 commit into from Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mumble/AudioWizard.cpp
Expand Up @@ -36,6 +36,7 @@ AudioWizard::AudioWizard(QWidget *p) : QWizard(p) {
bInit = true;
bLastActive = false;
g.bInAudioWizard = true;
g.mw->onChangeMute();

ticker = new QTimer(this);
ticker->setObjectName(QLatin1String("Ticker"));
Expand Down Expand Up @@ -398,6 +399,7 @@ void AudioWizard::reject() {
ao->wipe();
aosSource = NULL;
g.bInAudioWizard = false;
g.mw->onChangeMute();

QWizard::reject();
}
Expand Down Expand Up @@ -433,6 +435,7 @@ void AudioWizard::accept() {
g.bPosTest = false;
restartAudio();
g.bInAudioWizard = false;
g.mw->onChangeMute();
QWizard::accept();
}

Expand Down
12 changes: 12 additions & 0 deletions src/mumble/MainWindow.cpp
Expand Up @@ -2306,6 +2306,7 @@ void MainWindow::on_qaAudioMute_triggered() {
g.sh->setSelfMuteDeafState(g.s.bMute, g.s.bDeaf);
}

onChangeMute();
updateTrayIcon();
}

Expand Down Expand Up @@ -2342,6 +2343,7 @@ void MainWindow::on_qaAudioDeaf_triggered() {
g.sh->setSelfMuteDeafState(g.s.bMute, g.s.bDeaf);
}

onChangeMute();
updateTrayIcon();
}

Expand Down Expand Up @@ -2790,6 +2792,15 @@ void MainWindow::whisperReleased(QVariant scdata) {
updateTarget();
}

void MainWindow::onChangeMute()
{
if (!g.ai) {
return;
}

emit corkAudioInputStream(g.s.bMute && !g.bInAudioWizard);
}

void MainWindow::onResetAudio()
{
qWarning("MainWindow: Start audio reset");
Expand Down Expand Up @@ -2849,6 +2860,7 @@ void MainWindow::serverConnected() {

if (g.s.bMute || g.s.bDeaf) {
g.sh->setSelfMuteDeafState(g.s.bMute, g.s.bDeaf);
onChangeMute();
}

// Update QActions and menues
Expand Down
14 changes: 13 additions & 1 deletion src/mumble/MainWindow.h
Expand Up @@ -269,6 +269,9 @@ class MainWindow : public QMainWindow, public MessageHandler, public Ui::MainWin
void pttReleased();
void whisperReleased(QVariant scdata);
void onResetAudio();
/// Called whenever the self-mute state may have changed. Dispatches
/// corkAudioInputStream() if the desire for audio input has changed.
void onChangeMute();
cbs228 marked this conversation as resolved.
Show resolved Hide resolved
void showRaiseWindow();
void on_qaFilterToggle_triggered();
/// Opens a save dialog for the image referenced by qtcSaveImageCursor.
Expand All @@ -279,7 +282,16 @@ class MainWindow : public QMainWindow, public MessageHandler, public Ui::MainWin
/// Updates the user's image directory to the given path (any included
/// filename is discarded).
void updateImagePath(QString filepath) const;

signals:
/// Reports that audio input is now, or is no longer, required.
///
/// Signal allows an \ref AudioInput to suspend the input stream
/// when it is not required.
///
/// @param cork If true, the audio backend MAY now cork/suspend
/// the input stream. If false, the audio backend MUST immediately
/// un-cork/resume the stream.
void corkAudioInputStream(const bool cork);
public:
MainWindow(QWidget *parent);
~MainWindow() Q_DECL_OVERRIDE;
Expand Down
14 changes: 14 additions & 0 deletions src/mumble/PulseAudio.cpp
Expand Up @@ -301,6 +301,11 @@ void PulseAudioSystem::eventCallback(pa_mainloop_api *api, pa_defer_event *) {
qsInputCache = idev;

pa_stream_connect_record(pasInput, qPrintable(idev), &buff, PA_STREAM_ADJUST_LATENCY);

// Ensure stream is initially un-muted
pa_stream_cork(pasInput, 0, NULL, NULL);

connect(g.mw, &MainWindow::corkAudioInputStream, this, &PulseAudioSystem::corkAudioInputStream);
}
}

Expand Down Expand Up @@ -371,6 +376,15 @@ void PulseAudioSystem::eventCallback(pa_mainloop_api *api, pa_defer_event *) {
}
}

void PulseAudioSystem::corkAudioInputStream(const bool cork)
{
if (pasInput) {
pa_threaded_mainloop_lock(pam);
pa_stream_cork(pasInput, cork, nullptr, nullptr);
pa_threaded_mainloop_unlock(pam);
}
}

void PulseAudioSystem::context_state_callback(pa_context *c, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
pas->contextCallback(c);
Expand Down
3 changes: 3 additions & 0 deletions src/mumble/PulseAudio.h
Expand Up @@ -80,6 +80,9 @@ class PulseAudioSystem : public QObject {
void setVolumes();
PulseAttenuation* getAttenuation(QString stream_restore_id);

public slots:
void corkAudioInputStream(const bool cork);

public:
QHash<QString, QString> qhInput;
QHash<QString, QString> qhOutput;
Expand Down
1 change: 1 addition & 0 deletions src/mumble/main.cpp
Expand Up @@ -479,6 +479,7 @@ int main(int argc, char **argv) {
g.p->rescanPlugins();

Audio::start();
g.mw->onChangeMute();

a.setQuitOnLastWindowClosed(false);

Expand Down