0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 13:08:50 +02:00
obs-studio/libobs/obs.c

899 lines
21 KiB
C
Raw Normal View History

2013-10-01 04:37:13 +02:00
/******************************************************************************
Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
2013-10-01 04:37:13 +02:00
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-10-01 04:37:13 +02: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 "callback/calldata.h"
2013-10-01 04:37:13 +02:00
#include "obs.h"
#include "obs-internal.h"
2013-10-01 04:37:13 +02:00
struct obs_core *obs = NULL;
extern char *find_libobs_data_file(const char *file);
static inline void make_gs_init_data(struct gs_init_data *gid,
struct obs_video_info *ovi)
2013-10-01 04:37:13 +02:00
{
memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
gid->cx = ovi->window_width;
gid->cy = ovi->window_height;
gid->num_backbuffers = 2;
gid->format = GS_RGBA;
gid->zsformat = GS_ZS_NONE;
gid->adapter = ovi->adapter;
}
static inline void make_video_info(struct video_output_info *vi,
struct obs_video_info *ovi)
{
vi->name = "video";
vi->format = ovi->output_format;
vi->fps_num = ovi->fps_num;
vi->fps_den = ovi->fps_den;
vi->width = ovi->output_width;
vi->height = ovi->output_height;
}
#define PIXEL_SIZE 4
#define GET_ALIGN(val, align) \
(((val) + (align-1)) & ~(align-1))
static inline void set_420p_sizes(const struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
uint32_t chroma_pixels;
uint32_t total_bytes;
chroma_pixels = (ovi->output_width * ovi->output_height / 4);
chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
video->plane_offsets[0] = 0;
video->plane_offsets[1] = ovi->output_width * ovi->output_height;
video->plane_offsets[2] = video->plane_offsets[1] + chroma_pixels;
video->plane_linewidth[0] = ovi->output_width;
video->plane_linewidth[1] = ovi->output_width/2;
video->plane_linewidth[2] = ovi->output_width/2;
video->plane_sizes[0] = video->plane_offsets[1];
video->plane_sizes[1] = video->plane_sizes[0]/4;
video->plane_sizes[2] = video->plane_sizes[1];
total_bytes = video->plane_offsets[2] + chroma_pixels;
video->conversion_height =
(total_bytes/PIXEL_SIZE + ovi->output_width-1) /
ovi->output_width;
video->conversion_height = GET_ALIGN(video->conversion_height, 2);
video->conversion_tech = "Planar420";
}
static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
{
obs->video.conversion_height = 0;
memset(obs->video.plane_offsets, 0, sizeof(obs->video.plane_offsets));
memset(obs->video.plane_sizes, 0, sizeof(obs->video.plane_sizes));
memset(obs->video.plane_linewidth, 0,
sizeof(obs->video.plane_linewidth));
switch ((uint32_t)ovi->output_format) {
case VIDEO_FORMAT_I420:
set_420p_sizes(ovi);
}
}
static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
calc_gpu_conversion_sizes(ovi);
if (!video->conversion_height) {
blog(LOG_INFO, "GPU conversion not available for format: %u",
(unsigned int)ovi->output_format);
video->gpu_conversion = false;
return true;
}
for (size_t i = 0; i < NUM_TEXTURES; i++) {
video->convert_textures[i] = gs_create_texture(
ovi->output_width, video->conversion_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->convert_textures[i])
return false;
}
return true;
}
static bool obs_init_textures(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
bool yuv = format_is_yuv(ovi->output_format);
uint32_t output_height = video->gpu_conversion ?
video->conversion_height : ovi->output_height;
size_t i;
for (i = 0; i < NUM_TEXTURES; i++) {
video->copy_surfaces[i] = gs_create_stagesurface(
ovi->output_width, output_height, GS_RGBA);
if (!video->copy_surfaces[i])
return false;
video->render_textures[i] = gs_create_texture(
ovi->base_width, ovi->base_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->render_textures[i])
return false;
video->output_textures[i] = gs_create_texture(
ovi->output_width, ovi->output_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->output_textures[i])
return false;
if (yuv)
source_frame_init(&video->convert_frames[i],
ovi->output_format,
ovi->output_width, ovi->output_height);
}
return true;
}
static bool obs_init_graphics(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
struct gs_init_data graphics_data;
bool success = true;
int errorcode;
2013-10-01 04:37:13 +02:00
make_gs_init_data(&graphics_data, ovi);
errorcode = gs_create(&video->graphics, ovi->graphics_module,
&graphics_data);
2013-10-01 04:37:13 +02:00
if (errorcode != GS_SUCCESS) {
if (errorcode == GS_ERROR_MODULENOTFOUND)
blog(LOG_ERROR, "Could not find graphics module '%s'",
ovi->graphics_module);
2013-10-01 04:37:13 +02:00
return false;
}
gs_entercontext(video->graphics);
2013-10-01 04:37:13 +02:00
if (success) {
char *filename = find_libobs_data_file("default.effect");
video->default_effect = gs_create_effect_from_file(filename,
NULL);
bfree(filename);
filename = find_libobs_data_file("format_conversion.effect");
video->conversion_effect = gs_create_effect_from_file(filename,
NULL);
bfree(filename);
if (!video->default_effect)
success = false;
if (!video->conversion_effect)
success = false;
2013-10-01 04:37:13 +02:00
}
gs_leavecontext();
return success;
2013-10-01 04:37:13 +02:00
}
static bool obs_init_video(struct obs_video_info *ovi)
2013-10-01 04:37:13 +02:00
{
struct obs_core_video *video = &obs->video;
struct video_output_info vi;
int errorcode;
make_video_info(&vi, ovi);
video->base_width = ovi->base_width;
video->base_height = ovi->base_height;
video->output_width = ovi->output_width;
video->output_height = ovi->output_height;
video->gpu_conversion = ovi->gpu_conversion;
errorcode = video_output_open(&video->video, &vi);
if (errorcode != VIDEO_OUTPUT_SUCCESS) {
if (errorcode == VIDEO_OUTPUT_INVALIDPARAM)
blog(LOG_ERROR, "Invalid video parameters specified");
else
blog(LOG_ERROR, "Could not open video output");
2013-10-01 04:37:13 +02:00
return false;
}
if (!obs_display_init(&video->main_display, NULL))
return false;
video->main_display.cx = ovi->window_width;
video->main_display.cy = ovi->window_height;
gs_entercontext(video->graphics);
if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
return false;
if (!obs_init_textures(ovi))
return false;
gs_leavecontext();
errorcode = pthread_create(&video->video_thread, NULL,
obs_video_thread, obs);
if (errorcode != 0)
2013-10-01 04:37:13 +02:00
return false;
video->thread_initialized = true;
2013-10-01 04:37:13 +02:00
return true;
}
static void stop_video(void)
2013-10-01 04:37:13 +02:00
{
struct obs_core_video *video = &obs->video;
void *thread_retval;
2013-10-01 04:37:13 +02:00
if (video->video) {
video_output_stop(video->video);
if (video->thread_initialized) {
pthread_join(video->video_thread, &thread_retval);
video->thread_initialized = false;
}
}
}
static void obs_free_video(void)
{
struct obs_core_video *video = &obs->video;
if (video->video) {
obs_display_free(&video->main_display);
video_output_close(video->video);
video->video = NULL;
if (!video->graphics)
return;
gs_entercontext(video->graphics);
if (video->mapped_surface) {
stagesurface_unmap(video->mapped_surface);
video->mapped_surface = NULL;
}
for (size_t i = 0; i < NUM_TEXTURES; i++) {
stagesurface_destroy(video->copy_surfaces[i]);
texture_destroy(video->render_textures[i]);
texture_destroy(video->convert_textures[i]);
texture_destroy(video->output_textures[i]);
source_frame_free(&video->convert_frames[i]);
video->copy_surfaces[i] = NULL;
video->render_textures[i] = NULL;
video->convert_textures[i] = NULL;
video->output_textures[i] = NULL;
}
gs_leavecontext();
video->cur_texture = 0;
}
}
static void obs_free_graphics(void)
{
struct obs_core_video *video = &obs->video;
if (video->graphics) {
gs_entercontext(video->graphics);
effect_destroy(video->default_effect);
effect_destroy(video->conversion_effect);
video->default_effect = NULL;
gs_leavecontext();
gs_destroy(video->graphics);
video->graphics = NULL;
}
2013-10-01 04:37:13 +02:00
}
static bool obs_init_audio(struct audio_output_info *ai)
2013-10-01 04:37:13 +02:00
{
struct obs_core_audio *audio = &obs->audio;
int errorcode;
2013-10-01 04:37:13 +02:00
/* TODO: sound subsystem */
2013-10-01 04:37:13 +02:00
audio->user_volume = 1.0f;
audio->present_volume = 1.0f;
errorcode = audio_output_open(&audio->audio, ai);
if (errorcode == AUDIO_OUTPUT_SUCCESS)
return true;
2013-11-20 23:11:31 +01:00
else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
blog(LOG_ERROR, "Invalid audio parameters specified");
else
blog(LOG_ERROR, "Could not open audio output");
return false;
}
static void obs_free_audio(void)
{
struct obs_core_audio *audio = &obs->audio;
if (audio->audio)
audio_output_close(audio->audio);
memset(audio, 0, sizeof(struct obs_core_audio));
}
static bool obs_init_data(void)
{
struct obs_core_data *data = &obs->data;
pthread_mutexattr_t attr;
pthread_mutex_init_value(&obs->data.displays_mutex);
if (pthread_mutexattr_init(&attr) != 0)
return false;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
goto fail;
if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
goto fail;
if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
goto fail;
if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
goto fail;
if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
goto fail;
if (!obs_view_init(&data->main_view))
goto fail;
2013-10-01 04:37:13 +02:00
data->valid = true;
fail:
pthread_mutexattr_destroy(&attr);
return data->valid;
2013-10-01 04:37:13 +02:00
}
static void obs_free_data(void)
2013-10-01 04:37:13 +02:00
{
struct obs_core_data *data = &obs->data;
2013-11-22 18:02:57 +01:00
uint32_t i;
2013-10-01 04:37:13 +02:00
data->valid = false;
obs_view_free(&data->main_view);
while (data->outputs.num)
obs_output_destroy(data->outputs.array[0]);
while (data->encoders.num)
obs_encoder_destroy(data->encoders.array[0]);
while (data->displays.num)
obs_display_destroy(data->displays.array[0]);
pthread_mutex_lock(&obs->data.sources_mutex);
for (i = 0; i < data->sources.num; i++)
obs_source_release(data->sources.array[i]);
da_free(data->sources);
pthread_mutex_unlock(&obs->data.sources_mutex);
pthread_mutex_destroy(&data->sources_mutex);
pthread_mutex_destroy(&data->displays_mutex);
pthread_mutex_destroy(&data->outputs_mutex);
pthread_mutex_destroy(&data->encoders_mutex);
}
2013-10-01 04:37:13 +02:00
static const char *obs_signals[] = {
"void source_create(ptr source)",
"void source_destroy(ptr source)",
"void source_add(ptr source)",
"void source_remove(ptr source)",
"void source_activate(ptr source)",
"void source_deactivate(ptr source)",
"void source_show(ptr source)",
"void source_hide(ptr source)",
"void source_volume(ptr source, in out float volume)",
"void channel_change(int channel, in out ptr source, ptr prev_source)",
"void master_volume(in out float volume)",
NULL
};
static inline bool obs_init_handlers(void)
{
obs->signals = signal_handler_create();
if (!obs->signals)
return false;
obs->procs = proc_handler_create();
if (!obs->procs)
return false;
return signal_handler_add_array(obs->signals, obs_signals);
}
static bool obs_init(void)
{
obs = bzalloc(sizeof(struct obs_core));
obs_init_data();
return obs_init_handlers();
2013-10-01 04:37:13 +02:00
}
bool obs_startup(void)
2013-10-01 04:37:13 +02:00
{
bool success;
2013-10-01 04:37:13 +02:00
if (obs) {
2014-02-24 06:39:33 +01:00
blog(LOG_WARNING, "Tried to call obs_startup more than once");
return false;
}
success = obs_init();
if (!success)
obs_shutdown();
return success;
2013-10-01 04:37:13 +02:00
}
void obs_shutdown(void)
2013-10-01 04:37:13 +02:00
{
if (!obs)
return;
da_free(obs->input_types);
da_free(obs->filter_types);
da_free(obs->transition_types);
da_free(obs->output_types);
da_free(obs->service_types);
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 16:04:50 +01:00
da_free(obs->modal_ui_callbacks);
da_free(obs->modeless_ui_callbacks);
2013-10-01 04:37:13 +02:00
stop_video();
obs_free_data();
obs_free_video();
obs_free_graphics();
obs_free_audio();
proc_handler_destroy(obs->procs);
signal_handler_destroy(obs->signals);
2013-10-01 04:37:13 +02:00
2014-02-24 06:39:33 +01:00
for (size_t i = 0; i < obs->modules.num; i++)
2013-10-01 04:37:13 +02:00
free_module(obs->modules.array+i);
da_free(obs->modules);
bfree(obs);
obs = NULL;
}
bool obs_initialized(void)
{
return obs != NULL;
}
bool obs_reset_video(struct obs_video_info *ovi)
{
if (!obs) return false;
/* don't allow changing of video settings if active. */
if (obs->video.video && video_output_active(obs->video.video))
return false;
struct obs_core_video *video = &obs->video;
/* align to multiple-of-two and SSE alignment sizes */
ovi->output_width &= 0xFFFFFFFC;
ovi->output_height &= 0xFFFFFFFE;
stop_video();
obs_free_video();
if (!ovi) {
obs_free_graphics();
return true;
}
if (!video->graphics && !obs_init_graphics(ovi))
return false;
return obs_init_video(ovi);
}
bool obs_reset_audio(struct audio_output_info *ai)
2013-10-01 04:37:13 +02:00
{
if (!obs) return false;
/* don't allow changing of audio settings if active. */
if (obs->audio.audio && audio_output_active(obs->audio.audio))
return false;
obs_free_audio();
if(!ai)
return true;
return obs_init_audio(ai);
2013-10-01 04:37:13 +02:00
}
bool obs_get_video_info(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
const struct video_output_info *info;
if (!obs || !video->graphics)
return false;
info = video_output_getinfo(video->video);
memset(ovi, 0, sizeof(struct obs_video_info));
ovi->base_width = video->base_width;
ovi->base_height = video->base_height;
ovi->output_width = info->width;
ovi->output_height = info->height;
ovi->output_format = info->format;
ovi->fps_num = info->fps_num;
ovi->fps_den = info->fps_den;
return true;
}
bool obs_get_audio_info(struct audio_output_info *aoi)
{
struct obs_core_audio *audio = &obs->audio;
const struct audio_output_info *info;
if (!obs || !audio->audio)
return false;
info = audio_output_getinfo(audio->audio);
memcpy(aoi, info, sizeof(struct audio_output_info));
return true;
}
bool obs_enum_input_types(size_t idx, const char **id)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
if (!obs) return false;
2013-10-01 04:37:13 +02:00
if (idx >= obs->input_types.num)
return false;
*id = obs->input_types.array[idx].id;
2013-10-01 04:37:13 +02:00
return true;
}
bool obs_enum_filter_types(size_t idx, const char **id)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
if (!obs) return false;
2013-10-01 04:37:13 +02:00
if (idx >= obs->filter_types.num)
return false;
*id = obs->filter_types.array[idx].id;
2013-10-01 04:37:13 +02:00
return true;
}
bool obs_enum_transition_types(size_t idx, const char **id)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
if (!obs) return false;
2013-10-01 04:37:13 +02:00
if (idx >= obs->transition_types.num)
return false;
*id = obs->transition_types.array[idx].id;
2013-10-01 04:37:13 +02:00
return true;
}
bool obs_enum_output_types(size_t idx, const char **id)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
if (!obs) return false;
2013-10-01 04:37:13 +02:00
if (idx >= obs->output_types.num)
return false;
*id = obs->output_types.array[idx].id;
2013-10-01 04:37:13 +02:00
return true;
}
graphics_t obs_graphics(void)
2013-10-01 04:37:13 +02:00
{
return (obs != NULL) ? obs->video.graphics : NULL;
2013-10-01 04:37:13 +02:00
}
audio_t obs_audio(void)
{
return (obs != NULL) ? obs->audio.audio : NULL;
}
video_t obs_video(void)
2013-10-01 04:37:13 +02:00
{
return (obs != NULL) ? obs->video.video : NULL;
2013-10-01 04:37:13 +02:00
}
/* TODO: optimize this later so it's not just O(N) string lookups */
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 16:04:50 +01:00
static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
const char *task, const char *target)
{
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 16:04:50 +01:00
for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
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 16:04:50 +01:00
if (strcmp(callback->id, id) == 0 &&
strcmp(callback->task, task) == 0 &&
strcmp(callback->target, target) == 0)
return callback;
}
return NULL;
}
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 16:04:50 +01:00
static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
const char *task, const char *target)
{
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 16:04:50 +01:00
for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
struct obs_modeless_ui *callback;
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 16:04:50 +01:00
callback = obs->modeless_ui_callbacks.array+i;
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 16:04:50 +01:00
if (strcmp(callback->id, id) == 0 &&
strcmp(callback->task, task) == 0 &&
strcmp(callback->target, target) == 0)
return callback;
}
return NULL;
}
int obs_exec_ui(const char *name, const char *task, const char *target,
void *data, void *ui_data)
{
struct obs_modal_ui *callback;
int errorcode = OBS_UI_NOTFOUND;
2014-02-24 06:39:33 +01:00
if (!obs) return errorcode;
callback = get_modal_ui_callback(name, task, target);
if (callback) {
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 16:04:50 +01:00
bool success = callback->exec(data, ui_data);
errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
}
return errorcode;
}
void *obs_create_ui(const char *name, const char *task, const char *target,
void *data, void *ui_data)
{
struct obs_modeless_ui *callback;
2014-02-24 06:39:33 +01:00
if (!obs) return NULL;
callback = get_modeless_ui_callback(name, task, target);
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 16:04:50 +01:00
return callback ? callback->create(data, ui_data) : NULL;
}
bool obs_add_source(obs_source_t source)
{
struct calldata params = {0};
2014-02-24 06:39:33 +01:00
if (!obs) return false;
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(&params, "source", source);
signal_handler_signal(obs->signals, "source_add", &params);
calldata_free(&params);
return true;
}
obs_source_t obs_get_output_source(uint32_t channel)
{
2014-02-24 06:39:33 +01:00
if (!obs) return NULL;
return obs_view_getsource(&obs->data.main_view, channel);
2013-10-01 04:37:13 +02:00
}
void obs_set_output_source(uint32_t channel, obs_source_t source)
2013-10-01 04:37:13 +02:00
{
assert(channel < MAX_CHANNELS);
if (!obs) return;
if (channel >= MAX_CHANNELS) return;
struct obs_source *prev_source;
struct obs_view *view = &obs->data.main_view;
struct calldata params = {0};
pthread_mutex_lock(&view->channels_mutex);
prev_source = view->channels[channel];
calldata_setint(&params, "channel", channel);
calldata_setptr(&params, "prev_source", prev_source);
calldata_setptr(&params, "source", source);
signal_handler_signal(obs->signals, "channel_change", &params);
calldata_getptr(&params, "source", &source);
calldata_free(&params);
view->channels[channel] = source;
if (source) {
obs_source_addref(source);
obs_source_activate(source, MAIN_VIEW);
}
if (prev_source) {
obs_source_deactivate(prev_source, MAIN_VIEW);
obs_source_release(prev_source);
}
pthread_mutex_unlock(&view->channels_mutex);
2013-10-01 04:37:13 +02:00
}
void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t), void *param)
{
struct obs_core_data *data = &obs->data;
2014-02-24 06:39:33 +01:00
if (!obs) return;
pthread_mutex_lock(&data->outputs_mutex);
for (size_t i = 0; i < data->outputs.num; i++)
if (!enum_proc(param, data->outputs.array[i]))
break;
pthread_mutex_unlock(&data->outputs_mutex);
}
void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t), void *param)
{
struct obs_core_data *data = &obs->data;
2014-02-24 06:39:33 +01:00
if (!obs) return;
pthread_mutex_lock(&data->encoders_mutex);
for (size_t i = 0; i < data->encoders.num; i++)
if (!enum_proc(param, data->encoders.array[i]))
break;
pthread_mutex_unlock(&data->encoders_mutex);
}
void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t), void *param)
{
struct obs_core_data *data = &obs->data;
2014-02-24 06:39:33 +01:00
if (!obs) return;
pthread_mutex_lock(&data->sources_mutex);
for (size_t i = 0; i < data->sources.num; i++)
if (!enum_proc(param, data->sources.array[i]))
break;
pthread_mutex_unlock(&data->sources_mutex);
}
obs_source_t obs_get_source_by_name(const char *name)
{
struct obs_core_data *data = &obs->data;
struct obs_source *source = NULL;
size_t i;
2014-02-24 06:39:33 +01:00
if (!obs) return NULL;
pthread_mutex_lock(&data->sources_mutex);
for (i = 0; i < data->sources.num; i++) {
struct obs_source *cur_source = data->sources.array[i];
if (strcmp(cur_source->name, name) == 0) {
source = cur_source;
obs_source_addref(source);
break;
}
}
pthread_mutex_unlock(&data->sources_mutex);
return source;
}
effect_t obs_get_default_effect(void)
{
if (!obs) return NULL;
return obs->video.default_effect;
}
signal_handler_t obs_signalhandler(void)
{
if (!obs) return NULL;
return obs->signals;
}
proc_handler_t obs_prochandler(void)
{
if (!obs) return NULL;
return obs->procs;
}
void obs_add_draw_callback(
void (*draw)(void *param, uint32_t cx, uint32_t cy),
void *param)
{
if (!obs) return;
obs_display_add_draw_callback(&obs->video.main_display, draw, param);
}
void obs_remove_draw_callback(
void (*draw)(void *param, uint32_t cx, uint32_t cy),
void *param)
{
if (!obs) return;
obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
}
void obs_resize(uint32_t cx, uint32_t cy)
{
if (!obs || !obs->video.video || !obs->video.graphics) return;
obs_display_resize(&obs->video.main_display, cx, cy);
}
void obs_render_main_view(void)
{
if (!obs) return;
obs_view_render(&obs->data.main_view);
}
void obs_set_master_volume(float volume)
{
2014-02-22 01:51:16 +01:00
struct calldata data = {0};
2014-02-24 06:39:33 +01:00
if (!obs) return;
2014-02-22 01:51:16 +01:00
calldata_setfloat(&data, "volume", volume);
signal_handler_signal(obs->signals, "master_volume", &data);
volume = (float)calldata_float(&data, "volume");
2014-02-22 01:51:16 +01:00
calldata_free(&data);
obs->audio.user_volume = volume;
}
void obs_set_present_volume(float volume)
{
if (!obs) return;
obs->audio.present_volume = volume;
}
float obs_get_master_volume(void)
{
return obs ? obs->audio.user_volume : 0.0f;
}
float obs_get_present_volume(void)
{
return obs ? obs->audio.present_volume : 0.0f;
}