diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a214d6..ea63f35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,11 @@ find_package(projectM4 REQUIRED) find_package(GStreamer REQUIRED COMPONENTS gstreamer-audio gstreamer-gl gstreamer-pbutils gstreamer-video) find_package(GLIB2 REQUIRED) +if(projectM4_VERSION VERSION_LESS 4.2.0) + # needed for timekeeper API + message(FATAL_ERROR "libprojectM version 4.2.0 or higher is required. Version found: ${projectM4_VERSION}.") +endif() + add_library(gstprojectm SHARED src/caps.h src/caps.c diff --git a/src/plugin.c b/src/plugin.c index 4615f54..5886b68 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1,3 +1,4 @@ +#include #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -26,6 +27,9 @@ struct _GstProjectMPrivate { GLenum gl_format; projectm_handle handle; + + GstClockTime first_frame_time; + gboolean first_frame_received; }; G_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) { return TRUE; } +static double get_seconds_since_first_frame(GstProjectM *plugin, GstVideoFrame *frame) +{ + if (!plugin->priv->first_frame_received) { + // Store the timestamp of the first frame + plugin->priv->first_frame_time = GST_BUFFER_PTS (frame->buffer); + plugin->priv->first_frame_received = TRUE; + return 0.0; + } + + // Calculate elapsed time + GstClockTime current_time = GST_BUFFER_PTS (frame->buffer); + GstClockTime elapsed_time = current_time - plugin->priv->first_frame_time; + + // Convert to fractional seconds + gdouble elapsed_seconds = (gdouble) elapsed_time / GST_SECOND; + + return elapsed_seconds; +} + // TODO: CLEANUP & ADD DEBUGGING static gboolean gst_projectm_render(GstGLBaseAudioVisualizer *glav, GstBuffer *audio, GstVideoFrame *video) @@ -295,6 +318,10 @@ static gboolean gst_projectm_render(GstGLBaseAudioVisualizer *glav, GstBuffer *a GstMapInfo audioMap; gboolean result = TRUE; + // get current gst (PTS) time and set projectM time + double seconds_since_first_frame = get_seconds_since_first_frame(plugin, video); + projectm_set_frame_time(plugin->priv->handle, seconds_since_first_frame); + // AUDIO gst_buffer_map(audio, &audioMap, GST_MAP_READ); @@ -444,4 +471,4 @@ static gboolean plugin_init(GstPlugin *plugin) GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, GST_VERSION_MINOR, projectm, "plugin to visualize audio using the ProjectM library", plugin_init, - PACKAGE_VERSION, PACKAGE_LICENSE, PACKAGE_NAME, PACKAGE_ORIGIN) \ No newline at end of file + PACKAGE_VERSION, PACKAGE_LICENSE, PACKAGE_NAME, PACKAGE_ORIGIN)