From 326fa68d9fbd30330805e9557c0603d19269b5f0 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Fri, 14 Feb 2020 07:30:46 +0100 Subject: [PATCH] obs-signal-handler: Helper classes for signals and audio --- CMakeLists.txt | 2 + source/obs/obs-signal-handler.cpp | 20 +++++++ source/obs/obs-signal-handler.hpp | 97 +++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 source/obs/obs-signal-handler.cpp create mode 100644 source/obs/obs-signal-handler.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b10435..0965cf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -421,6 +421,8 @@ set(PROJECT_PRIVATE_SOURCE "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-vertex.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-vertexbuffer.hpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-vertexbuffer.cpp" + "${PROJECT_SOURCE_DIR}/source/obs/obs-signal-handler.hpp" + "${PROJECT_SOURCE_DIR}/source/obs/obs-signal-handler.cpp" "${PROJECT_SOURCE_DIR}/source/obs/obs-source.hpp" "${PROJECT_SOURCE_DIR}/source/obs/obs-source.cpp" "${PROJECT_SOURCE_DIR}/source/obs/obs-source-factory.hpp" diff --git a/source/obs/obs-signal-handler.cpp b/source/obs/obs-signal-handler.cpp new file mode 100644 index 0000000..e9faf7a --- /dev/null +++ b/source/obs/obs-signal-handler.cpp @@ -0,0 +1,20 @@ +/* + * Modern effects for a modern Streamer + * Copyright (C) 2018 Michael Fabian Dirks + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "obs-signal-handler.hpp" diff --git a/source/obs/obs-signal-handler.hpp b/source/obs/obs-signal-handler.hpp new file mode 100644 index 0000000..c7ebd4c --- /dev/null +++ b/source/obs/obs-signal-handler.hpp @@ -0,0 +1,97 @@ +/* + * Modern effects for a modern Streamer + * Copyright (C) 2020 Michael Fabian Dirks + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma once +#include +#include +#include "util-event.hpp" + +#include + +namespace obs { + template + class signal_handler_base { + protected: + std::string _signal; + + public: + util::event event; + }; + + template + class signal_handler : public signal_handler_base { + public: + signal_handler(std::string signal, T keepalive) {} + virtual ~signal_handler() {} + }; + + template<> + class signal_handler> : public signal_handler_base> { + std::shared_ptr _keepalive; + + static void handle_signal(void* ptr, calldata* cd) noexcept + try { + auto p = reinterpret_cast>&>(ptr); + p.event(p._keepalive, cd); + } catch (...) { + } + + public: + signal_handler(std::string signal, std::shared_ptr keepalive) : _keepalive(keepalive) + { + _signal = signal; + signal_handler_t* sh = obs_source_get_signal_handler(_keepalive.get()); + signal_handler_connect(sh, _signal.c_str(), handle_signal, this); + } + virtual ~signal_handler() + { + event.clear(); + signal_handler_t* sh = obs_source_get_signal_handler(_keepalive.get()); + signal_handler_disconnect(sh, _signal.c_str(), handle_signal, this); + } + }; + + typedef signal_handler> source_signal_handler; + + // Audio Capture is also here, as it could be considered a signal. + class audio_signal_handler { + std::shared_ptr _keepalive; + + static void handle_audio(void* ptr, obs_source_t*, const struct audio_data* audio_data, bool muted) noexcept + try { + auto p = reinterpret_cast(ptr); + p.event(p._keepalive, audio_data, muted); + } catch (...) { + } + + public: + audio_signal_handler(std::shared_ptr keepalive) : _keepalive(keepalive), event() + { + obs_source_add_audio_capture_callback(_keepalive.get(), handle_audio, this); + } + virtual ~audio_signal_handler() + { + event.clear(); + obs_source_remove_audio_capture_callback(_keepalive.get(), handle_audio, this); + } + + util::event, const struct audio_data*, bool> event; + }; + +} // namespace obs