diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index 6363cb4ec..81255b16e 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -44,6 +44,11 @@ static inline int64_t packet_dts_usec(struct encoder_packet *packet) return packet->dts * MICROSECOND_DEN / packet->timebase_den; } +struct tick_callback { + void (*tick)(void *param, float seconds); + void *param; +}; + struct draw_callback { void (*draw)(void *param, uint32_t cx, uint32_t cy); void *param; @@ -319,6 +324,7 @@ struct obs_core_data { pthread_mutex_t audio_sources_mutex; pthread_mutex_t draw_callbacks_mutex; DARRAY(struct draw_callback) draw_callbacks; + DARRAY(struct tick_callback) tick_callbacks; struct obs_view main_view; diff --git a/libobs/obs-video.c b/libobs/obs-video.c index 9ad873c8e..d7641ad75 100644 --- a/libobs/obs-video.c +++ b/libobs/obs-video.c @@ -38,9 +38,24 @@ static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time) delta_time = cur_time - last_time; seconds = (float)((double)delta_time / 1000000000.0); + /* ------------------------------------- */ + /* call tick callbacks */ + + pthread_mutex_lock(&obs->data.draw_callbacks_mutex); + + for (size_t i = obs->data.tick_callbacks.num; i > 0; i--) { + struct tick_callback *callback; + callback = obs->data.tick_callbacks.array + (i - 1); + callback->tick(callback->param, seconds); + } + + pthread_mutex_unlock(&obs->data.draw_callbacks_mutex); + + /* ------------------------------------- */ + /* call the tick function of each source */ + pthread_mutex_lock(&data->sources_mutex); - /* call the tick function of each source */ source = data->first_source; while (source) { obs_source_video_tick(source, seconds); @@ -111,9 +126,9 @@ static inline void render_main_texture(struct obs_core_video *video, pthread_mutex_lock(&obs->data.draw_callbacks_mutex); - for (size_t i = 0; i < obs->data.draw_callbacks.num; i++) { + for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) { struct draw_callback *callback; - callback = obs->data.draw_callbacks.array+i; + callback = obs->data.draw_callbacks.array + (i - 1); callback->draw(callback->param, video->base_width, video->base_height); diff --git a/libobs/obs.c b/libobs/obs.c index 5d1a9b77d..e8001cc16 100644 --- a/libobs/obs.c +++ b/libobs/obs.c @@ -560,7 +560,7 @@ static bool obs_init_data(void) goto fail; if (pthread_mutex_init(&data->services_mutex, &attr) != 0) goto fail; - if (pthread_mutex_init(&obs->data.draw_callbacks_mutex, NULL) != 0) + if (pthread_mutex_init(&obs->data.draw_callbacks_mutex, &attr) != 0) goto fail; if (!obs_view_init(&data->main_view)) goto fail; @@ -619,6 +619,7 @@ static void obs_free_data(void) pthread_mutex_destroy(&data->services_mutex); pthread_mutex_destroy(&data->draw_callbacks_mutex); da_free(data->draw_callbacks); + da_free(data->tick_callbacks); } static const char *obs_signals[] = { @@ -1977,6 +1978,34 @@ void obs_get_audio_monitoring_device(const char **name, const char **id) *id = obs->audio.monitoring_device_id; } +void obs_add_tick_callback( + void (*tick)(void *param, float seconds), + void *param) +{ + if (!obs) + return; + + struct tick_callback data = {tick, param}; + + pthread_mutex_lock(&obs->data.draw_callbacks_mutex); + da_insert(obs->data.tick_callbacks, 0, &data); + pthread_mutex_unlock(&obs->data.draw_callbacks_mutex); +} + +void obs_remove_tick_callback( + void (*tick)(void *param, float seconds), + void *param) +{ + if (!obs) + return; + + struct tick_callback data = {tick, param}; + + pthread_mutex_lock(&obs->data.draw_callbacks_mutex); + da_erase_item(obs->data.tick_callbacks, &data); + pthread_mutex_unlock(&obs->data.draw_callbacks_mutex); +} + void obs_add_main_render_callback( void (*draw)(void *param, uint32_t cx, uint32_t cy), void *param) @@ -1987,7 +2016,7 @@ void obs_add_main_render_callback( struct draw_callback data = {draw, param}; pthread_mutex_lock(&obs->data.draw_callbacks_mutex); - da_push_back(obs->data.draw_callbacks, &data); + da_insert(obs->data.draw_callbacks, 0, &data); pthread_mutex_unlock(&obs->data.draw_callbacks_mutex); } diff --git a/libobs/obs.h b/libobs/obs.h index d745bd0e1..5c51ed29a 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -602,6 +602,13 @@ EXPORT void obs_enum_audio_monitoring_devices(obs_enum_audio_device_cb cb, EXPORT bool obs_set_audio_monitoring_device(const char *name, const char *id); EXPORT void obs_get_audio_monitoring_device(const char **name, const char **id); +EXPORT void obs_add_tick_callback( + void (*tick)(void *param, float seconds), + void *param); +EXPORT void obs_remove_tick_callback( + void (*tick)(void *param, float seconds), + void *param); + EXPORT void obs_add_main_render_callback( void (*draw)(void *param, uint32_t cx, uint32_t cy), void *param);