From 911b8a1cf433c1c6f263f8e88d36b68c78e4afe8 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Wed, 23 Jan 2019 20:06:30 +0100 Subject: [PATCH] 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. --- source/obs-audio-capture.cpp | 40 +++++++++++++++++++++--------------- source/obs-audio-capture.hpp | 34 ++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/source/obs-audio-capture.cpp b/source/obs-audio-capture.cpp index 278c264..fee690e 100644 --- a/source/obs-audio-capture.cpp +++ b/source/obs-audio-capture.cpp @@ -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(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(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 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(); } diff --git a/source/obs-audio-capture.hpp b/source/obs-audio-capture.hpp index a03f7f9..4fab8f1 100644 --- a/source/obs-audio-capture.hpp +++ b/source/obs-audio-capture.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include "obs-source.hpp" // OBS #ifdef _MSC_VER @@ -31,20 +32,37 @@ #endif namespace obs { - typedef std::function audio_capture_callback_t; - class audio_capture { - obs_source_t* source; - audio_capture_callback_t cb; - void* cb_data; + std::shared_ptr 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 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 Source + // @param audio_data const* const Audio Data + // @param bool Muted + util::event, audio_data const* const, bool> data; + } on; }; } // namespace obs