1+ #include <projectM-4/parameters.h>
12#ifdef HAVE_CONFIG_H
23#include "config.h"
34#endif
@@ -26,6 +27,9 @@ struct _GstProjectMPrivate
2627{
2728 GLenum gl_format ;
2829 projectm_handle handle ;
30+
31+ GstClockTime first_frame_time ;
32+ gboolean first_frame_received ;
2933};
3034
3135G_DEFINE_TYPE_WITH_CODE (GstProjectM , gst_projectm , GST_TYPE_GL_BASE_AUDIO_VISUALIZER , G_ADD_PRIVATE (GstProjectM )
@@ -286,6 +290,25 @@ static gboolean gst_projectm_setup(GstGLBaseAudioVisualizer *glav) {
286290 return TRUE;
287291}
288292
293+ static double get_seconds_since_first_frame (GstProjectM * plugin , GstVideoFrame * frame )
294+ {
295+ if (!plugin -> priv -> first_frame_received ) {
296+ // Store the timestamp of the first frame
297+ plugin -> priv -> first_frame_time = GST_BUFFER_PTS (frame -> buffer );
298+ plugin -> priv -> first_frame_received = TRUE;
299+ return 0.0 ;
300+ }
301+
302+ // Calculate elapsed time
303+ GstClockTime current_time = GST_BUFFER_PTS (frame -> buffer );
304+ GstClockTime elapsed_time = current_time - plugin -> priv -> first_frame_time ;
305+
306+ // Convert to fractional seconds
307+ gdouble elapsed_seconds = (gdouble ) elapsed_time / GST_SECOND ;
308+
309+ return elapsed_seconds ;
310+ }
311+
289312
290313// TODO: CLEANUP & ADD DEBUGGING
291314static gboolean gst_projectm_render (GstGLBaseAudioVisualizer * glav , GstBuffer * audio , GstVideoFrame * video )
@@ -295,6 +318,10 @@ static gboolean gst_projectm_render(GstGLBaseAudioVisualizer *glav, GstBuffer *a
295318 GstMapInfo audioMap ;
296319 gboolean result = TRUE;
297320
321+ // get current gst (PTS) time and set projectM time
322+ double seconds_since_first_frame = get_seconds_since_first_frame (plugin , video );
323+ projectm_set_frame_time (plugin -> priv -> handle , seconds_since_first_frame );
324+
298325 // AUDIO
299326 gst_buffer_map (audio , & audioMap , GST_MAP_READ );
300327
@@ -444,4 +471,4 @@ static gboolean plugin_init(GstPlugin *plugin)
444471
445472GST_PLUGIN_DEFINE (GST_VERSION_MAJOR , GST_VERSION_MINOR , projectm ,
446473 "plugin to visualize audio using the ProjectM library" , plugin_init ,
447- PACKAGE_VERSION , PACKAGE_LICENSE , PACKAGE_NAME , PACKAGE_ORIGIN )
474+ PACKAGE_VERSION , PACKAGE_LICENSE , PACKAGE_NAME , PACKAGE_ORIGIN )
0 commit comments