| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2012-2023 Libvisual Team |
| 4 |
* Copyright (C) 2004-2006 Dennis Smit <[email protected]> |
| 5 |
* |
| 6 |
* Authors: Dennis Smit <[email protected]> |
| 7 |
* Chong Kai Xiong <[email protected]> |
| 8 |
* Sebastian Pipping <[email protected]> |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as |
| 12 |
* published by the Free Software Foundation; either version 2.1 |
| 13 |
* of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with this program; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <config.h> |
| 26 |
#include <gettext.h> |
| 27 |
#include <libvisual/libvisual.h> |
| 28 |
#include <gst/gst.h> |
| 29 |
|
| 30 |
typedef struct { |
| 31 |
GstElement *pipeline; |
| 32 |
GMainLoop *glib_main_loop; |
| 33 |
GstBuffer *buffer; |
| 34 |
GstElement *capsfilter; |
| 35 |
GstElement *sink; |
| 36 |
GMutex *mutex; |
| 37 |
} GstreamerPrivate; |
| 38 |
|
| 39 |
int act_gstreamer_init (VisPluginData *plugin); |
| 40 |
int act_gstreamer_cleanup (VisPluginData *plugin); |
| 41 |
int act_gstreamer_requisition (VisPluginData *plugin, int *width, int *height); |
| 42 |
int act_gstreamer_dimension (VisPluginData *plugin, VisVideo *video, int width, int height); |
| 43 |
int act_gstreamer_events (VisPluginData *plugin, VisEventQueue *events); |
| 44 |
VisPalette *act_gstreamer_palette (VisPluginData *plugin); |
| 45 |
int act_gstreamer_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio); |
| 46 |
|
| 47 |
void handle_sink_handoff (GstElement *sink, GstBuffer *buffer, GstPad *pad, GstreamerPrivate *data); |
| 48 |
void handle_bus_error_message (GstBus *bus, GstMessage *message, GstreamerPrivate *priv); |
| 49 |
void handle_bus_eos_message (GstBus *bus, GstMessage *message, GstreamerPrivate *priv); |
| 50 |
|
| 51 |
VISUAL_PLUGIN_API_VERSION_VALIDATOR |
| 52 |
|
| 53 |
const VisPluginInfo *get_plugin_info (int *count) |
| 54 |
{ |
| 55 |
static VisActorPlugin actor[] = {{ |
| 56 |
.requisition = act_gstreamer_requisition, |
| 57 |
.palette = act_gstreamer_palette, |
| 58 |
.render = act_gstreamer_render, |
| 59 |
.vidoptions.depth = VISUAL_VIDEO_DEPTH_24BIT |
| 60 |
}}; |
| 61 |
|
| 62 |
static VisPluginInfo info[] = {{ |
| 63 |
.type = VISUAL_PLUGIN_TYPE_ACTOR, |
| 64 |
|
| 65 |
.plugname = "gstreamer", |
| 66 |
.name = "libvisual gstreamer", |
| 67 |
.author = N_("Dennis Smit <[email protected]>"), |
| 68 |
.version = "0.1", |
| 69 |
.about = N_("Libvisual gstreamer plugin"), |
| 70 |
.help = N_("Capable of using gstreamer to play video, or do effects using the gstreamer pipeline"), |
| 71 |
.license = VISUAL_PLUGIN_LICENSE_LGPL, |
| 72 |
|
| 73 |
.init = act_gstreamer_init, |
| 74 |
.cleanup = act_gstreamer_cleanup, |
| 75 |
.events = act_gstreamer_events, |
| 76 |
|
| 77 |
.plugin = VISUAL_OBJECT (&actor[0]) |
| 78 |
}}; |
| 79 |
|
| 80 |
*count = sizeof (info) / sizeof (*info); |
| 81 |
|
| 82 |
return info; |
| 83 |
} |
| 84 |
|
| 85 |
int act_gstreamer_init (VisPluginData *plugin) |
| 86 |
{ |
| 87 |
GstreamerPrivate *priv; |
| 88 |
|
| 89 |
#if ENABLE_NLS |
| 90 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 91 |
#endif |
| 92 |
|
| 93 |
priv = visual_mem_new0 (GstreamerPrivate, 1); |
| 94 |
visual_object_set_private (VISUAL_OBJECT (plugin), priv); |
| 95 |
|
| 96 |
gst_init (NULL, NULL); |
| 97 |
|
| 98 |
// Regarding the pipeline below: |
| 99 |
// - Element "filesrc" reads a video file from disk. |
| 100 |
// - Element "decodebin" auto-magically constructs a decoding pipeline |
| 101 |
// using available decoders and demuxers via auto-plugging. |
| 102 |
// - Element "videoconvert" does conversion between video formats |
| 103 |
// from given to wanted. |
| 104 |
// - Element "videoscale" does scaling from given to wanted resolution. |
| 105 |
// - Element "capsfilter" limits the output to the width/height/format we want. |
| 106 |
// - Element "fakesink" allows accessing the rendered raw pixels from C code. |
| 107 |
// - "name=capsfilter" and "name=sink" are needed to access these elements |
| 108 |
// from within C code further down. |
| 109 |
// - "signal-handoffs=true" makes fakesink let use know it started using |
| 110 |
// a different buffer. |
| 111 |
// - "sync=true" makes fakesink respect the target framerate of the video |
| 112 |
// input to give the appearance of original playback speed |
| 113 |
// (in contrast to serving frames as fast as possible). |
| 114 |
char *launch_str = g_strdup_printf ("filesrc location=%s ! decodebin ! videoconvert ! " |
| 115 |
"videoscale ! capsfilter name=capsfilter ! " |
| 116 |
"fakesink name=sink signal-handoffs=true sync=true", |
| 117 |
"test.mpg"); |
| 118 |
|
| 119 |
GError *error = NULL; |
| 120 |
priv->pipeline = gst_parse_launch (launch_str, &error); |
| 121 |
|
| 122 |
if (!priv->pipeline) { |
| 123 |
visual_log (VISUAL_LOG_ERROR, "Failed to create pipeline: %s", error->message); |
| 124 |
g_error_free (error); |
| 125 |
g_free (launch_str); |
| 126 |
return FALSE; |
| 127 |
} |
| 128 |
|
| 129 |
priv->capsfilter = gst_bin_get_by_name (GST_BIN (priv->pipeline), "capsfilter"); |
| 130 |
GstCaps *caps = gst_caps_new_simple ("video/x-raw", |
| 131 |
"width", G_TYPE_INT, 320, |
| 132 |
"height", G_TYPE_INT, 240, |
| 133 |
"format", G_TYPE_STRING, "BGR", |
| 134 |
NULL); |
| 135 |
g_object_set (priv->capsfilter, "caps", caps, NULL); |
| 136 |
gst_caps_unref (caps); |
| 137 |
|
| 138 |
priv->buffer = NULL; |
| 139 |
priv->mutex = g_slice_new0 (GMutex); |
| 140 |
g_mutex_init (priv->mutex); |
| 141 |
|
| 142 |
priv->sink = gst_bin_get_by_name (GST_BIN (priv->pipeline), "sink"); |
| 143 |
g_signal_connect (priv->sink, "handoff", G_CALLBACK (handle_sink_handoff), priv); |
| 144 |
|
| 145 |
gst_element_set_state (priv->pipeline, GST_STATE_PAUSED); |
| 146 |
|
| 147 |
GstStateChangeReturn status = gst_element_get_state (priv->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE); |
| 148 |
if (status != GST_STATE_CHANGE_SUCCESS) { |
| 149 |
visual_log (VISUAL_LOG_ERROR, "Failed to ready pipeline: %s", launch_str); |
| 150 |
g_free (launch_str); |
| 151 |
return FALSE; |
| 152 |
} |
| 153 |
g_free (launch_str); |
| 154 |
|
| 155 |
GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline)); |
| 156 |
g_signal_connect (bus, "message::error", G_CALLBACK (handle_bus_error_message), priv); |
| 157 |
g_signal_connect (bus, "message::eos", G_CALLBACK (handle_bus_eos_message), priv); |
| 158 |
gst_object_unref (bus); |
| 159 |
|
| 160 |
priv->glib_main_loop = g_main_loop_new (NULL, FALSE); |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
int act_gstreamer_cleanup (VisPluginData *plugin) |
| 166 |
{ |
| 167 |
GstreamerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 168 |
|
| 169 |
if (priv->pipeline) { |
| 170 |
g_signal_handlers_disconnect_by_func (priv->sink, "handoff", handle_sink_handoff); |
| 171 |
|
| 172 |
GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline)); |
| 173 |
g_signal_handlers_disconnect_by_func (bus, handle_bus_error_message, priv); |
| 174 |
g_signal_handlers_disconnect_by_func (bus, handle_bus_eos_message, priv); |
| 175 |
gst_object_unref (bus); |
| 176 |
|
| 177 |
g_main_loop_unref (priv->glib_main_loop); |
| 178 |
|
| 179 |
gst_object_unref (priv->sink); |
| 180 |
gst_object_unref (priv->capsfilter); |
| 181 |
|
| 182 |
gst_element_set_state (priv->pipeline, GST_STATE_NULL); |
| 183 |
gst_object_unref (priv->pipeline); |
| 184 |
|
| 185 |
if (priv->buffer) { |
| 186 |
gst_buffer_unref (priv->buffer); |
| 187 |
} |
| 188 |
|
| 189 |
g_mutex_clear (priv->mutex); |
| 190 |
g_slice_free (GMutex, priv->mutex); |
| 191 |
} |
| 192 |
|
| 193 |
visual_mem_free (priv); |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
int act_gstreamer_requisition (VisPluginData *plugin, int *width, int *height) |
| 199 |
{ |
| 200 |
GstreamerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 201 |
|
| 202 |
GstCaps *caps = NULL; |
| 203 |
g_object_get (priv->capsfilter, "caps", &caps, NULL); |
| 204 |
|
| 205 |
GstStructure *structure = gst_caps_get_structure (caps, 0); |
| 206 |
gst_structure_get (structure, |
| 207 |
"width", width, |
| 208 |
"height", height, |
| 209 |
NULL); |
| 210 |
gst_caps_unref (caps); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
int act_gstreamer_dimension (VisPluginData *plugin, VisVideo *video, int width, int height) |
| 216 |
{ |
| 217 |
GstreamerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 218 |
|
| 219 |
GstCaps *caps = gst_caps_new_simple ("video/x-raw", |
| 220 |
"width" , G_TYPE_INT, width, |
| 221 |
"height", G_TYPE_INT, height, |
| 222 |
"format", G_TYPE_STRING, "BGR", |
| 223 |
NULL); |
| 224 |
g_object_set (priv->capsfilter, "caps", caps, NULL); |
| 225 |
gst_caps_unref (caps); |
| 226 |
|
| 227 |
visual_video_set_dimension (video, width, height); |
| 228 |
|
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
int act_gstreamer_events (VisPluginData *plugin, VisEventQueue *events) |
| 233 |
{ |
| 234 |
VisEvent ev; |
| 235 |
|
| 236 |
while (visual_event_queue_poll (events, &ev)) { |
| 237 |
switch (ev.type) { |
| 238 |
case VISUAL_EVENT_RESIZE: |
| 239 |
act_gstreamer_dimension (plugin, ev.event.resize.video, |
| 240 |
ev.event.resize.width, ev.event.resize.height); |
| 241 |
break; |
| 242 |
|
| 243 |
|
| 244 |
default: |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
VisPalette *act_gstreamer_palette (VisPluginData *plugin) |
| 253 |
{ |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
|
| 257 |
int act_gstreamer_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio) |
| 258 |
{ |
| 259 |
GstreamerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 260 |
|
| 261 |
GstState state, pending_state; |
| 262 |
GstStateChangeReturn status; |
| 263 |
|
| 264 |
gst_element_get_state (priv->pipeline, &state, &pending_state, 0); |
| 265 |
if (state != GST_STATE_PLAYING && pending_state != GST_STATE_PLAYING) { |
| 266 |
/* Set pipeline to playing if not already done so */ |
| 267 |
gst_element_set_state (priv->pipeline, GST_STATE_PLAYING); |
| 268 |
|
| 269 |
/* Check pipeline state, make sure it's already playing, or going to be set playing */ |
| 270 |
status = gst_element_get_state (priv->pipeline, &state, &pending_state, 10 * GST_MSECOND); |
| 271 |
|
| 272 |
switch (status) { |
| 273 |
case GST_STATE_CHANGE_ASYNC: |
| 274 |
visual_log (VISUAL_LOG_INFO, "Waiting for pipeline to get ready (Current state: %s)", |
| 275 |
gst_element_state_get_name (state)); |
| 276 |
return 0; |
| 277 |
|
| 278 |
case GST_STATE_CHANGE_FAILURE: |
| 279 |
visual_log (VISUAL_LOG_INFO, "Failed to animate pipeline"); |
| 280 |
return 0; |
| 281 |
|
| 282 |
default: /* nothing */; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/* Process any messages on the bus */ |
| 287 |
g_main_context_iteration (g_main_loop_get_context (priv->glib_main_loop), FALSE); |
| 288 |
|
| 289 |
/* Draw if we have a buffer */ |
| 290 |
g_mutex_lock (priv->mutex); |
| 291 |
if (priv->buffer) { |
| 292 |
// NOTE: GStreamer format "BGR" does not have any padding |
| 293 |
const int expected_row_pitch = video->width * (24 / 8); |
| 294 |
const int expected_buffer_size = expected_row_pitch * video->height; |
| 295 |
|
| 296 |
/* Copy buffer to video only if dimensions match, ignoring |
| 297 |
* buffers received from GStreamer before their corresponding |
| 298 |
* resizes were completed. */ |
| 299 |
if (gst_buffer_get_size (priv->buffer) == expected_buffer_size) { |
| 300 |
GstMapInfo info; |
| 301 |
if (gst_buffer_map(priv->buffer, &info, GST_MAP_READ)) { |
| 302 |
VisVideo * const source = visual_video_new(); |
| 303 |
visual_video_set_depth(source, VISUAL_VIDEO_DEPTH_24BIT); |
| 304 |
visual_video_set_dimension(source, video->width, video->height); |
| 305 |
visual_video_set_buffer(source, info.data); |
| 306 |
|
| 307 |
visual_video_depth_transform (video, source); |
| 308 |
|
| 309 |
visual_object_unref (VISUAL_OBJECT (source)); |
| 310 |
|
| 311 |
gst_buffer_unmap(priv->buffer, &info); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
gst_buffer_unref (priv->buffer); |
| 316 |
|
| 317 |
priv->buffer = NULL; |
| 318 |
} |
| 319 |
g_mutex_unlock (priv->mutex); |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
void handle_sink_handoff (GstElement *sink, GstBuffer *buffer, GstPad *pad, GstreamerPrivate* priv) |
| 325 |
{ |
| 326 |
g_mutex_lock (priv->mutex); |
| 327 |
|
| 328 |
/* Keep the most recently received buffer alive for rendering */ |
| 329 |
if (priv->buffer) { |
| 330 |
gst_buffer_unref (priv->buffer); |
| 331 |
} |
| 332 |
|
| 333 |
priv->buffer = buffer; |
| 334 |
gst_buffer_ref (priv->buffer); |
| 335 |
|
| 336 |
g_mutex_unlock (priv->mutex); |
| 337 |
} |
| 338 |
|
| 339 |
void handle_bus_error_message (GstBus *bus, GstMessage *message, GstreamerPrivate *priv) |
| 340 |
{ |
| 341 |
GError *error = NULL; |
| 342 |
char *msg = NULL; |
| 343 |
gst_message_parse_error (message, &error, &msg); |
| 344 |
visual_log (VISUAL_LOG_ERROR, "GStreamer error: %s", msg); |
| 345 |
g_error_free (error); |
| 346 |
g_free (msg); |
| 347 |
} |
| 348 |
|
| 349 |
void handle_bus_eos_message (GstBus *bus, GstMessage *message, GstreamerPrivate *priv) |
| 350 |
{ |
| 351 |
visual_log (VISUAL_LOG_INFO, "End of stream!"); |
| 352 |
} |