Add love.audiodisconnected callback.

The callback is fired when the audio has been disconnected.
This commit is contained in:
Miku AuahDark
2022-05-03 20:39:32 +08:00
parent 5e0f0dc02e
commit 2cc93af436
4 changed files with 44 additions and 5 deletions
+31 -2
View File
@@ -20,6 +20,7 @@
#include "Pool.h"
#include "event/Event.h"
#include "Source.h"
namespace love
@@ -29,8 +30,10 @@ namespace audio
namespace openal
{
Pool::Pool()
: sources()
Pool::Pool(ALCdevice *device)
: device(device)
, sources()
, disconnectNotified(false)
, totalSources(0)
{
// Clear errors.
@@ -101,8 +104,34 @@ bool Pool::isPlaying(Source *s)
void Pool::update()
{
#ifndef ALC_CONNECTED
constexpr ALCenum ALC_CONNECTED = 0x313;
#endif
thread::Lock lock(mutex);
static bool disconnectExtSupported = alcIsExtensionPresent(device, "ALC_EXT_Disconnect") == ALC_TRUE;
// Device disconnection event
if (disconnectExtSupported)
{
auto eventModule = Module::getInstance<event::Event>(Module::M_EVENT);
if (eventModule)
{
ALCint connected;
alcGetIntegerv(device, ALC_CONNECTED, 1, &connected);
if (connected)
disconnectNotified = false;
else if (!disconnectNotified)
{
StrongRef<event::Message> msg(new event::Message("audiodisconnected"), Acquire::NORETAIN);
eventModule->push(msg);
disconnectNotified = true;
}
}
}
std::vector<Source *> torelease;
for (const auto &i : playing)