[vlc-commits] [Git][videolan/vlc][master] 2 commits: clock: rework event handling
Thomas Guillem (@tguillem)
gitlab at videolan.org
Fri Mar 15 15:57:17 UTC 2024
Thomas Guillem pushed to branch master at VideoLAN / VLC
Commits:
a3f7f88f by Thomas Guillem at 2024-03-15T15:35:29+00:00
clock: rework event handling
vlc_clock_RegisterEvents become:
- vlc_clock_AddListener
- vlc_clock_RemoveListener
Using the same mechanism than player and playlist.
This will allow more controls on (future) clock events.
- - - - -
e4a4e067 by Thomas Guillem at 2024-03-15T15:35:29+00:00
clock: remove event limitation
Future events might be used by every type of clocks.
- - - - -
3 changed files:
- src/clock/clock.c
- src/clock/clock.h
- src/video_output/video_output.c
Changes:
=====================================
src/clock/clock.c
=====================================
@@ -32,7 +32,7 @@
#define COEFF_THRESHOLD 0.2 /* between 0.8 and 1.2 */
-struct vlc_clock_event
+struct vlc_clock_listener_id
{
vlc_clock_t *clock;
const struct vlc_clock_event_cbs *cbs;
@@ -70,7 +70,7 @@ struct vlc_clock_main_t
vlc_tick_t output_dejitter; /* Delay used to absorb the output clock jitter */
vlc_tick_t input_dejitter; /* Delay used to absorb the input jitter */
- struct VLC_VECTOR(struct vlc_clock_event) events;
+ struct VLC_VECTOR(vlc_clock_listener_id *) listeners;
};
struct vlc_clock_ops
@@ -96,53 +96,60 @@ struct vlc_clock_t
void *cbs_data;
};
-int vlc_clock_RegisterEvents(vlc_clock_t *clock,
- const struct vlc_clock_event_cbs *cbs,
- void *data)
+vlc_clock_listener_id *
+vlc_clock_AddListener(vlc_clock_t *clock,
+ const struct vlc_clock_event_cbs *cbs,
+ void *data)
{
vlc_clock_main_t *main_clock = clock->owner;
+ assert(cbs != NULL);
+
+ vlc_clock_listener_id *listener_id = malloc(sizeof(*listener_id));
+ if (listener_id == NULL)
+ return NULL;
+
+ listener_id->clock = clock;
+ listener_id->cbs = cbs;
+ listener_id->data = data;
vlc_mutex_lock(&main_clock->lock);
- if (clock == main_clock->master || clock == main_clock->input_master)
- {
- /* Events are only from master to slaves */
- vlc_mutex_unlock(&main_clock->lock);
- return -EINVAL;
- }
+ bool success = vlc_vector_push(&main_clock->listeners, listener_id);
+ vlc_mutex_unlock(&main_clock->lock);
- int ret;
- if (cbs != NULL)
+ if (!success)
{
- const struct vlc_clock_event event = {
- .clock = clock,
- .cbs = cbs,
- .data = data,
- };
- ret = vlc_vector_push(&main_clock->events, event) ? 0 : -ENOMEM;
- }
- else
- {
- ret = -EINVAL;
- const struct vlc_clock_event *event;
- vlc_vector_foreach_ref(event, &main_clock->events)
- if (event->clock == clock)
- {
- vlc_vector_remove(&main_clock->events, vlc_vector_idx_event);
- ret = 0;
- break;
- }
- assert(ret == 0);
+ free(listener_id);
+ return NULL;
}
+ return listener_id;
+}
- vlc_mutex_unlock(&main_clock->lock);
- return ret;
+void
+vlc_clock_RemoveListener(vlc_clock_t *clock, vlc_clock_listener_id *listener_id)
+{
+ vlc_clock_main_t *main_clock = clock->owner;
+
+ vlc_mutex_lock(&main_clock->lock);
+
+ const vlc_clock_listener_id *it;
+ vlc_vector_foreach(it, &main_clock->listeners)
+ if (it == listener_id)
+ {
+ vlc_vector_remove(&main_clock->listeners, vlc_vector_idx_it);
+ free(listener_id);
+
+ vlc_mutex_unlock(&main_clock->lock);
+ return;
+ }
+
+ vlc_assert_unreachable();
}
#define vlc_clock_SendEvent(main_clock, event) { \
- const struct vlc_clock_event *event; \
- vlc_vector_foreach_ref(event, &main_clock->events) \
- if (event->cbs->on_##event != NULL) \
- event->cbs->on_##event(event->data); \
+ const struct vlc_clock_listener_id *listener_id; \
+ vlc_vector_foreach(listener_id, &main_clock->listeners) \
+ if (listener_id->cbs->on_##event != NULL) \
+ listener_id->cbs->on_##event(listener_id->data); \
}
static inline void TraceRender(struct vlc_tracer *tracer, const char *type,
@@ -575,7 +582,7 @@ vlc_clock_main_t *vlc_clock_main_New(struct vlc_logger *parent_logger, struct vl
AvgInit(&main_clock->coeff_avg, 10);
AvgResetAndFill(&main_clock->coeff_avg, main_clock->coeff);
- vlc_vector_init(&main_clock->events);
+ vlc_vector_init(&main_clock->listeners);
return main_clock;
}
@@ -655,8 +662,8 @@ void vlc_clock_main_Delete(vlc_clock_main_t *main_clock)
if (main_clock->logger != NULL)
vlc_LogDestroy(main_clock->logger);
- assert(main_clock->events.size == 0);
- vlc_vector_destroy(&main_clock->events);
+ assert(main_clock->listeners.size == 0);
+ vlc_vector_destroy(&main_clock->listeners);
free(main_clock);
}
=====================================
src/clock/clock.h
=====================================
@@ -70,6 +70,8 @@ struct vlc_clock_event_cbs
void (*on_discontinuity)(void *data);
};
+typedef struct vlc_clock_listener_id vlc_clock_listener_id;
+
/**
* This function creates the vlc_clock_main_t of the program
*/
@@ -231,16 +233,26 @@ int vlc_clock_Wait(vlc_clock_t *clock, vlc_tick_t system_deadline);
void vlc_clock_Wake(vlc_clock_t *clock);
/**
- * Register for events
+ * Add a listener for events
*
* @param clock the clock used by the source
- * @param cbs valid pointer to register events or NULL to unregister
+ * @param cbs valid pointer to register events
* @param data opaque data used by cbs
- * @return 0 in case of success
+ * @return a valid listener id, or NULL in case of allocation error
+ */
+vlc_clock_listener_id *
+vlc_clock_AddListener(vlc_clock_t *clock,
+ const struct vlc_clock_event_cbs *cbs,
+ void *data);
+
+/**
+ * Remove a event listener callback
+ *
+ * @param clock the clock used by the source
+ * @param listener_id listener id returned by vlc_clock_AddListener()
*/
-int vlc_clock_RegisterEvents(vlc_clock_t *clock,
- const struct vlc_clock_event_cbs *cbs,
- void *data);
+void
+vlc_clock_RemoveListener(vlc_clock_t *clock, vlc_clock_listener_id *listener_id);
/**
* This function converts a timestamp from stream to system
=====================================
src/video_output/video_output.c
=====================================
@@ -84,6 +84,7 @@ typedef struct vout_thread_sys_t
bool wait_interrupted;
vlc_clock_t *clock;
+ vlc_clock_listener_id *clock_listener_id;
float rate;
vlc_tick_t delay;
@@ -1898,7 +1899,11 @@ static void vout_ReleaseDisplay(vout_thread_sys_t *vout)
if (sys->spu)
spu_Detach(sys->spu);
- vlc_clock_RegisterEvents(sys->clock, NULL, NULL);
+ if (sys->clock_listener_id != NULL)
+ {
+ vlc_clock_RemoveListener(sys->clock, sys->clock_listener_id);
+ sys->clock_listener_id = NULL;
+ }
vlc_mutex_lock(&sys->clock_lock);
sys->clock = NULL;
@@ -2249,7 +2254,8 @@ int vout_Request(const vout_configuration_t *cfg, vlc_video_context *vctx, input
static const struct vlc_clock_event_cbs clock_event_cbs = {
.on_discontinuity = clock_event_OnDiscontinuity,
};
- vlc_clock_RegisterEvents(sys->clock, &clock_event_cbs, vout);
+ sys->clock_listener_id =
+ vlc_clock_AddListener(sys->clock, &clock_event_cbs, vout);
sys->delay = 0;
@@ -2271,7 +2277,9 @@ error_thread:
vout_ReleaseDisplay(vout);
error_display:
vout_DisableWindow(vout);
- vlc_clock_RegisterEvents(sys->clock, NULL, NULL);
+ if (sys->clock_listener_id != NULL)
+ vlc_clock_RemoveListener(sys->clock, sys->clock_listener_id);
+ sys->clock_listener_id = NULL;
vlc_mutex_lock(&sys->clock_lock);
sys->clock = NULL;
vlc_mutex_unlock(&sys->clock_lock);
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5cd2d7aec11914d0e6c6ef0b5e7c02aeb1bc3e3b...e4a4e0674c8a3022f2fe2d254ba8c288673d9eaa
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5cd2d7aec11914d0e6c6ef0b5e7c02aeb1bc3e3b...e4a4e0674c8a3022f2fe2d254ba8c288673d9eaa
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
More information about the vlc-commits
mailing list