Skip to content

Commit 9a6ea87

Browse files
authored
Sync projectM time to gstreamer PTS time (#3)
* Sync projectM time to gstreamer PTS time * Require projectM 4.2.0 * Fix cmake version check syntax * cmake version for projectM min * retry CI
1 parent afbc7c9 commit 9a6ea87

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project(gstprojectm VERSION 0.0.1)
99

1010
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1111

12-
find_package(projectM4 REQUIRED)
12+
find_package(projectM4 4.1.0 REQUIRED)
1313
find_package(GStreamer REQUIRED COMPONENTS gstreamer-audio gstreamer-gl gstreamer-pbutils gstreamer-video)
1414
find_package(GLIB2 REQUIRED)
1515

@@ -70,4 +70,4 @@ target_link_libraries(gstprojectm
7070
${GSTREAMER_PBUTILS_LIBRARIES}
7171
${GLIB2_LIBRARIES}
7272
${GLIB2_GOBJECT_LIBRARIES}
73-
)
73+
)

src/plugin.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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

3135
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) {
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
291314
static 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

445472
GST_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

Comments
 (0)