dolphin/Source/Core/AudioCommon/AlsaSoundStream.h
Scott Mansell a10a3ecbac ALSA: Don't block on Clear() call.
snd_pcm_writei() is meant to block block until all samples are written,
but apparently in some situations it can block for much longer, prehaps
even a infinite time, in the case of virtual machine FifoCI runs in.

Because it grabed a mutex before blocking, it could also block the
Clear() call for an infinite length of time, blocking dolphin's emu
thread.

snd_pcm_writei() also takes 10-15 seconds if you run dolphin under GDB
and can randomly take 5 or so seconds during normal usage.

By moving all the pause code to the ALSA thread, Clear() no-longer
blocks and everyone keeps their sanity.
2015-10-03 16:31:55 +13:00

66 lines
1.2 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#if defined(HAVE_ALSA) && HAVE_ALSA
#include <alsa/asoundlib.h>
#endif
#include "AudioCommon/SoundStream.h"
#include "Common/CommonTypes.h"
class AlsaSound final : public SoundStream
{
#if defined(HAVE_ALSA) && HAVE_ALSA
public:
AlsaSound();
bool Start() override;
void SoundLoop() override;
void Stop() override;
void Update() override;
void Clear(bool) override;
static bool isValid()
{
return true;
}
private:
// maximum number of frames the buffer can hold
static constexpr size_t BUFFER_SIZE_MAX = 8192;
// minimum number of frames to deliver in one transfer
static constexpr u32 FRAME_COUNT_MIN = 256;
// number of channels per frame
static constexpr u32 CHANNEL_COUNT = 2;
enum class ALSAThreadStatus
{
RUNNING,
PAUSED,
STOPPING,
STOPPED,
};
bool AlsaInit();
void AlsaShutdown();
s16 mix_buffer[BUFFER_SIZE_MAX * CHANNEL_COUNT];
std::thread thread;
std::atomic<ALSAThreadStatus> m_thread_status;
std::condition_variable cv;
std::mutex cv_m;
snd_pcm_t *handle;
unsigned int frames_to_deliver;
#endif
};