obs-audio-capture: Use util::event and delete move and copy operator

By using util::event a single obs::audio_capture can now have multiple callbacks for the same data, without requiring additional obs::audio_capture to be present. Additionally it allows us to stop listening on an obs callback if there are no actual listeners, and start listening as soon as there is a listener.

Move and Copy constructors are currently not supported by this class and thus have been marked deleted. Internally also now using obs::source to keep track of the used source.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-01-23 20:06:30 +01:00
parent 52af973014
commit 911b8a1cf4
2 changed files with 50 additions and 24 deletions

View file

@ -22,28 +22,36 @@
void obs::audio_capture::audio_capture_cb(void* data, obs_source_t*, const struct audio_data* audio, bool muted)
{
auto self = reinterpret_cast<obs::audio_capture*>(data);
self->cb(self->cb_data, audio, muted);
if (self->on.data) {
self->on.data(self->m_self, audio, muted);
}
}
void obs::audio_capture::on_data_listen()
{
if (this->m_self) {
obs_source_add_audio_capture_callback(this->m_self->get(), audio_capture_cb, this);
}
}
void obs::audio_capture::on_data_silence()
{
if (this->m_self) {
obs_source_remove_audio_capture_callback(this->m_self->get(), audio_capture_cb, this);
}
}
obs::audio_capture::audio_capture(obs_source_t* source)
{
this->source = source;
obs_source_add_audio_capture_callback(this->source, audio_capture_cb, this);
this->m_self = std::make_shared<obs::source>(source, true, true);
this->on.data.set_listen_callback(std::bind(&obs::audio_capture::on_data_listen, this));
this->on.data.set_silence_callback(std::bind(&obs::audio_capture::on_data_silence, this));
}
obs::audio_capture::audio_capture(std::shared_ptr<obs::source> source) : audio_capture(source->get()) {}
obs::audio_capture::~audio_capture()
{
obs_source_remove_audio_capture_callback(this->source, audio_capture_cb, this);
}
void obs::audio_capture::set_callback(audio_capture_callback_t cb, void* data)
{
this->cb = cb;
this->cb_data = data;
}
void obs::audio_capture::set_callback(audio_capture_callback_t cb)
{
this->cb = cb;
this->cb_data = nullptr;
on.data.clear();
this->m_self.reset();
}

View file

@ -19,6 +19,7 @@
#pragma once
#include <functional>
#include "obs-source.hpp"
// OBS
#ifdef _MSC_VER
@ -31,20 +32,37 @@
#endif
namespace obs {
typedef std::function<void(void* data, struct audio_data const* audio, bool muted)> audio_capture_callback_t;
class audio_capture {
obs_source_t* source;
audio_capture_callback_t cb;
void* cb_data;
std::shared_ptr<obs::source> m_self;
static void audio_capture_cb(void*, obs_source_t*, struct audio_data const*, bool);
void on_data_listen();
void on_data_silence();
public:
audio_capture(obs_source_t* source);
virtual ~audio_capture();
void set_callback(audio_capture_callback_t cb, void* data);
void set_callback(audio_capture_callback_t cb);
audio_capture(std::shared_ptr<obs::source> source);
~audio_capture();
public /*copy*/:
audio_capture(audio_capture const& other) = delete;
audio_capture& operator=(audio_capture const& other) = delete;
public /*move*/:
audio_capture(audio_capture&& other) = delete;
audio_capture& operator=(audio_capture&& other) = delete;
public /*events*/:
struct {
//! Called if there is new audio data.
//
// @param std::shared_ptr<obs::source> Source
// @param audio_data const* const Audio Data
// @param bool Muted
util::event<std::shared_ptr<obs::source>, audio_data const* const, bool> data;
} on;
};
} // namespace obs