Skip to content

Commit

Permalink
Replace goto with RAII
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed Feb 3, 2023
1 parent 23e2e17 commit f1bc07e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/mumble/OSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "User.h"
#include "Global.h"

#include <memory>
#include <vector>

#define NBLOCKS 8
Expand Down Expand Up @@ -177,14 +178,30 @@ OSSInput::~OSSInput() {
wait();
}

class FileDescriptor {
public:
FileDescriptor(int fd = -1) : m_fd(fd) {}
~FileDescriptor() {
if (m_fd != -1) {
close(m_fd);
m_fd = -1;
}
}

operator int() const { return m_fd; }

private:
int m_fd = -1;
};

void OSSInput::run() {
QByteArray device = cards->qhDevices.value(Global::get().s.qsOSSInput).toLatin1();
if (device.isEmpty()) {
qWarning("OSSInput: Stored device not found, falling back to default");
device = cards->qhDevices.value(QString()).toLatin1();
}

int fd = open(device.constData(), O_RDONLY, 0);
FileDescriptor fd(open(device.constData(), O_RDONLY, 0));
if (fd == -1) {
qWarning("OSSInput: Failed to open %s", device.constData());
return;
Expand All @@ -195,20 +212,20 @@ void OSSInput::run() {
ival = AFMT_S16_NE;
if ((ioctl(fd, SNDCTL_DSP_SETFMT, &ival) == -1) || (ival != AFMT_S16_NE)) {
qWarning("OSSInput: Failed to set sound format");
goto out;
return;
}

ival = 1;
if ((ioctl(fd, SNDCTL_DSP_CHANNELS, &ival) == -1)) {
qWarning("OSSInput: Failed to set mono mode");
goto out;
return;
}
iMicChannels = ival;

ival = SAMPLE_RATE;
if (ioctl(fd, SNDCTL_DSP_SPEED, &ival) == -1) {
qWarning("OSSInput: Failed to set speed");
goto out;
return;
}
iMicFreq = ival;

Expand All @@ -231,9 +248,6 @@ void OSSInput::run() {

qWarning("OSSInput: Releasing.");
ioctl(fd, SNDCTL_DSP_RESET, nullptr);

out:
close(fd);
}

OSSOutput::OSSOutput() {
Expand Down

0 comments on commit f1bc07e

Please sign in to comment.