|
| 1 | +/* |
| 2 | +obs-websocket |
| 3 | +Copyright (C) 2016-2021 Stephane Lepin <[email protected]> |
| 4 | +Copyright (C) 2020-2021 Kyle Manning <[email protected]> |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 2 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License along |
| 17 | +with this program. If not, see <https://www.gnu.org/licenses/> |
| 18 | +*/ |
| 19 | + |
| 20 | +#ifndef _OBS_WEBSOCKET_API_H |
| 21 | +#define _OBS_WEBSOCKET_API_H |
| 22 | + |
| 23 | +#include <obs.h> |
| 24 | + |
| 25 | +#define OBS_WEBSOCKET_API_VERSION 1 |
| 26 | + |
| 27 | +#ifdef __cplusplus |
| 28 | +extern "C" { |
| 29 | +#endif |
| 30 | + |
| 31 | +typedef void* obs_websocket_vendor; |
| 32 | +typedef void (*obs_websocket_request_callback_function)(obs_data_t*, obs_data_t*, void*); |
| 33 | + |
| 34 | +struct obs_websocket_request_callback { |
| 35 | + obs_websocket_request_callback_function callback; |
| 36 | + void *priv_data; |
| 37 | +}; |
| 38 | + |
| 39 | +inline proc_handler_t *ph; |
| 40 | + |
| 41 | +static inline proc_handler_t *obs_websocket_get_ph(void) |
| 42 | +{ |
| 43 | + proc_handler_t *global_ph = obs_get_proc_handler(); |
| 44 | + assert(global_ph != NULL); |
| 45 | + |
| 46 | + calldata_t cd = {0}; |
| 47 | + if (!proc_handler_call(global_ph, "obs_websocket_api_get_ph", &cd)) |
| 48 | + blog(LOG_DEBUG, "Unable to fetch obs-websocket proc handler object. obs-websocket not installed?"); |
| 49 | + proc_handler_t *ret = (proc_handler_t*)calldata_ptr(&cd, "ph"); |
| 50 | + calldata_free(&cd); |
| 51 | + |
| 52 | + return ret; |
| 53 | +} |
| 54 | + |
| 55 | +static inline bool obs_websocket_run_simple_proc(obs_websocket_vendor vendor, const char *proc_name, calldata_t *cd) |
| 56 | +{ |
| 57 | + if (!ph || !vendor || !proc_name || !strlen(proc_name) || !cd) |
| 58 | + return false; |
| 59 | + |
| 60 | + calldata_set_ptr(cd, "vendor", vendor); |
| 61 | + |
| 62 | + proc_handler_call(ph, proc_name, cd); |
| 63 | + return calldata_bool(cd, "success"); |
| 64 | +} |
| 65 | + |
| 66 | +// ALWAYS CALL VIA `obs_module_post_load()` CALLBACK! |
| 67 | +// Registers a new "vendor" (Example: obs-ndi) |
| 68 | +static inline obs_websocket_vendor obs_websocket_register_vendor(const char *vendor_name) |
| 69 | +{ |
| 70 | + ph = obs_websocket_get_ph(); |
| 71 | + if (!ph) |
| 72 | + return NULL; |
| 73 | + |
| 74 | + calldata_t cd = {0}; |
| 75 | + |
| 76 | + calldata_set_string(&cd, "name", vendor_name); |
| 77 | + |
| 78 | + proc_handler_call(ph, "vendor_register", &cd); |
| 79 | + obs_websocket_vendor ret = calldata_ptr(&cd, "vendor"); |
| 80 | + calldata_free(&cd); |
| 81 | + |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +// Registers a new request for a vendor |
| 86 | +static inline bool obs_websocket_vendor_register_request(obs_websocket_vendor vendor, const char *request_type, obs_websocket_request_callback_function request_callback, void* priv_data) |
| 87 | +{ |
| 88 | + calldata_t cd = {0}; |
| 89 | + |
| 90 | + struct obs_websocket_request_callback cb = {}; |
| 91 | + cb.callback = request_callback; |
| 92 | + cb.priv_data = priv_data; |
| 93 | + |
| 94 | + calldata_set_string(&cd, "type", request_type); |
| 95 | + calldata_set_ptr(&cd, "callback", &cb); |
| 96 | + |
| 97 | + bool success = obs_websocket_run_simple_proc(vendor, "vendor_request_register", &cd); |
| 98 | + calldata_free(&cd); |
| 99 | + |
| 100 | + return success; |
| 101 | +} |
| 102 | + |
| 103 | +// Unregisters an existing vendor request |
| 104 | +static inline bool obs_websocket_vendor_unregister_request(obs_websocket_vendor vendor, const char *request_type) |
| 105 | +{ |
| 106 | + calldata_t cd = {0}; |
| 107 | + |
| 108 | + calldata_set_string(&cd, "type", request_type); |
| 109 | + |
| 110 | + bool success = obs_websocket_run_simple_proc(vendor, "vendor_request_unregister", &cd); |
| 111 | + calldata_free(&cd); |
| 112 | + |
| 113 | + return success; |
| 114 | +} |
| 115 | + |
| 116 | +// Does not affect event_data refcount. |
| 117 | +// Emits an event under the vendor's name |
| 118 | +static inline bool obs_websocket_vendor_emit_event(obs_websocket_vendor vendor, const char *event_name, obs_data_t *event_data) |
| 119 | +{ |
| 120 | + calldata_t cd = {0}; |
| 121 | + |
| 122 | + calldata_set_string(&cd, "type", event_name); |
| 123 | + calldata_set_ptr(&cd, "data", (void*)event_data); |
| 124 | + |
| 125 | + bool success = obs_websocket_run_simple_proc(vendor, "vendor_event_emit", &cd); |
| 126 | + calldata_free(&cd); |
| 127 | + |
| 128 | + return success; |
| 129 | +} |
| 130 | + |
| 131 | +#ifdef __cplusplus |
| 132 | +} |
| 133 | +#endif |
| 134 | + |
| 135 | +#endif |
0 commit comments