Support audio::stream on_stop callback

This commit is contained in:
Nikita Lisitsa 2022-02-25 14:52:48 +03:00
parent 425d41eae4
commit 0fa0cbbc09
2 changed files with 24 additions and 3 deletions

View file

@ -2,6 +2,8 @@
#include <psemek/audio/effect.hpp> #include <psemek/audio/effect.hpp>
#include <psemek/util/function.hpp>
#include <memory> #include <memory>
namespace psemek::audio namespace psemek::audio
@ -22,6 +24,8 @@ namespace psemek::audio
virtual void stop() = 0; virtual void stop() = 0;
virtual bool is_playing() const = 0; virtual bool is_playing() const = 0;
virtual void on_stop(util::function<void()> callback) = 0;
virtual ~stream() {} virtual ~stream() {}
}; };

View file

@ -85,6 +85,8 @@ namespace psemek::audio
std::vector<std::shared_ptr<effect>> effects; std::vector<std::shared_ptr<effect>> effects;
util::function<void()> callback;
stream_impl(int channel, std::shared_ptr<track_impl> track, bool loop, spec_t spec) stream_impl(int channel, std::shared_ptr<track_impl> track, bool loop, spec_t spec)
: channel(channel) : channel(channel)
, track(track) , track(track)
@ -152,6 +154,11 @@ namespace psemek::audio
{ {
return playing; return playing;
} }
void on_stop(util::function<void()> callback) override
{
this->callback = std::move(callback);
}
}; };
} }
@ -268,9 +275,19 @@ namespace psemek::audio
} }
if (!self) return; if (!self) return;
std::lock_guard lock{self->channels_mutex}; util::function<void()> callback;
self->channels[ch].stream->playing = false; {
self->channels[ch].stream = nullptr; // callback will probably lock something as well,
// leading to a deadlock, so we have to call the
// callback outside of the lock
std::lock_guard lock{self->channels_mutex};
self->channels[ch].stream->playing = false;
callback = std::move(self->channels[ch].stream->callback);
self->channels[ch].stream = nullptr;
}
if (callback)
callback();
} }
engine::engine() engine::engine()