1 /*
2  * Modern effects for a modern Streamer
3  * Copyright (C) 2020 Michael Fabian Dirks
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 
20 #pragma once
21 #include "common.hpp"
22 #include "util/util-event.hpp"
23 
24 namespace obs {
25 	template<typename T>
26 	class signal_handler_base {
27 		protected:
28 		std::string _signal;
29 
30 		public:
31 		util::event<T, calldata*> event;
32 	};
33 
34 	template<typename T>
35 	class signal_handler : public signal_handler_base<T> {
36 		public:
signal_handler(std::string signal,T keepalive)37 		signal_handler(std::string signal, T keepalive) {}
~signal_handler()38 		virtual ~signal_handler() {}
39 	};
40 
41 	template<>
42 	class signal_handler<std::shared_ptr<obs_source_t>> : public signal_handler_base<std::shared_ptr<obs_source_t>> {
43 		std::shared_ptr<obs_source_t> _keepalive;
44 
handle_signal(void * ptr,calldata * cd)45 		static void handle_signal(void* ptr, calldata* cd) noexcept
46 		try {
47 			auto p = reinterpret_cast<signal_handler<std::shared_ptr<obs_source_t>>*>(ptr);
48 			p->event(p->_keepalive, cd);
49 		} catch (...) {
50 		}
51 
52 		public:
signal_handler(std::string signal,std::shared_ptr<obs_source_t> keepalive)53 		signal_handler(std::string signal, std::shared_ptr<obs_source_t> keepalive) : _keepalive(keepalive)
54 		{
55 			_signal              = signal;
56 			signal_handler_t* sh = obs_source_get_signal_handler(_keepalive.get());
57 			signal_handler_connect(sh, _signal.c_str(), handle_signal, this);
58 		}
~signal_handler()59 		virtual ~signal_handler()
60 		{
61 			event.clear();
62 			signal_handler_t* sh = obs_source_get_signal_handler(_keepalive.get());
63 			signal_handler_disconnect(sh, _signal.c_str(), handle_signal, this);
64 		}
65 	};
66 
67 	typedef signal_handler<std::shared_ptr<obs_source_t>> source_signal_handler;
68 
69 	// Audio Capture is also here, as it could be considered a signal.
70 	class audio_signal_handler {
71 		std::shared_ptr<obs_source_t> _keepalive;
72 
handle_audio(void * ptr,obs_source_t *,const struct audio_data * audio_data,bool muted)73 		static void handle_audio(void* ptr, obs_source_t*, const struct audio_data* audio_data, bool muted) noexcept
74 		try {
75 			auto p = reinterpret_cast<audio_signal_handler*>(ptr);
76 			p->event(p->_keepalive, audio_data, muted);
77 		} catch (...) {
78 		}
79 
80 		public:
audio_signal_handler(std::shared_ptr<obs_source_t> keepalive)81 		audio_signal_handler(std::shared_ptr<obs_source_t> keepalive) : _keepalive(keepalive), event()
82 		{
83 			obs_source_add_audio_capture_callback(_keepalive.get(), handle_audio, this);
84 		}
~audio_signal_handler()85 		virtual ~audio_signal_handler()
86 		{
87 			event.clear();
88 			obs_source_remove_audio_capture_callback(_keepalive.get(), handle_audio, this);
89 		}
90 
91 		util::event<std::shared_ptr<obs_source_t>, const struct audio_data*, bool> event;
92 	};
93 
94 } // namespace obs
95