Skip to content

Commit

Permalink
rpcsx-os: alsa: non-blocking mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DHrpcs3 committed Oct 4, 2024
1 parent 113abf2 commit 87845d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
49 changes: 21 additions & 28 deletions rpcsx-os/audio/AlsaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ void AlsaDevice::start() {
std::abort();
}

uint periods = mSampleCount;
if (auto err = snd_pcm_hw_params_set_periods_max(mPCMHandle, mHWParams,
&periods, nullptr);
if (auto err =
snd_pcm_hw_params_set_periods(mPCMHandle, mHWParams, mSampleCount, 0);
err < 0) {
ORBIS_LOG_FATAL("Cannot set periods count", snd_strerror(err));
std::abort();
Expand All @@ -91,7 +90,7 @@ void AlsaDevice::start() {
snd_pcm_uframes_t size = mSampleSize / frameBytes;

{
auto trySize = size * mSampleCount;
auto trySize = size * mSampleCount * 8;
int err = -1;
while (trySize >= 1024) {
err = snd_pcm_hw_params_set_buffer_size(mPCMHandle, mHWParams, trySize);
Expand All @@ -104,7 +103,7 @@ void AlsaDevice::start() {
}

if (err < 0) {
trySize = size;
trySize = size * mSampleCount * 8;
err = snd_pcm_hw_params_set_buffer_size_near(mPCMHandle, mHWParams,
&trySize);
}
Expand All @@ -116,7 +115,7 @@ void AlsaDevice::start() {
}

{
auto trySize = size;
auto trySize = size * mSampleCount;
int err = -1;
while (trySize >= 256) {
err =
Expand All @@ -130,7 +129,7 @@ void AlsaDevice::start() {
}

if (err < 0) {
trySize = size;
trySize = size * mSampleCount;
err = snd_pcm_hw_params_set_period_size_near(mPCMHandle, mHWParams,
&trySize, 0);
}
Expand Down Expand Up @@ -193,6 +192,11 @@ void AlsaDevice::start() {
std::abort();
}

if (auto err = snd_pcm_nonblock(mPCMHandle, 1); err < 0) {
ORBIS_LOG_FATAL("set nonblock mode failed", snd_strerror(err));
std::abort();
}

if (auto err = snd_pcm_prepare(mPCMHandle); err < 0) {
ORBIS_LOG_FATAL("cannot prepare audio interface for use",
snd_strerror(err));
Expand Down Expand Up @@ -220,20 +224,8 @@ int AlsaDevice::fixXRun() {
}

int AlsaDevice::resume() {
int err;
while (true) {
err = snd_pcm_resume(mPCMHandle);

if (err == -EAGAIN) {
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}

break;
}

if (err == 0) {
return 0;
if (int err = snd_pcm_resume(mPCMHandle); err == -EAGAIN) {
return err;
}

return snd_pcm_prepare(mPCMHandle);
Expand All @@ -245,24 +237,25 @@ long AlsaDevice::write(void *buf, long len) {

int frameBytes = snd_pcm_format_physical_width(mAlsaFormat) * mChannels / 8;
snd_pcm_uframes_t frames = len / frameBytes;
if (frames == 0) {
return 0;
}

snd_pcm_wait(mPCMHandle, SND_PCM_WAIT_IO);

while (true) {
snd_pcm_sframes_t r = snd_pcm_writei(mPCMHandle, buf, frames);

if (r == -EPIPE) {
r = fixXRun();

if (r == 0) {
continue;
}
}

if (r == -ESTRPIPE) {
r = resume();
}

if (r == 0) {
continue;
}
if (r == 0 || r == -EAGAIN) {
continue;
}

if (r < 0) {
Expand Down
13 changes: 11 additions & 2 deletions rpcsx-os/iodev/aout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,22 @@ static orbis::ErrorCode aout_ioctl(orbis::File *file, std::uint64_t request,
static orbis::ErrorCode aout_write(orbis::File *file, orbis::Uio *uio,
orbis::Thread *thread) {
auto device = static_cast<AoutDevice *>(file->device.get());
std::uint64_t totalSize = 0;
if (auto audioDevice = device->audioDevice) {
for (auto vec : std::span(uio->iov, uio->iovcnt)) {
audioDevice->write(vec.base, vec.len);
auto result = audioDevice->write(vec.base, vec.len);

if (result < 0) {
return static_cast<orbis::ErrorCode>(-result);
}

// rx::hexdump({(std::byte*)vec.base, vec.len});
uio->offset += vec.len;
uio->offset += result;
totalSize += result;
}
}

thread->retval[0] = totalSize;
return {};
}

Expand Down

0 comments on commit 87845d6

Please sign in to comment.