obs-studio/libobs/obs-scene.c

496 lines
10 KiB
C
Raw Normal View History

2013-09-30 19:37:13 -07:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
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
2013-09-30 19:37:13 -07:00
(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, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "graphics/math-defs.h"
#include "obs-scene.h"
static inline void signal_item_remove(struct obs_scene_item *item)
{
struct calldata params = {0};
calldata_setptr(&params, "scene", item->parent);
calldata_setptr(&params, "item", item);
signal_handler_signal(item->parent->source->signals, "item-remove",
&params);
calldata_free(&params);
}
static const char *scene_getname(const char *locale)
{
UNUSED_PARAMETER(locale);
return "Scene internal source type";
}
static void *scene_create(obs_data_t settings, struct obs_source *source)
2013-09-30 19:37:13 -07:00
{
pthread_mutexattr_t attr;
2013-09-30 19:37:13 -07:00
struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
scene->source = source;
scene->first_item = NULL;
if (pthread_mutexattr_init(&attr) != 0)
goto fail;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
goto fail;
if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
goto fail;
}
2013-09-30 19:37:13 -07:00
UNUSED_PARAMETER(settings);
2013-09-30 19:37:13 -07:00
return scene;
fail:
pthread_mutexattr_destroy(&attr);
bfree(scene);
return NULL;
2013-09-30 19:37:13 -07:00
}
static void scene_destroy(void *data)
2013-09-30 19:37:13 -07:00
{
struct obs_scene *scene = data;
2014-01-30 11:41:11 -08:00
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
2013-09-30 19:37:13 -07:00
2014-01-30 11:41:11 -08:00
item = scene->first_item;
while (item) {
struct obs_scene_item *del_item = item;
item = item->next;
obs_sceneitem_remove(del_item);
}
2013-09-30 19:37:13 -07:00
pthread_mutex_unlock(&scene->mutex);
pthread_mutex_destroy(&scene->mutex);
2013-09-30 19:37:13 -07:00
bfree(scene);
}
static void scene_enum_sources(void *data,
obs_source_enum_proc_t enum_callback,
void *param)
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
struct obs_scene_item *next = item->next;
obs_sceneitem_addref(item);
enum_callback(scene->source, item->source, param);
obs_sceneitem_release(item);
item = next;
}
pthread_mutex_unlock(&scene->mutex);
}
static inline void detach_sceneitem(struct obs_scene_item *item)
{
if (item->prev)
item->prev->next = item->next;
else
item->parent->first_item = item->next;
if (item->next)
item->next->prev = item->prev;
item->parent = NULL;
}
static inline void attach_sceneitem(struct obs_scene_item *item,
struct obs_scene_item *prev)
{
item->prev = prev;
if (prev) {
item->next = prev->next;
if (prev->next)
prev->next->prev = item;
prev->next = item;
} else {
item->next = item->parent->first_item;
item->parent->first_item = item;
}
}
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
static void scene_video_render(void *data, effect_t effect)
2013-09-30 19:37:13 -07:00
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
2013-09-30 19:37:13 -07:00
while (item) {
if (obs_source_removed(item->source)) {
struct obs_scene_item *del_item = item;
item = item->next;
obs_sceneitem_remove(del_item);
continue;
}
2013-09-30 19:37:13 -07:00
gs_matrix_push();
gs_matrix_translate3f(item->origin.x, item->origin.y, 0.0f);
gs_matrix_scale3f(item->scale.x, item->scale.y, 1.0f);
gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(-item->rot));
gs_matrix_translate3f(-item->pos.x, -item->pos.y, 0.0f);
obs_source_video_render(item->source);
2013-09-30 19:37:13 -07:00
gs_matrix_pop();
item = item->next;
2013-09-30 19:37:13 -07:00
}
pthread_mutex_unlock(&scene->mutex);
UNUSED_PARAMETER(effect);
}
static uint32_t scene_getwidth(void *data)
{
UNUSED_PARAMETER(data);
return obs->video.base_width;
2013-09-30 19:37:13 -07:00
}
static uint32_t scene_getheight(void *data)
2013-09-30 19:37:13 -07:00
{
UNUSED_PARAMETER(data);
return obs->video.base_height;
2013-09-30 19:37:13 -07:00
}
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
static const struct obs_source_info scene_info =
2013-09-30 19:37:13 -07:00
{
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
.id = "scene",
.type = OBS_SOURCE_TYPE_SCENE,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
.getname = scene_getname,
.create = scene_create,
.destroy = scene_destroy,
.video_render = scene_video_render,
.getwidth = scene_getwidth,
.getheight = scene_getheight,
.enum_sources = scene_enum_sources
2013-09-30 19:37:13 -07:00
};
obs_scene_t obs_scene_create(const char *name)
2013-09-30 19:37:13 -07:00
{
struct obs_source *source = bzalloc(sizeof(struct obs_source));
struct obs_scene *scene;
2013-09-30 19:37:13 -07:00
if (!obs_source_init_handlers(source)) {
bfree(source);
return NULL;
}
source->settings = obs_data_create();
scene = scene_create(source->settings, source);
2013-09-30 19:37:13 -07:00
source->data = scene;
assert(scene);
if (!scene) {
obs_data_release(source->settings);
2013-09-30 19:37:13 -07:00
bfree(source);
return NULL;
}
source->name = bstrdup(name);
2013-09-30 19:37:13 -07:00
scene->source = source;
obs_source_init(source, &scene_info);
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
memcpy(&source->info, &scene_info, sizeof(struct obs_source_info));
2013-09-30 19:37:13 -07:00
return scene;
}
void obs_scene_addref(obs_scene_t scene)
{
if (scene)
obs_source_addref(scene->source);
}
void obs_scene_release(obs_scene_t scene)
2013-09-30 19:37:13 -07:00
{
if (scene)
obs_source_release(scene->source);
2013-09-30 19:37:13 -07:00
}
obs_source_t obs_scene_getsource(obs_scene_t scene)
2013-09-30 19:37:13 -07:00
{
return scene ? scene->source : NULL;
2013-09-30 19:37:13 -07:00
}
obs_scene_t obs_scene_fromsource(obs_source_t source)
{
2014-02-23 21:39:33 -08:00
if (!source || source->info.type != OBS_SOURCE_TYPE_SCENE)
return NULL;
return source->data;
}
obs_sceneitem_t obs_scene_findsource(obs_scene_t scene, const char *name)
{
struct obs_scene_item *item;
2014-02-23 21:39:33 -08:00
if (!scene)
return NULL;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
if (strcmp(item->source->name, name) == 0)
break;
item = item->next;
}
pthread_mutex_unlock(&scene->mutex);
return item;
}
void obs_scene_enum_items(obs_scene_t scene,
bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
void *param)
{
struct obs_scene_item *item;
2014-02-23 21:39:33 -08:00
if (!scene || !callback)
return;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
struct obs_scene_item *next = item->next;
obs_sceneitem_addref(item);
if (!callback(scene, item, param))
break;
obs_sceneitem_release(item);
item = next;
}
pthread_mutex_unlock(&scene->mutex);
}
obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
2013-09-30 19:37:13 -07:00
{
struct obs_scene_item *last;
struct obs_scene_item *item;
struct calldata params = {0};
if (!scene)
return NULL;
if (!source) {
blog(LOG_WARNING, "Tried to add a NULL source to a scene");
return NULL;
}
item = bzalloc(sizeof(struct obs_scene_item));
2013-09-30 19:37:13 -07:00
item->source = source;
item->visible = true;
item->parent = scene;
item->ref = 1;
2013-09-30 19:37:13 -07:00
vec2_set(&item->scale, 1.0f, 1.0f);
obs_source_addref(source);
obs_source_add_child(scene->source, source);
pthread_mutex_lock(&scene->mutex);
last = scene->first_item;
if (!last) {
scene->first_item = item;
} else {
while (last->next)
last = last->next;
last->next = item;
item->prev = last;
}
pthread_mutex_unlock(&scene->mutex);
calldata_setptr(&params, "scene", scene);
calldata_setptr(&params, "item", item);
signal_handler_signal(scene->source->signals, "item-add", &params);
calldata_free(&params);
2013-09-30 19:37:13 -07:00
return item;
}
static void obs_sceneitem_destroy(obs_sceneitem_t item)
2013-09-30 19:37:13 -07:00
{
if (item) {
if (item->source)
obs_source_release(item->source);
2013-09-30 19:37:13 -07:00
bfree(item);
}
}
void obs_sceneitem_addref(obs_sceneitem_t item)
{
if (item)
++item->ref;
}
void obs_sceneitem_release(obs_sceneitem_t item)
{
if (!item)
return;
if (--item->ref == 0)
obs_sceneitem_destroy(item);
}
void obs_sceneitem_remove(obs_sceneitem_t item)
{
obs_scene_t scene;
if (!item)
return;
scene = item->parent;
if (scene)
pthread_mutex_lock(&scene->mutex);
if (item->removed) {
if (scene)
pthread_mutex_unlock(&scene->mutex);
return;
}
item->removed = true;
obs_source_remove_child(scene->source, item->source);
signal_item_remove(item);
detach_sceneitem(item);
pthread_mutex_unlock(&scene->mutex);
obs_sceneitem_release(item);
}
obs_scene_t obs_sceneitem_getscene(obs_sceneitem_t item)
{
2014-02-23 21:39:33 -08:00
return item ? item->parent : NULL;
}
obs_source_t obs_sceneitem_getsource(obs_sceneitem_t item)
{
2014-02-23 21:39:33 -08:00
return item ? item->source : NULL;
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(&item->pos, pos);
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
item->rot = rot;
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_setorigin(obs_sceneitem_t item, const struct vec2 *origin)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(&item->origin, origin);
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(&item->scale, scale);
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (!item) return;
2013-09-30 19:37:13 -07:00
struct obs_scene *scene = item->parent;
obs_scene_addref(scene);
pthread_mutex_lock(&scene->mutex);
detach_sceneitem(item);
2013-09-30 19:37:13 -07:00
if (movement == ORDER_MOVE_UP) {
attach_sceneitem(item, item->prev);
2013-09-30 19:37:13 -07:00
} else if (movement == ORDER_MOVE_DOWN) {
attach_sceneitem(item, item->next);
2013-09-30 19:37:13 -07:00
} else if (movement == ORDER_MOVE_TOP) {
struct obs_scene_item *last = item->next;
if (!last) {
last = item->prev;
} else {
while (last->next)
last = last->next;
}
2013-09-30 19:37:13 -07:00
attach_sceneitem(item, last);
} else if (movement == ORDER_MOVE_BOTTOM) {
attach_sceneitem(item, NULL);
2013-09-30 19:37:13 -07:00
}
pthread_mutex_unlock(&scene->mutex);
obs_scene_release(scene);
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(pos, &item->pos);
2013-09-30 19:37:13 -07:00
}
float obs_sceneitem_getrot(obs_sceneitem_t item)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
return item ? item->rot : 0.0f;
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_getorigin(obs_sceneitem_t item, struct vec2 *origin)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(origin, &item->origin);
2013-09-30 19:37:13 -07:00
}
void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
2013-09-30 19:37:13 -07:00
{
2014-02-23 21:39:33 -08:00
if (item)
vec2_copy(scale, &item->scale);
2013-09-30 19:37:13 -07:00
}