Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync projectM time to gstreamer PTS time #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have CMake check this directly in the find_package() command using this (rather weird) syntax:

find_package(projectM4 4.2.0...< REQUIRED)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to like that
CMake Error at CMakeLists.txt:12 (find_package):
find_package called with invalid argument "4.1.0...<"

The docs aren't super helpful to me https://cmake.org/cmake/help/latest/command/find_package.html#find-package-version-format

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the docs are relatively vague. I guess the upper limit can't be excluded, which I should have seen. This syntax here is probably the right one, it basically says "minimum 4.2.0, and everything before 5.0".

find_package(projectM4 4.2.0...<5.0.0 REQUIRED)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really, you have to specify the upper limit? that's fucked

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconvenient for sure. You could in theory just put 9999 there, but I'd also prefer a notation like >=4.2.0 or even a comma/semicolon-separated list of ranges so you may exclude certain versions, e.g. bugged ones, like >=1.0.0...<1.5.0,>=1.5.3 to exclude versions from 1.5.0 up to 1.5.2 if those have a bug that was fixed in 1.5.3. But it is what it is. Putting 5.0.0 there should suffice, as the projectM-4 package will never reach this version.

# 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
Expand Down
29 changes: 28 additions & 1 deletion src/plugin.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <projectM-4/parameters.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -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)
PACKAGE_VERSION, PACKAGE_LICENSE, PACKAGE_NAME, PACKAGE_ORIGIN)
Loading