From e90926286a87a544d6a0782cffaca9f03f7d4c22 Mon Sep 17 00:00:00 2001 From: Exeldro Date: Sat, 16 Feb 2019 13:48:07 +0100 Subject: [PATCH] remove unused filters --- replay-filter-async.c | 4 +- replay-filter-audio.c | 2 + replay-filter.c | 20 ++++- replay-source.c | 194 +++++++++++++++++++++++++++++------------- replay.c | 25 ++++++ replay.h | 5 +- 6 files changed, 190 insertions(+), 60 deletions(-) diff --git a/replay-filter-async.c b/replay-filter-async.c index 7ed5c58..ca17dc5 100644 --- a/replay-filter-async.c +++ b/replay-filter-async.c @@ -36,6 +36,7 @@ static void *replay_filter_create(obs_data_t *settings, obs_source_t *source) struct replay_filter *context = bzalloc(sizeof(struct replay_filter)); context->src = source; pthread_mutex_init(&context->mutex, NULL); + context->last_check = obs_get_video_frame_time(); replay_filter_update(context, settings); @@ -80,7 +81,7 @@ static struct obs_source_frame *replay_filter_video(void *data, uint64_t last_timestamp = 0; obs_source_t* target = filter->internal_frames ? obs_filter_get_parent(filter->src) : NULL; - const uint64_t os_time = os_gettime_ns(); + const uint64_t os_time = obs_get_video_frame_time(); struct obs_source_frame *new_frame = NULL; pthread_mutex_lock(&filter->mutex); @@ -162,6 +163,7 @@ static struct obs_source_frame *replay_filter_video(void *data, } } pthread_mutex_unlock(&filter->mutex); + replay_filter_check(filter); return frame; } diff --git a/replay-filter-audio.c b/replay-filter-audio.c index 9630c4c..b20080b 100644 --- a/replay-filter-audio.c +++ b/replay-filter-audio.c @@ -28,6 +28,7 @@ static void replay_filter_update(void *data, obs_data_t *settings) filter->duration = new_duration; const double db = obs_data_get_double(settings, SETTING_AUDIO_THRESHOLD); filter->threshold = db_to_mul((float)db); + } @@ -38,6 +39,7 @@ static void *replay_filter_create(obs_data_t *settings, obs_source_t *source) struct replay_filter *context = bzalloc(sizeof(struct replay_filter)); context->src = source; pthread_mutex_init(&context->mutex, NULL); + context->last_check = obs_get_video_frame_time(); replay_filter_update(context, settings); diff --git a/replay-filter.c b/replay-filter.c index 97343ef..09dbd78 100644 --- a/replay-filter.c +++ b/replay-filter.c @@ -136,7 +136,7 @@ void replay_filter_offscreen_render(void* data, uint32_t cx, uint32_t cy) struct video_frame output_frame; if (filter->video_output && video_output_lock_frame(filter->video_output, - &output_frame, 1, os_gettime_ns())) + &output_frame, 1, obs_get_video_frame_time())) { if (filter->video_data) { gs_stagesurface_unmap(filter->stagesurface); @@ -179,6 +179,22 @@ static void replay_filter_update(void *data, obs_data_t *settings) filter->duration = new_duration; obs_add_main_render_callback(replay_filter_offscreen_render, filter); + + replay_filter_check(filter); + + obs_source_t * s = obs_get_source_by_name(obs_source_get_name(filter->src)); + if(s) + { + if(obs_data_get_bool(settings, SETTING_SOUND_TRIGGER) && !filter->trigger_threshold) + { + filter->threshold_data = (struct replay_source*)s->context.data; + filter->trigger_threshold = replay_trigger_threshold; + } + obs_source_release(s); + }else + { + obs_source_filter_remove(obs_filter_get_parent(filter->src),filter->src); + } } @@ -194,6 +210,7 @@ static void *replay_filter_create(obs_data_t *settings, obs_source_t *source) context->video_data = NULL; obs_get_video_info(&context->ovi); obs_get_audio_info(&context->oai); + context->last_check = obs_get_video_frame_time(); replay_filter_update(context, settings); @@ -249,6 +266,7 @@ void replay_filter_tick(void* data, float seconds) { struct replay_filter *filter = data; obs_get_video_info(&filter->ovi); + replay_filter_check(filter); } void replay_filter_video_render(void* data, gs_effect_t* effect) diff --git a/replay-source.c b/replay-source.c index 8e9b4c1..b098dd8 100644 --- a/replay-source.c +++ b/replay-source.c @@ -92,6 +92,8 @@ struct replay_source { obs_hotkey_id reverse_hotkey; obs_hotkey_id forward_hotkey; obs_hotkey_id backward_hotkey; + obs_hotkey_id forward_or_faster_hotkey; + obs_hotkey_id backward_or_faster_hotkey; obs_hotkey_id save_hotkey; obs_hotkey_id enable_hotkey; obs_hotkey_id disable_hotkey; @@ -147,6 +149,7 @@ struct replay_source { char *text_source_name; char *text_format; bool sound_trigger; + bool filter_loaded; }; static void replace_text(struct dstr *str, size_t pos, size_t len, @@ -235,7 +238,7 @@ static void replay_update_text(struct replay_source* c) time = c->pause_timestamp - c->start_timestamp; }else { - time = os_gettime_ns() - c->start_timestamp; + time = obs_get_video_frame_time() - c->start_timestamp; } if(c->speed_percent != 100.0f) { @@ -408,31 +411,31 @@ static void replay_forward_hotkey(void *data, obs_hotkey_id id, struct replay_source *c = data; - if(pressed){ - const int64_t time = obs_get_video_frame_time(); - if(c->pause_timestamp) - { - c->start_timestamp += time - c->pause_timestamp; - c->pause_timestamp = 0; - } - c->play = true; - if(c->end){ - c->end = false; - c->video_frame_position = 0; - c->start_timestamp = os_gettime_ns(); - c->backward = false; - }else if(c->backward) - { - c->backward = false; + if(!pressed) + return; + const int64_t time = obs_get_video_frame_time(); + if(c->pause_timestamp) + { + c->start_timestamp += time - c->pause_timestamp; + c->pause_timestamp = 0; + } + c->play = true; + if(c->end){ + c->end = false; + c->video_frame_position = 0; + c->start_timestamp = obs_get_video_frame_time(); + c->backward = false; + }else if(c->backward) + { + c->backward = false; - const int64_t duration = ((int64_t)c->current_replay.last_frame_timestamp - (int64_t)c->current_replay.first_frame_timestamp) * 100.0 / c->speed_percent; - int64_t play_duration = time - c->start_timestamp; - if(play_duration > duration) - { - play_duration = duration; - } - c->start_timestamp = time - duration + play_duration; + const int64_t duration = ((int64_t)c->current_replay.last_frame_timestamp - (int64_t)c->current_replay.first_frame_timestamp) * 100.0 / c->speed_percent; + int64_t play_duration = time - c->start_timestamp; + if(play_duration > duration) + { + play_duration = duration; } + c->start_timestamp = time - duration + play_duration; } } @@ -444,32 +447,32 @@ static void replay_backward_hotkey(void *data, obs_hotkey_id id, struct replay_source *c = data; - if(pressed){ - const int64_t time = obs_get_video_frame_time(); - if(c->pause_timestamp) - { - c->start_timestamp += time - c->pause_timestamp; - c->pause_timestamp = 0; - } - c->play = true; - if(c->end || c->video_frame_position == 0){ - c->end = false; - if(c->current_replay.video_frame_count) - c->video_frame_position = c->current_replay.video_frame_count-1; - c->start_timestamp = os_gettime_ns(); - c->backward = true; - }else if(!c->backward) - { - c->backward = true; + if(!pressed) + return; + const int64_t time = obs_get_video_frame_time(); + if(c->pause_timestamp) + { + c->start_timestamp += time - c->pause_timestamp; + c->pause_timestamp = 0; + } + c->play = true; + if(c->end || c->video_frame_position == 0){ + c->end = false; + if(c->current_replay.video_frame_count) + c->video_frame_position = c->current_replay.video_frame_count-1; + c->start_timestamp = obs_get_video_frame_time(); + c->backward = true; + }else if(!c->backward) + { + c->backward = true; - const int64_t duration = ((int64_t)c->current_replay.last_frame_timestamp - (int64_t)c->current_replay.first_frame_timestamp) * 100.0 / c->speed_percent; - int64_t play_duration = time - c->start_timestamp; - if(play_duration > duration) - { - play_duration = duration; - } - c->start_timestamp = time - duration + play_duration; + const int64_t duration = ((int64_t)c->current_replay.last_frame_timestamp - (int64_t)c->current_replay.first_frame_timestamp) * 100.0 / c->speed_percent; + int64_t play_duration = time - c->start_timestamp; + if(play_duration > duration) + { + play_duration = duration; } + c->start_timestamp = time - duration + play_duration; } } @@ -501,7 +504,7 @@ static void replay_update_position(struct replay_source *c, bool lock){ memcpy(&c->current_replay, circlebuf_data(&c->replays, c->replay_position*sizeof c->current_replay), sizeof c->current_replay); c->video_frame_position = 0; c->audio_frame_position = 0; - c->start_timestamp = os_gettime_ns(); + c->start_timestamp = obs_get_video_frame_time(); c->backward = c->backward_start; if(!c->backward && c->current_replay.trim_front != 0){ if(c->speed_percent == 100.0f){ @@ -583,10 +586,10 @@ static void replay_purge_replays(struct replay_source *context) } void replay_retrieve(struct replay_source *c); -static void replay_trigger_threshold(void *data) +void replay_trigger_threshold(void *data) { struct replay_source *context = data; - const uint64_t os_time = os_gettime_ns(); + const uint64_t os_time = obs_get_video_frame_time(); uint64_t duration = context->current_replay.duration; if(context->speed_percent < 100.f) duration = duration * 100.0 / context->speed_percent; @@ -597,7 +600,7 @@ static void replay_trigger_threshold(void *data) if(context->retrieve_delay > 0) { - context->retrieve_timestamp = os_gettime_ns() + context->retrieve_delay; + context->retrieve_timestamp = obs_get_video_frame_time() + context->retrieve_delay; }else{ replay_retrieve(context); } @@ -705,7 +708,7 @@ static void replay_source_update(void *data, obs_data_t *settings) ((struct replay_filter*)context->source_filter->context.data)->threshold_data = data; ((struct replay_filter*)context->source_filter->context.data)->trigger_threshold = context->sound_trigger?replay_trigger_threshold:NULL; - + context->filter_loaded = true; obs_source_release(s); } s = obs_get_source_by_name(context->source_audio_name); @@ -726,6 +729,7 @@ static void replay_source_update(void *data, obs_data_t *settings) } ((struct replay_filter*)context->source_audio_filter->context.data)->threshold_data = data; ((struct replay_filter*)context->source_audio_filter->context.data)->trigger_threshold = context->sound_trigger?replay_trigger_threshold:NULL; + context->filter_loaded = true; obs_source_release(s); } } @@ -820,11 +824,12 @@ static void replay_source_active(void *data) struct replay_source *context = data; if(context->visibility_action == VISIBILITY_ACTION_PAUSE || context->visibility_action == VISIBILITY_ACTION_CONTINUE) { - if(!context->play){ + if(!context->play && !context->end){ context->play = true; if(context->pause_timestamp) { context->start_timestamp += obs_get_video_frame_time() - context->pause_timestamp; + context->pause_timestamp = 0; } } } @@ -1126,7 +1131,7 @@ void replay_save(struct replay_source *context) obs_data_release(settings); context->video_save_position = 0; - context->start_save_timestamp = os_gettime_ns(); + context->start_save_timestamp = obs_get_video_frame_time(); struct obs_source_frame* frame = context->saving_replay.video_frames[0]; struct video_frame output_frame; @@ -1285,7 +1290,7 @@ static void replay_hotkey(void *data, obs_hotkey_id id, if(c->retrieve_delay > 0) { - c->retrieve_timestamp = os_gettime_ns() + c->retrieve_delay; + c->retrieve_timestamp = obs_get_video_frame_time() + c->retrieve_delay; }else{ replay_retrieve(c); } @@ -1623,6 +1628,45 @@ static void replay_double_speed_hotkey(void *data, obs_hotkey_id id, update_speed(c, 200.0f); } +static void replay_forward_or_faster_hotkey(void *data, obs_hotkey_id id, + obs_hotkey_t *hotkey, bool pressed) +{ + UNUSED_PARAMETER(id); + UNUSED_PARAMETER(hotkey); + + struct replay_source *c = data; + + if(!pressed) + return; + + if(c->backward) + { + replay_forward_hotkey(data,id,hotkey,pressed); + replay_normal_speed_hotkey(data,id,hotkey,pressed); + return; + } + replay_normal_or_faster_hotkey(data,id,hotkey,pressed); +} + +static void replay_backward_or_faster_hotkey(void *data, obs_hotkey_id id, + obs_hotkey_t *hotkey, bool pressed) +{ + UNUSED_PARAMETER(id); + UNUSED_PARAMETER(hotkey); + + struct replay_source *c = data; + + if(!pressed) + return; + if(!c->backward) + { + replay_backward_hotkey(data,id,hotkey,pressed); + replay_normal_speed_hotkey(data,id,hotkey,pressed); + return; + } + replay_normal_or_faster_hotkey(data,id,hotkey,pressed); +} + static void replay_trim_front_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { @@ -1841,6 +1885,16 @@ static void *replay_source_create(obs_data_t *settings, obs_source_t *source) obs_module_text("Backward"), replay_backward_hotkey, context); + context->forward_or_faster_hotkey = obs_hotkey_register_source(source, + "ReplaySource.ForwardOrFaster", + "Forward or faster", + replay_forward_or_faster_hotkey, context); + + context->backward_or_faster_hotkey = obs_hotkey_register_source(source, + "ReplaySource.BackwardOrFaster", + "Backward or faster", + replay_backward_or_faster_hotkey, context); + context->disable_hotkey = obs_hotkey_register_source(source, "ReplaySource.Disable", obs_module_text("Disable"), @@ -2056,13 +2110,39 @@ static void replay_source_tick(void *data, float seconds) { struct replay_source *context = data; - const uint64_t os_timestamp = os_gettime_ns(); + const uint64_t os_timestamp = obs_get_video_frame_time(); if(context->retrieve_timestamp && context->retrieve_timestamp < os_timestamp) { context->retrieve_timestamp = 0; replay_retrieve(context); } + if(!context->filter_loaded) + { + if(context->source_name){ + obs_source_t *s = obs_get_source_by_name(context->source_name); + if(s) + { + obs_data_t* settings = obs_source_get_settings(context->source); + replay_source_update(data, settings); + obs_data_release(settings); + obs_source_release(s); + return; + } + } + if(context->source_audio_name){ + obs_source_t *s = obs_get_source_by_name(context->source_audio_name); + if(s) + { + obs_data_t* settings = obs_source_get_settings(context->source); + replay_source_update(data, settings); + obs_data_release(settings); + obs_source_release(s); + return; + } + } + return; + } if(context->saving_status == SAVING_STATUS_STARTING){ replay_save(context); diff --git a/replay.c b/replay.c index 376fdfb..54c0734 100644 --- a/replay.c +++ b/replay.c @@ -86,6 +86,7 @@ struct obs_audio_data *replay_filter_audio(void *data, cur_duration = adjusted_time - cached.timestamp; } pthread_mutex_unlock(&filter->mutex); + replay_filter_check(filter); return audio; } @@ -250,3 +251,27 @@ const char *obs_module_author(void) { return "Exeldro"; } + +void replay_filter_check(struct replay_filter* filter) +{ + if(filter->last_check && filter->last_check + SEC_TO_NSEC > obs_get_video_frame_time()) + return; + filter->last_check = obs_get_video_frame_time(); + obs_source_t * s = obs_get_source_by_name(obs_source_get_name(filter->src)); + if(s) + { + if(!filter->trigger_threshold) + { + obs_data_t* settings= obs_source_get_settings(s); + if(obs_data_get_bool(settings, SETTING_SOUND_TRIGGER)){ + filter->threshold_data = (struct replay_source*)s->context.data; + filter->trigger_threshold = replay_trigger_threshold; + } + obs_data_release(settings); + } + obs_source_release(s); + }else + { + obs_source_filter_remove(obs_filter_get_parent(filter->src),filter->src); + } +} diff --git a/replay.h b/replay.h index 2b40add..4d19f8a 100644 --- a/replay.h +++ b/replay.h @@ -24,7 +24,7 @@ struct replay_filter { uint32_t video_linesize; video_t* video_output; - uint64_t duration; + uint64_t duration; obs_source_t *src; pthread_mutex_t mutex; int64_t timing_adjust; @@ -32,6 +32,7 @@ struct replay_filter { float threshold; void (*trigger_threshold)(void *data); void *threshold_data; + uint64_t last_check; }; void obs_source_frame_copy(struct obs_source_frame * dst,const struct obs_source_frame *src); @@ -41,6 +42,8 @@ void free_video_data(struct replay_filter *filter); void free_audio_data(struct replay_filter *filter); void obs_enum_scenes(bool (*enum_proc)(void*, obs_source_t*),void *param); obs_properties_t *replay_filter_properties(void *unused); +void replay_trigger_threshold(void *data); +void replay_filter_check(struct replay_filter* filter); #define REPLAY_FILTER_ID "replay_filter" #define TEXT_FILTER_NAME "Replay filter"