From cec94b042ee88d97b63c22c8fb63b49a17a09ae7 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sat, 28 Dec 2013 05:33:16 -0700 Subject: [PATCH] implement scene adding callbacks, make a few API tweaks --- libobs/callback/proc.c | 4 ++-- libobs/callback/proc.h | 2 +- libobs/callback/signal.c | 8 +++---- libobs/callback/signal.h | 4 ++-- libobs/obs-scene.c | 13 ++++++++++- libobs/obs-source.c | 6 ++--- libobs/obs.c | 6 +++++ libobs/obs.h | 7 ++++-- obs/window-main-basic.cpp | 46 ++++++++++++++++++++++++++++++++++----- obs/window-main-basic.hpp | 7 ++++-- test/osx/test.mm | 2 +- test/win/test.cpp | 2 +- 12 files changed, 83 insertions(+), 24 deletions(-) diff --git a/libobs/callback/proc.c b/libobs/callback/proc.c index 44aad370f..52e2ad83f 100644 --- a/libobs/callback/proc.c +++ b/libobs/callback/proc.c @@ -52,7 +52,7 @@ void proc_handler_destroy(proc_handler_t handler) } void proc_handler_add(proc_handler_t handler, const char *name, - void (*proc)(calldata_t, void*), void *data) + void (*proc)(void*, calldata_t), void *data) { struct proc_info pi = {bstrdup(name), data, proc}; da_push_back(handler->procs, &pi); @@ -65,7 +65,7 @@ bool proc_handler_call(proc_handler_t handler, const char *name, struct proc_info *info = handler->procs.array+i; if (strcmp(info->name, name) == 0) { - info->proc(params, info->data); + info->proc(info->data, params); return true; } } diff --git a/libobs/callback/proc.h b/libobs/callback/proc.h index 37f882b69..1bc1e17d1 100644 --- a/libobs/callback/proc.h +++ b/libobs/callback/proc.h @@ -39,7 +39,7 @@ EXPORT proc_handler_t proc_handler_create(void); EXPORT void proc_handler_destroy(proc_handler_t handler); EXPORT void proc_handler_add(proc_handler_t handler, const char *name, - void (*proc)(calldata_t, void*), void *data); + void (*proc)(void*, calldata_t), void *data); /** * Calls a function in a procedure handler. Returns false if the named diff --git a/libobs/callback/signal.c b/libobs/callback/signal.c index 8d388fd11..a5d86afe3 100644 --- a/libobs/callback/signal.c +++ b/libobs/callback/signal.c @@ -60,7 +60,7 @@ static inline void signal_info_destroy(struct signal_info *si) } static inline size_t signal_get_callback_idx(struct signal_info *si, - void (*callback)(calldata_t, void*), void *data) + void (*callback)(void*, calldata_t), void *data) { for (size_t i = 0; i < si->callbacks.num; i++) { struct signal_callback *sc = si->callbacks.array+i; @@ -129,7 +129,7 @@ void signal_handler_destroy(signal_handler_t handler) } void signal_handler_connect(signal_handler_t handler, const char *signal, - void (*callback)(calldata_t, void*), void *data) + void (*callback)(void*, calldata_t), void *data) { struct signal_info *sig, *last; struct signal_callback cb_data = {callback, data}; @@ -174,7 +174,7 @@ static inline struct signal_info *getsignal_locked(signal_handler_t handler, } void signal_handler_disconnect(signal_handler_t handler, const char *signal, - void (*callback)(calldata_t, void*), void *data) + void (*callback)(void*, calldata_t), void *data) { struct signal_info *sig = getsignal_locked(handler, signal); size_t idx; @@ -203,7 +203,7 @@ void signal_handler_signal(signal_handler_t handler, const char *signal, for (size_t i = 0; i < sig->callbacks.num; i++) { struct signal_callback *cb = sig->callbacks.array+i; - cb->callback(params, cb->data); + cb->callback(cb->data, params); } pthread_mutex_unlock(&sig->mutex); diff --git a/libobs/callback/signal.h b/libobs/callback/signal.h index c2e8317d8..33d28cfd1 100644 --- a/libobs/callback/signal.h +++ b/libobs/callback/signal.h @@ -38,9 +38,9 @@ EXPORT signal_handler_t signal_handler_create(void); EXPORT void signal_handler_destroy(signal_handler_t handler); EXPORT void signal_handler_connect(signal_handler_t handler, const char *signal, - void (*callback)(calldata_t, void*), void *data); + void (*callback)(void*, calldata_t), void *data); EXPORT void signal_handler_disconnect(signal_handler_t handler, - const char *signal, void (*callback)(calldata_t, void*), + const char *signal, void (*callback)(void*, calldata_t), void *data); EXPORT void signal_handler_signal(signal_handler_t handler, const char *signal, diff --git a/libobs/obs-scene.c b/libobs/obs-scene.c index 7b0fe72ba..7f0633f0c 100644 --- a/libobs/obs-scene.c +++ b/libobs/obs-scene.c @@ -97,7 +97,7 @@ static const struct source_info scene_info = .getheight = scene_getsize, }; -obs_scene_t obs_scene_create(void) +obs_scene_t obs_scene_create(const char *name) { struct obs_source *source = bmalloc(sizeof(struct obs_source)); struct obs_scene *scene = scene_create(NULL, source); @@ -110,6 +110,9 @@ obs_scene_t obs_scene_create(void) return NULL; } + source->name = bstrdup(name); + source->type = SOURCE_SCENE; + scene->source = source; obs_source_init(source, NULL, &scene_info); memcpy(&source->callbacks, &scene_info, sizeof(struct source_info)); @@ -127,6 +130,14 @@ obs_source_t obs_scene_getsource(obs_scene_t scene) return scene->source; } +obs_scene_t obs_scene_fromsource(obs_source_t source) +{ + if (source->type != SOURCE_SCENE) + return NULL; + + return source->data; +} + obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source) { struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item)); diff --git a/libobs/obs-source.c b/libobs/obs-source.c index 35d0728b6..b3b750452 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -954,11 +954,11 @@ void obs_source_setname(obs_source_t source, const char *name) source->name = bstrdup(name); } -void obs_source_getid(obs_source_t source, enum obs_source_type *type, +void obs_source_gettype(obs_source_t source, enum obs_source_type *type, const char **id) { - *type = source->type; - *id = source->callbacks.id; + if (type) *type = source->type; + if (id) *id = source->callbacks.id; } static inline void render_filter_bypass(obs_source_t target, effect_t effect, diff --git a/libobs/obs.c b/libobs/obs.c index fe955bb9b..43ac8687b 100644 --- a/libobs/obs.c +++ b/libobs/obs.c @@ -433,11 +433,17 @@ media_t obs_media(void) bool obs_add_source(obs_source_t source) { + struct calldata params = {0}; + pthread_mutex_lock(&obs->data.sources_mutex); da_push_back(obs->data.sources, &source); obs_source_addref(source); pthread_mutex_unlock(&obs->data.sources_mutex); + calldata_setptr(¶ms, "source", source); + signal_handler_signal(obs->signals, "source-add", ¶ms); + calldata_free(¶ms); + return true; } diff --git a/libobs/obs.h b/libobs/obs.h index 90249cce5..92a3ab630 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -398,7 +398,7 @@ EXPORT const char *obs_source_getname(obs_source_t source); EXPORT void obs_source_setname(obs_source_t source, const char *name); /** Gets the source type and identifier */ -EXPORT void obs_source_getid(obs_source_t source, enum obs_source_type *type, +EXPORT void obs_source_gettype(obs_source_t source, enum obs_source_type *type, const char **id); /** Returns the signal handler for a source */ @@ -444,12 +444,15 @@ EXPORT void obs_source_process_filter(obs_source_t filter, * A scene is a source which is a container of other sources with specific * display oriantations. Scenes can also be used like any other source. */ -EXPORT obs_scene_t obs_scene_create(void); +EXPORT obs_scene_t obs_scene_create(const char *name); EXPORT void obs_scene_destroy(obs_scene_t scene); /** Gets the scene's source context */ EXPORT obs_source_t obs_scene_getsource(obs_scene_t scene); +/** Gets the scene from its source, or NULL if not a scene */ +EXPORT obs_scene_t obs_scene_fromsource(obs_source_t source); + /** Adds/creates a new scene item for a source */ EXPORT obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source); diff --git a/obs/window-main-basic.cpp b/obs/window-main-basic.cpp index 633e60986..87c925726 100644 --- a/obs/window-main-basic.cpp +++ b/obs/window-main-basic.cpp @@ -22,6 +22,37 @@ #include "window-settings-basic.hpp" #include "window-main-basic.hpp" +void OBSBasic::SceneAdded(obs_source_t source) +{ + const char *name = obs_source_getname(source); + obs_scene_t scene = obs_scene_fromsource(source); + scenes->Append(wxString(name, wxConvUTF8), scene); +} + +void OBSBasic::SourceAdded(void *data, calldata_t params) +{ + OBSBasic *window = (OBSBasic*)data; + + obs_source_t source; + calldata_getptr(params, "source", (void**)&source); + + obs_source_type type; + obs_source_gettype(source, &type, NULL); + + if (type == SOURCE_SCENE) + window->SceneAdded(source); +} + +void OBSBasic::SourceDestroyed(void *data, calldata_t params) +{ + OBSBasic *window = (OBSBasic*)data; + obs_source_t source; + + calldata_getptr(params, "source", (void**)&source); + + /* TODO */ +} + bool OBSBasic::Init() { if (!obs_startup()) @@ -29,6 +60,16 @@ bool OBSBasic::Init() if (!InitGraphics()) return false; + signal_handler_connect(obs_signalhandler(), "source-add", + OBSBasic::SourceAdded, this); + signal_handler_connect(obs_signalhandler(), "source-destroy", + OBSBasic::SourceDestroyed, this); + + //obs_scene_t scene = obs_scene_create("test scene"); + //obs_add_source(obs_scene_getsource(scene)); + + //obs_load_module("test-input"); + return true; } @@ -67,11 +108,6 @@ bool OBSBasic::InitGraphics() return true; } -bool OBSBasic::AddScene(const char *name) -{ - return false; -} - void OBSBasic::OnClose(wxCloseEvent &event) { wxGetApp().ExitMainLoop(); diff --git a/obs/window-main-basic.hpp b/obs/window-main-basic.hpp index 1dfecd0c4..7d33b0eb5 100644 --- a/obs/window-main-basic.hpp +++ b/obs/window-main-basic.hpp @@ -25,6 +25,11 @@ using namespace std; class OBSBasic : public OBSBasicBase { + void SceneAdded(obs_source_t scene); + + static void SourceAdded(void *data, calldata_t params); + static void SourceDestroyed(void *data, calldata_t params); + bool InitGraphics(); void NewProject(); @@ -60,8 +65,6 @@ public: bool Init(); - bool AddScene(const char *name); - inline wxPanel *GetPreviewPanel() {return previewPanel;} inline wxSizer *GetPreviewContainer() {return previewContainer;} }; diff --git a/test/osx/test.mm b/test/osx/test.mm index e4408c623..a71e46194 100644 --- a/test/osx/test.mm +++ b/test/osx/test.mm @@ -163,7 +163,7 @@ static void test() /* ------------------------------------------------------ */ /* create scene and add source to scene (twice) */ - SceneContext scene{obs_scene_create()}; + SceneContext scene{obs_scene_create("test scene")}; if (!scene) throw "Couldn't create scene"; diff --git a/test/win/test.cpp b/test/win/test.cpp index 380de4bd7..a263002ae 100644 --- a/test/win/test.cpp +++ b/test/win/test.cpp @@ -167,7 +167,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, /* ------------------------------------------------------ */ /* create scene and add source to scene (twice) */ - SceneContext scene = obs_scene_create(); + SceneContext scene = obs_scene_create("test scene"); if (!scene) throw "Couldn't create scene";