2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstaudiovisualizer.h: base class for audio visualisation elements
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * SECTION:gstaudiovisualizer
23 * A baseclass for scopes (visualizers). It takes care of re-fitting the
24 * audio-rate to video-rate and handles renegotiation (downstream video size
27 * It also provides several background shading effects. These effects are
28 * applied to a previous picture before the render() implementation can draw a
36 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
37 * with newer GLib versions (>= 2.31.0) */
38 #define GLIB_DISABLE_DEPRECATION_WARNINGS
42 #include "gstaudiovisualizer.h"
44 GST_DEBUG_CATEGORY_STATIC (audio_visualizer_debug);
45 #define GST_CAT_DEFAULT (audio_visualizer_debug)
47 #define DEFAULT_SHADER GST_AUDIO_VISUALIZER_SHADER_FADE
48 #define DEFAULT_SHADE_AMOUNT 0x000a0a0a
57 static GstBaseTransformClass *parent_class = NULL;
59 static void gst_audio_visualizer_class_init (GstAudioVisualizerClass * klass);
60 static void gst_audio_visualizer_init (GstAudioVisualizer * scope,
61 GstAudioVisualizerClass * g_class);
62 static void gst_audio_visualizer_set_property (GObject * object,
63 guint prop_id, const GValue * value, GParamSpec * pspec);
64 static void gst_audio_visualizer_get_property (GObject * object,
65 guint prop_id, GValue * value, GParamSpec * pspec);
66 static void gst_audio_visualizer_dispose (GObject * object);
68 static gboolean gst_audio_visualizer_src_negotiate (GstAudioVisualizer * scope);
69 static gboolean gst_audio_visualizer_src_setcaps (GstAudioVisualizer *
70 scope, GstCaps * caps);
71 static gboolean gst_audio_visualizer_sink_setcaps (GstAudioVisualizer *
72 scope, GstCaps * caps);
74 static GstFlowReturn gst_audio_visualizer_chain (GstPad * pad,
75 GstObject * parent, GstBuffer * buffer);
77 static gboolean gst_audio_visualizer_src_event (GstPad * pad,
78 GstObject * parent, GstEvent * event);
79 static gboolean gst_audio_visualizer_sink_event (GstPad * pad,
80 GstObject * parent, GstEvent * event);
82 static gboolean gst_audio_visualizer_src_query (GstPad * pad,
83 GstObject * parent, GstQuery * query);
84 static gboolean gst_audio_visualizer_sink_query (GstPad * pad,
85 GstObject * parent, GstQuery * query);
87 static GstStateChangeReturn gst_audio_visualizer_change_state (GstElement *
88 element, GstStateChange transition);
90 /* shading functions */
92 #define GST_TYPE_AUDIO_VISUALIZER_SHADER (gst_audio_visualizer_shader_get_type())
94 gst_audio_visualizer_shader_get_type (void)
96 static GType shader_type = 0;
97 static const GEnumValue shaders[] = {
98 {GST_AUDIO_VISUALIZER_SHADER_NONE, "None", "none"},
99 {GST_AUDIO_VISUALIZER_SHADER_FADE, "Fade", "fade"},
100 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP, "Fade and move up",
102 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN, "Fade and move down",
103 "fade-and-move-down"},
104 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT, "Fade and move left",
105 "fade-and-move-left"},
106 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT,
107 "Fade and move right",
108 "fade-and-move-right"},
109 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT,
110 "Fade and move horizontally out", "fade-and-move-horiz-out"},
111 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN,
112 "Fade and move horizontally in", "fade-and-move-horiz-in"},
113 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT,
114 "Fade and move vertically out", "fade-and-move-vert-out"},
115 {GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_IN,
116 "Fade and move vertically in", "fade-and-move-vert-in"},
120 if (G_UNLIKELY (shader_type == 0)) {
121 /* TODO: rename when exporting it as a library */
123 g_enum_register_static ("GstAudioVisualizerShader-BaseExtVisual",
129 /* we're only supporting GST_VIDEO_FORMAT_xRGB right now) */
130 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
132 #define SHADE1(_d, _s, _i, _r, _g, _b) \
134 _d[_i] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
136 _d[_i] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
138 _d[_i] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
143 #define SHADE2(_d, _s, _j, _i, _r, _g, _b) \
145 _d[_j++] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
147 _d[_j++] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
149 _d[_j++] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
157 #define SHADE1(_d, _s, _i, _r, _g, _b) \
160 _d[_i] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
162 _d[_i] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
164 _d[_i] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
168 #define SHADE2(_d, _s, _j, _i, _r, _g, _b) \
172 _d[_j++] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
174 _d[_j++] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
176 _d[_j++] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
183 shader_fade (GstAudioVisualizer * scope, const guint8 * s, guint8 * d)
185 guint i, bpf = scope->bpf;
186 guint r = (scope->shade_amount >> 16) & 0xff;
187 guint g = (scope->shade_amount >> 8) & 0xff;
188 guint b = (scope->shade_amount >> 0) & 0xff;
190 for (i = 0; i < bpf;) {
191 SHADE1 (d, s, i, r, g, b);
196 shader_fade_and_move_up (GstAudioVisualizer * scope, const guint8 * s,
199 guint i, j, bpf = scope->bpf;
200 guint bpl = 4 * scope->width;
201 guint r = (scope->shade_amount >> 16) & 0xff;
202 guint g = (scope->shade_amount >> 8) & 0xff;
203 guint b = (scope->shade_amount >> 0) & 0xff;
205 for (j = 0, i = bpl; i < bpf;) {
206 SHADE2 (d, s, j, i, r, g, b);
211 shader_fade_and_move_down (GstAudioVisualizer * scope, const guint8 * s,
214 guint i, j, bpf = scope->bpf;
215 guint bpl = 4 * scope->width;
216 guint r = (scope->shade_amount >> 16) & 0xff;
217 guint g = (scope->shade_amount >> 8) & 0xff;
218 guint b = (scope->shade_amount >> 0) & 0xff;
220 for (j = bpl, i = 0; j < bpf;) {
221 SHADE2 (d, s, j, i, r, g, b);
226 shader_fade_and_move_left (GstAudioVisualizer * scope,
227 const guint8 * s, guint8 * d)
229 guint i, j, k, bpf = scope->bpf;
230 guint w = scope->width;
231 guint r = (scope->shade_amount >> 16) & 0xff;
232 guint g = (scope->shade_amount >> 8) & 0xff;
233 guint b = (scope->shade_amount >> 0) & 0xff;
235 /* move to the left */
236 for (j = 0, i = 4; i < bpf;) {
237 for (k = 0; k < w - 1; k++) {
238 SHADE2 (d, s, j, i, r, g, b);
246 shader_fade_and_move_right (GstAudioVisualizer * scope,
247 const guint8 * s, guint8 * d)
249 guint i, j, k, bpf = scope->bpf;
250 guint w = scope->width;
251 guint r = (scope->shade_amount >> 16) & 0xff;
252 guint g = (scope->shade_amount >> 8) & 0xff;
253 guint b = (scope->shade_amount >> 0) & 0xff;
255 /* move to the left */
256 for (j = 4, i = 0; i < bpf;) {
257 for (k = 0; k < w - 1; k++) {
258 SHADE2 (d, s, j, i, r, g, b);
266 shader_fade_and_move_horiz_out (GstAudioVisualizer * scope,
267 const guint8 * s, guint8 * d)
269 guint i, j, bpf = scope->bpf / 2;
270 guint bpl = 4 * scope->width;
271 guint r = (scope->shade_amount >> 16) & 0xff;
272 guint g = (scope->shade_amount >> 8) & 0xff;
273 guint b = (scope->shade_amount >> 0) & 0xff;
275 /* move upper half up */
276 for (j = 0, i = bpl; i < bpf;) {
277 SHADE2 (d, s, j, i, r, g, b);
279 /* move lower half down */
280 for (j = bpf + bpl, i = bpf; j < bpf + bpf;) {
281 SHADE2 (d, s, j, i, r, g, b);
286 shader_fade_and_move_horiz_in (GstAudioVisualizer * scope,
287 const guint8 * s, guint8 * d)
289 guint i, j, bpf = scope->bpf / 2;
290 guint bpl = 4 * scope->width;
291 guint r = (scope->shade_amount >> 16) & 0xff;
292 guint g = (scope->shade_amount >> 8) & 0xff;
293 guint b = (scope->shade_amount >> 0) & 0xff;
295 /* move upper half down */
296 for (i = 0, j = bpl; i < bpf;) {
297 SHADE2 (d, s, j, i, r, g, b);
299 /* move lower half up */
300 for (i = bpf + bpl, j = bpf; i < bpf + bpf;) {
301 SHADE2 (d, s, j, i, r, g, b);
306 shader_fade_and_move_vert_out (GstAudioVisualizer * scope,
307 const guint8 * s, guint8 * d)
309 guint i, j, k, bpf = scope->bpf;
310 guint m = scope->width / 2;
311 guint r = (scope->shade_amount >> 16) & 0xff;
312 guint g = (scope->shade_amount >> 8) & 0xff;
313 guint b = (scope->shade_amount >> 0) & 0xff;
315 /* move left half to the left */
316 for (j = 0, i = 4; i < bpf;) {
317 for (k = 0; k < m; k++) {
318 SHADE2 (d, s, j, i, r, g, b);
323 /* move right half to the right */
324 for (j = 4 * (m + 1), i = 4 * m; j < bpf;) {
325 for (k = 0; k < m; k++) {
326 SHADE2 (d, s, j, i, r, g, b);
334 shader_fade_and_move_vert_in (GstAudioVisualizer * scope,
335 const guint8 * s, guint8 * d)
337 guint i, j, k, bpf = scope->bpf;
338 guint m = scope->width / 2;
339 guint r = (scope->shade_amount >> 16) & 0xff;
340 guint g = (scope->shade_amount >> 8) & 0xff;
341 guint b = (scope->shade_amount >> 0) & 0xff;
343 /* move left half to the right */
344 for (j = 4, i = 0; j < bpf;) {
345 for (k = 0; k < m; k++) {
346 SHADE2 (d, s, j, i, r, g, b);
351 /* move right half to the left */
352 for (j = 4 * m, i = 4 * (m + 1); i < bpf;) {
353 for (k = 0; k < m; k++) {
354 SHADE2 (d, s, j, i, r, g, b);
362 gst_audio_visualizer_change_shader (GstAudioVisualizer * scope)
364 switch (scope->shader_type) {
365 case GST_AUDIO_VISUALIZER_SHADER_NONE:
366 scope->shader = NULL;
368 case GST_AUDIO_VISUALIZER_SHADER_FADE:
369 scope->shader = shader_fade;
371 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP:
372 scope->shader = shader_fade_and_move_up;
374 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN:
375 scope->shader = shader_fade_and_move_down;
377 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT:
378 scope->shader = shader_fade_and_move_left;
380 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT:
381 scope->shader = shader_fade_and_move_right;
383 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT:
384 scope->shader = shader_fade_and_move_horiz_out;
386 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN:
387 scope->shader = shader_fade_and_move_horiz_in;
389 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT:
390 scope->shader = shader_fade_and_move_vert_out;
392 case GST_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_IN:
393 scope->shader = shader_fade_and_move_vert_in;
396 GST_ERROR ("invalid shader function");
397 scope->shader = NULL;
405 gst_audio_visualizer_get_type (void)
407 static volatile gsize audio_visualizer_type = 0;
409 if (g_once_init_enter (&audio_visualizer_type)) {
410 static const GTypeInfo audio_visualizer_info = {
411 sizeof (GstAudioVisualizerClass),
414 (GClassInitFunc) gst_audio_visualizer_class_init,
417 sizeof (GstAudioVisualizer),
419 (GInstanceInitFunc) gst_audio_visualizer_init,
423 /* TODO: rename when exporting it as a library */
424 _type = g_type_register_static (GST_TYPE_ELEMENT,
425 "GstAudioVisualizer-BaseExtVisual", &audio_visualizer_info,
426 G_TYPE_FLAG_ABSTRACT);
427 g_once_init_leave (&audio_visualizer_type, _type);
429 return (GType) audio_visualizer_type;
433 gst_audio_visualizer_class_init (GstAudioVisualizerClass * klass)
435 GObjectClass *gobject_class = (GObjectClass *) klass;
436 GstElementClass *element_class = (GstElementClass *) klass;
438 parent_class = g_type_class_peek_parent (klass);
440 GST_DEBUG_CATEGORY_INIT (audio_visualizer_debug, "audiobasevisualizer",
441 0, "scope audio visualisation base class");
443 gobject_class->set_property = gst_audio_visualizer_set_property;
444 gobject_class->get_property = gst_audio_visualizer_get_property;
445 gobject_class->dispose = gst_audio_visualizer_dispose;
447 element_class->change_state =
448 GST_DEBUG_FUNCPTR (gst_audio_visualizer_change_state);
450 g_object_class_install_property (gobject_class, PROP_SHADER,
451 g_param_spec_enum ("shader", "shader type",
452 "Shader function to apply on each frame",
453 GST_TYPE_AUDIO_VISUALIZER_SHADER, DEFAULT_SHADER,
454 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
455 g_object_class_install_property (gobject_class, PROP_SHADE_AMOUNT,
456 g_param_spec_uint ("shade-amount", "shade amount",
457 "Shading color to use (big-endian ARGB)", 0, G_MAXUINT32,
458 DEFAULT_SHADE_AMOUNT,
459 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
463 gst_audio_visualizer_init (GstAudioVisualizer * scope,
464 GstAudioVisualizerClass * g_class)
466 GstPadTemplate *pad_template;
468 /* create the sink and src pads */
470 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
471 g_return_if_fail (pad_template != NULL);
472 scope->sinkpad = gst_pad_new_from_template (pad_template, "sink");
473 gst_pad_set_chain_function (scope->sinkpad,
474 GST_DEBUG_FUNCPTR (gst_audio_visualizer_chain));
475 gst_pad_set_event_function (scope->sinkpad,
476 GST_DEBUG_FUNCPTR (gst_audio_visualizer_sink_event));
477 gst_pad_set_query_function (scope->sinkpad,
478 GST_DEBUG_FUNCPTR (gst_audio_visualizer_sink_query));
479 gst_element_add_pad (GST_ELEMENT (scope), scope->sinkpad);
482 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
483 g_return_if_fail (pad_template != NULL);
484 scope->srcpad = gst_pad_new_from_template (pad_template, "src");
485 gst_pad_set_event_function (scope->srcpad,
486 GST_DEBUG_FUNCPTR (gst_audio_visualizer_src_event));
487 gst_pad_set_query_function (scope->srcpad,
488 GST_DEBUG_FUNCPTR (gst_audio_visualizer_src_query));
489 gst_element_add_pad (GST_ELEMENT (scope), scope->srcpad);
491 scope->adapter = gst_adapter_new ();
492 scope->inbuf = gst_buffer_new ();
495 scope->shader_type = DEFAULT_SHADER;
496 gst_audio_visualizer_change_shader (scope);
497 scope->shade_amount = DEFAULT_SHADE_AMOUNT;
499 /* reset the initial video state */
502 scope->fps_n = 25; /* desired frame rate */
504 scope->frame_duration = GST_CLOCK_TIME_NONE;
506 /* reset the initial state */
507 gst_audio_info_init (&scope->ainfo);
508 gst_video_info_init (&scope->vinfo);
510 g_mutex_init (&scope->config_lock);
514 gst_audio_visualizer_set_property (GObject * object, guint prop_id,
515 const GValue * value, GParamSpec * pspec)
517 GstAudioVisualizer *scope = GST_AUDIO_VISUALIZER (object);
521 scope->shader_type = g_value_get_enum (value);
522 gst_audio_visualizer_change_shader (scope);
524 case PROP_SHADE_AMOUNT:
525 scope->shade_amount = g_value_get_uint (value);
528 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534 gst_audio_visualizer_get_property (GObject * object, guint prop_id,
535 GValue * value, GParamSpec * pspec)
537 GstAudioVisualizer *scope = GST_AUDIO_VISUALIZER (object);
541 g_value_set_enum (value, scope->shader_type);
543 case PROP_SHADE_AMOUNT:
544 g_value_set_uint (value, scope->shade_amount);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553 gst_audio_visualizer_dispose (GObject * object)
555 GstAudioVisualizer *scope = GST_AUDIO_VISUALIZER (object);
557 if (scope->adapter) {
558 g_object_unref (scope->adapter);
559 scope->adapter = NULL;
562 gst_buffer_unref (scope->inbuf);
565 if (scope->pixelbuf) {
566 g_free (scope->pixelbuf);
567 scope->pixelbuf = NULL;
569 if (scope->config_lock.p) {
570 g_mutex_clear (&scope->config_lock);
571 scope->config_lock.p = NULL;
573 G_OBJECT_CLASS (parent_class)->dispose (object);
577 gst_audio_visualizer_reset (GstAudioVisualizer * scope)
579 gst_adapter_clear (scope->adapter);
580 gst_segment_init (&scope->segment, GST_FORMAT_UNDEFINED);
582 GST_OBJECT_LOCK (scope);
583 scope->proportion = 1.0;
584 scope->earliest_time = -1;
585 GST_OBJECT_UNLOCK (scope);
589 gst_audio_visualizer_sink_setcaps (GstAudioVisualizer * scope, GstCaps * caps)
594 if (!gst_audio_info_from_caps (&info, caps))
599 GST_DEBUG_OBJECT (scope, "audio: channels %d, rate %d",
600 GST_AUDIO_INFO_CHANNELS (&info), GST_AUDIO_INFO_RATE (&info));
608 GST_WARNING_OBJECT (scope, "could not parse caps");
615 gst_audio_visualizer_src_setcaps (GstAudioVisualizer * scope, GstCaps * caps)
618 GstAudioVisualizerClass *klass;
619 GstStructure *structure;
622 if (!gst_video_info_from_caps (&info, caps))
625 structure = gst_caps_get_structure (caps, 0);
626 if (!gst_structure_get_int (structure, "width", &scope->width) ||
627 !gst_structure_get_int (structure, "height", &scope->height) ||
628 !gst_structure_get_fraction (structure, "framerate", &scope->fps_n,
632 klass = GST_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
635 scope->video_format = info.finfo->format;
637 scope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
638 scope->fps_d, scope->fps_n);
639 scope->spf = gst_util_uint64_scale_int (GST_AUDIO_INFO_RATE (&scope->ainfo),
640 scope->fps_d, scope->fps_n);
641 scope->req_spf = scope->spf;
643 scope->bpf = scope->width * scope->height * 4;
646 g_free (scope->pixelbuf);
647 scope->pixelbuf = g_malloc0 (scope->bpf);
650 res = klass->setup (scope);
652 GST_DEBUG_OBJECT (scope, "video: dimension %dx%d, framerate %d/%d",
653 scope->width, scope->height, scope->fps_n, scope->fps_d);
654 GST_DEBUG_OBJECT (scope, "blocks: spf %u, req_spf %u",
655 scope->spf, scope->req_spf);
657 res = gst_pad_set_caps (scope->srcpad, caps);
664 GST_DEBUG_OBJECT (scope, "error parsing caps");
670 gst_audio_visualizer_src_negotiate (GstAudioVisualizer * scope)
672 GstCaps *othercaps, *target;
673 GstStructure *structure;
677 GstStructure *config;
678 guint size, min, max;
680 templ = gst_pad_get_pad_template_caps (scope->srcpad);
682 GST_DEBUG_OBJECT (scope, "performing negotiation");
684 /* see what the peer can do */
685 othercaps = gst_pad_peer_query_caps (scope->srcpad, NULL);
687 target = gst_caps_intersect (othercaps, templ);
688 gst_caps_unref (othercaps);
689 gst_caps_unref (templ);
691 if (gst_caps_is_empty (target))
694 target = gst_caps_truncate (target);
699 target = gst_caps_make_writable (target);
700 structure = gst_caps_get_structure (target, 0);
701 gst_structure_fixate_field_nearest_int (structure, "width", scope->width);
702 gst_structure_fixate_field_nearest_int (structure, "height", scope->height);
703 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
704 scope->fps_n, scope->fps_d);
706 GST_DEBUG_OBJECT (scope, "final caps are %" GST_PTR_FORMAT, target);
708 gst_audio_visualizer_src_setcaps (scope, target);
710 /* try to get a bufferpool now */
711 /* find a pool for the negotiated caps now */
712 query = gst_query_new_allocation (target, TRUE);
714 if (!gst_pad_peer_query (scope->srcpad, query)) {
715 /* not a problem, we use the query defaults */
716 GST_DEBUG_OBJECT (scope, "allocation query failed");
719 if (gst_query_get_n_allocation_pools (query) > 0) {
720 /* we got configuration from our peer, parse them */
721 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
729 /* we did not get a pool, make one ourselves then */
730 pool = gst_buffer_pool_new ();
733 config = gst_buffer_pool_get_config (pool);
734 gst_buffer_pool_config_set_params (config, target, size, min, max);
735 gst_buffer_pool_set_config (pool, config);
738 gst_buffer_pool_set_active (scope->pool, FALSE);
739 gst_object_unref (scope->pool);
744 gst_buffer_pool_set_active (pool, TRUE);
746 gst_caps_unref (target);
752 gst_caps_unref (target);
757 /* make sure we are negotiated */
759 gst_audio_visualizer_ensure_negotiated (GstAudioVisualizer * scope)
761 gboolean reconfigure;
763 reconfigure = gst_pad_check_reconfigure (scope->srcpad);
765 /* we don't know an output format yet, pick one */
766 if (reconfigure || !gst_pad_has_current_caps (scope->srcpad)) {
767 if (!gst_audio_visualizer_src_negotiate (scope))
768 return GST_FLOW_NOT_NEGOTIATED;
774 gst_audio_visualizer_chain (GstPad * pad, GstObject * parent,
777 GstFlowReturn ret = GST_FLOW_OK;
778 GstAudioVisualizer *scope;
779 GstAudioVisualizerClass *klass;
784 gboolean (*render) (GstAudioVisualizer * scope, GstBuffer * audio,
786 gint bps, channels, rate;
788 scope = GST_AUDIO_VISUALIZER (parent);
789 klass = GST_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
791 render = klass->render;
793 GST_LOG_OBJECT (scope, "chainfunc called");
795 /* resync on DISCONT */
796 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
797 gst_adapter_clear (scope->adapter);
800 /* Make sure have an output format */
801 ret = gst_audio_visualizer_ensure_negotiated (scope);
802 if (ret != GST_FLOW_OK) {
803 gst_buffer_unref (buffer);
806 channels = GST_AUDIO_INFO_CHANNELS (&scope->ainfo);
807 rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
808 bps = GST_AUDIO_INFO_BPS (&scope->ainfo);
811 ret = GST_FLOW_NOT_NEGOTIATED;
815 gst_adapter_push (scope->adapter, buffer);
817 g_mutex_lock (&scope->config_lock);
819 /* this is what we want */
820 sbpf = scope->req_spf * channels * sizeof (gint16);
822 inbuf = scope->inbuf;
823 /* FIXME: the timestamp in the adapter would be different */
824 gst_buffer_copy_into (inbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
826 /* this is what we have */
827 avail = gst_adapter_available (scope->adapter);
828 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
829 while (avail >= sbpf) {
833 /* get timestamp of the current adapter content */
834 ts = gst_adapter_prev_timestamp (scope->adapter, &dist);
835 if (GST_CLOCK_TIME_IS_VALID (ts)) {
836 /* convert bytes to time */
838 ts += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
841 if (GST_CLOCK_TIME_IS_VALID (ts)) {
846 gst_segment_to_running_time (&scope->segment, GST_FORMAT_TIME, ts) +
847 scope->frame_duration;
849 GST_OBJECT_LOCK (scope);
850 /* check for QoS, don't compute buffers that are known to be late */
851 need_skip = scope->earliest_time != -1 && qostime <= scope->earliest_time;
852 GST_OBJECT_UNLOCK (scope);
855 GST_WARNING_OBJECT (scope,
856 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
857 GST_TIME_ARGS (qostime), GST_TIME_ARGS (scope->earliest_time));
862 g_mutex_unlock (&scope->config_lock);
863 ret = gst_buffer_pool_acquire_buffer (scope->pool, &outbuf, NULL);
864 g_mutex_lock (&scope->config_lock);
865 /* recheck as the value could have changed */
866 sbpf = scope->req_spf * channels * sizeof (gint16);
868 /* no buffer allocated, we don't care why. */
869 if (ret != GST_FLOW_OK)
872 /* sync controlled properties */
873 gst_object_sync_values (GST_OBJECT (scope), ts);
875 GST_BUFFER_TIMESTAMP (outbuf) = ts;
876 GST_BUFFER_DURATION (outbuf) = scope->frame_duration;
878 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
880 memcpy (map.data, scope->pixelbuf, scope->bpf);
882 memset (map.data, 0, scope->bpf);
885 /* this can fail as the data size we need could have changed */
886 if (!(adata = (gpointer) gst_adapter_map (scope->adapter, sbpf)))
889 gst_buffer_replace_all_memory (inbuf,
890 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, adata, sbpf, 0,
893 /* call class->render() vmethod */
895 if (!render (scope, inbuf, outbuf)) {
896 ret = GST_FLOW_ERROR;
898 /* run various post processing (shading and geometri transformation */
900 scope->shader (scope, map.data, scope->pixelbuf);
905 gst_buffer_unmap (outbuf, &map);
906 gst_buffer_resize (outbuf, 0, scope->bpf);
908 g_mutex_unlock (&scope->config_lock);
909 ret = gst_pad_push (scope->srcpad, outbuf);
911 g_mutex_lock (&scope->config_lock);
914 /* recheck as the value could have changed */
915 sbpf = scope->req_spf * channels * sizeof (gint16);
916 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
917 /* we want to take less or more, depending on spf : req_spf */
918 if (avail - sbpf >= sbpf) {
919 gst_adapter_flush (scope->adapter, sbpf);
920 gst_adapter_unmap (scope->adapter);
921 } else if (avail >= sbpf) {
922 /* just flush a bit and stop */
923 gst_adapter_flush (scope->adapter, (avail - sbpf));
924 gst_adapter_unmap (scope->adapter);
927 avail = gst_adapter_available (scope->adapter);
929 if (ret != GST_FLOW_OK)
933 g_mutex_unlock (&scope->config_lock);
940 gst_audio_visualizer_src_event (GstPad * pad, GstObject * parent,
944 GstAudioVisualizer *scope;
946 scope = GST_AUDIO_VISUALIZER (parent);
948 switch (GST_EVENT_TYPE (event)) {
952 GstClockTimeDiff diff;
953 GstClockTime timestamp;
955 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
957 /* save stuff for the _chain() function */
958 GST_OBJECT_LOCK (scope);
959 scope->proportion = proportion;
961 /* we're late, this is a good estimate for next displayable
962 * frame (see part-qos.txt) */
963 scope->earliest_time = timestamp + 2 * diff + scope->frame_duration;
965 scope->earliest_time = timestamp + diff;
966 GST_OBJECT_UNLOCK (scope);
968 res = gst_pad_push_event (scope->sinkpad, event);
971 case GST_EVENT_RECONFIGURE:
973 gst_event_unref (event);
977 res = gst_pad_push_event (scope->sinkpad, event);
985 gst_audio_visualizer_sink_event (GstPad * pad, GstObject * parent,
989 GstAudioVisualizer *scope;
991 scope = GST_AUDIO_VISUALIZER (parent);
993 switch (GST_EVENT_TYPE (event)) {
998 gst_event_parse_caps (event, &caps);
999 res = gst_audio_visualizer_sink_setcaps (scope, caps);
1002 case GST_EVENT_FLUSH_START:
1003 res = gst_pad_push_event (scope->srcpad, event);
1005 case GST_EVENT_FLUSH_STOP:
1006 gst_audio_visualizer_reset (scope);
1007 res = gst_pad_push_event (scope->srcpad, event);
1009 case GST_EVENT_SEGMENT:
1011 /* the newsegment values are used to clip the input samples
1012 * and to convert the incomming timestamps to running time so
1014 gst_event_copy_segment (event, &scope->segment);
1016 res = gst_pad_push_event (scope->srcpad, event);
1020 res = gst_pad_push_event (scope->srcpad, event);
1028 gst_audio_visualizer_src_query (GstPad * pad, GstObject * parent,
1031 gboolean res = FALSE;
1032 GstAudioVisualizer *scope;
1034 scope = GST_AUDIO_VISUALIZER (parent);
1036 switch (GST_QUERY_TYPE (query)) {
1037 case GST_QUERY_LATENCY:
1039 /* We need to send the query upstream and add the returned latency to our
1041 GstClockTime min_latency, max_latency;
1043 GstClockTime our_latency;
1045 gint rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
1050 if ((res = gst_pad_peer_query (scope->sinkpad, query))) {
1051 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1053 GST_DEBUG_OBJECT (scope, "Peer latency: min %"
1054 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1055 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1057 /* the max samples we must buffer buffer */
1058 max_samples = MAX (scope->req_spf, scope->spf);
1059 our_latency = gst_util_uint64_scale_int (max_samples, GST_SECOND, rate);
1061 GST_DEBUG_OBJECT (scope, "Our latency: %" GST_TIME_FORMAT,
1062 GST_TIME_ARGS (our_latency));
1064 /* we add some latency but only if we need to buffer more than what
1065 * upstream gives us */
1066 min_latency += our_latency;
1067 if (max_latency != -1)
1068 max_latency += our_latency;
1070 GST_DEBUG_OBJECT (scope, "Calculated total latency : min %"
1071 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1072 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1074 gst_query_set_latency (query, TRUE, min_latency, max_latency);
1079 res = gst_pad_query_default (pad, parent, query);
1087 gst_audio_visualizer_sink_query (GstPad * pad, GstObject * parent,
1090 gboolean res = FALSE;
1092 switch (GST_QUERY_TYPE (query)) {
1094 res = gst_pad_query_default (pad, parent, query);
1100 static GstStateChangeReturn
1101 gst_audio_visualizer_change_state (GstElement * element,
1102 GstStateChange transition)
1104 GstStateChangeReturn ret;
1105 GstAudioVisualizer *scope;
1107 scope = GST_AUDIO_VISUALIZER (element);
1109 switch (transition) {
1110 case GST_STATE_CHANGE_READY_TO_PAUSED:
1111 gst_audio_visualizer_reset (scope);
1117 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1119 switch (transition) {
1120 case GST_STATE_CHANGE_PAUSED_TO_READY:
1122 gst_buffer_pool_set_active (scope->pool, FALSE);
1123 gst_object_replace ((GstObject **) & scope->pool, NULL);
1126 case GST_STATE_CHANGE_READY_TO_NULL: