2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstaudiobasevisualizer.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:gstaudiobasevisualizer
23 * A basclass 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 "gstaudiobasevisualizer.h"
44 GST_DEBUG_CATEGORY_STATIC (audio_base_visualizer_debug);
45 #define GST_CAT_DEFAULT (audio_base_visualizer_debug)
47 #define DEFAULT_SHADER GST_AUDIO_BASE_VISUALIZER_SHADER_FADE
48 #define DEFAULT_SHADE_AMOUNT 0x000a0a0a
57 static GstBaseTransformClass *parent_class = NULL;
59 static void gst_audio_base_visualizer_class_init (GstAudioBaseVisualizerClass *
61 static void gst_audio_base_visualizer_init (GstAudioBaseVisualizer * scope,
62 GstAudioBaseVisualizerClass * g_class);
63 static void gst_audio_base_visualizer_set_property (GObject * object,
64 guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_audio_base_visualizer_get_property (GObject * object,
66 guint prop_id, GValue * value, GParamSpec * pspec);
67 static void gst_audio_base_visualizer_dispose (GObject * object);
69 static gboolean gst_audio_base_visualizer_src_negotiate (GstAudioBaseVisualizer
71 static gboolean gst_audio_base_visualizer_src_setcaps (GstAudioBaseVisualizer *
72 scope, GstCaps * caps);
73 static gboolean gst_audio_base_visualizer_sink_setcaps (GstAudioBaseVisualizer *
74 scope, GstCaps * caps);
76 static GstFlowReturn gst_audio_base_visualizer_chain (GstPad * pad,
77 GstObject * parent, GstBuffer * buffer);
79 static gboolean gst_audio_base_visualizer_src_event (GstPad * pad,
80 GstObject * parent, GstEvent * event);
81 static gboolean gst_audio_base_visualizer_sink_event (GstPad * pad,
82 GstObject * parent, GstEvent * event);
84 static gboolean gst_audio_base_visualizer_src_query (GstPad * pad,
85 GstObject * parent, GstQuery * query);
86 static gboolean gst_audio_base_visualizer_sink_query (GstPad * pad,
87 GstObject * parent, GstQuery * query);
89 static GstStateChangeReturn gst_audio_base_visualizer_change_state (GstElement *
90 element, GstStateChange transition);
92 /* shading functions */
94 #define GST_TYPE_AUDIO_BASE_VISUALIZER_SHADER (gst_audio_base_visualizer_shader_get_type())
96 gst_audio_base_visualizer_shader_get_type (void)
98 static GType shader_type = 0;
99 static const GEnumValue shaders[] = {
100 {GST_AUDIO_BASE_VISUALIZER_SHADER_NONE, "None", "none"},
101 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE, "Fade", "fade"},
102 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_UP, "Fade and move up",
104 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN, "Fade and move down",
105 "fade-and-move-down"},
106 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT, "Fade and move left",
107 "fade-and-move-left"},
108 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT,
109 "Fade and move right",
110 "fade-and-move-right"},
111 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT,
112 "Fade and move horizontally out", "fade-and-move-horiz-out"},
113 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN,
114 "Fade and move horizontally in", "fade-and-move-horiz-in"},
115 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT,
116 "Fade and move vertically out", "fade-and-move-vert-out"},
117 {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_IN,
118 "Fade and move vertically in", "fade-and-move-vert-in"},
122 if (G_UNLIKELY (shader_type == 0)) {
124 g_enum_register_static ("GstAudioBaseVisualizerShader", shaders);
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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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 (GstAudioBaseVisualizer * 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_base_visualizer_change_shader (GstAudioBaseVisualizer * scope)
364 switch (scope->shader_type) {
365 case GST_AUDIO_BASE_VISUALIZER_SHADER_NONE:
366 scope->shader = NULL;
368 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE:
369 scope->shader = shader_fade;
371 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_UP:
372 scope->shader = shader_fade_and_move_up;
374 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN:
375 scope->shader = shader_fade_and_move_down;
377 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT:
378 scope->shader = shader_fade_and_move_left;
380 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT:
381 scope->shader = shader_fade_and_move_right;
383 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT:
384 scope->shader = shader_fade_and_move_horiz_out;
386 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN:
387 scope->shader = shader_fade_and_move_horiz_in;
389 case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT:
390 scope->shader = shader_fade_and_move_vert_out;
392 case GST_AUDIO_BASE_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_base_visualizer_get_type (void)
407 static volatile gsize audio_base_visualizer_type = 0;
409 if (g_once_init_enter (&audio_base_visualizer_type)) {
410 static const GTypeInfo audio_base_visualizer_info = {
411 sizeof (GstAudioBaseVisualizerClass),
414 (GClassInitFunc) gst_audio_base_visualizer_class_init,
417 sizeof (GstAudioBaseVisualizer),
419 (GInstanceInitFunc) gst_audio_base_visualizer_init,
423 _type = g_type_register_static (GST_TYPE_ELEMENT,
424 "GstAudioBaseVisualizer", &audio_base_visualizer_info,
425 G_TYPE_FLAG_ABSTRACT);
426 g_once_init_leave (&audio_base_visualizer_type, _type);
428 return (GType) audio_base_visualizer_type;
432 gst_audio_base_visualizer_class_init (GstAudioBaseVisualizerClass * klass)
434 GObjectClass *gobject_class = (GObjectClass *) klass;
435 GstElementClass *element_class = (GstElementClass *) klass;
437 parent_class = g_type_class_peek_parent (klass);
439 GST_DEBUG_CATEGORY_INIT (audio_base_visualizer_debug, "audiobasevisualizer",
440 0, "scope audio visualisation base class");
442 gobject_class->set_property = gst_audio_base_visualizer_set_property;
443 gobject_class->get_property = gst_audio_base_visualizer_get_property;
444 gobject_class->dispose = gst_audio_base_visualizer_dispose;
446 element_class->change_state =
447 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_change_state);
449 g_object_class_install_property (gobject_class, PROP_SHADER,
450 g_param_spec_enum ("shader", "shader type",
451 "Shader function to apply on each frame",
452 GST_TYPE_AUDIO_BASE_VISUALIZER_SHADER, DEFAULT_SHADER,
453 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_SHADE_AMOUNT,
455 g_param_spec_uint ("shade-amount", "shade amount",
456 "Shading color to use (big-endian ARGB)", 0, G_MAXUINT32,
457 DEFAULT_SHADE_AMOUNT,
458 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
462 gst_audio_base_visualizer_init (GstAudioBaseVisualizer * scope,
463 GstAudioBaseVisualizerClass * g_class)
465 GstPadTemplate *pad_template;
467 /* create the sink and src pads */
469 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
470 g_return_if_fail (pad_template != NULL);
471 scope->sinkpad = gst_pad_new_from_template (pad_template, "sink");
472 gst_pad_set_chain_function (scope->sinkpad,
473 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_chain));
474 gst_pad_set_event_function (scope->sinkpad,
475 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_sink_event));
476 gst_pad_set_query_function (scope->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_sink_query));
478 gst_element_add_pad (GST_ELEMENT (scope), scope->sinkpad);
481 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
482 g_return_if_fail (pad_template != NULL);
483 scope->srcpad = gst_pad_new_from_template (pad_template, "src");
484 gst_pad_set_event_function (scope->srcpad,
485 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_src_event));
486 gst_pad_set_query_function (scope->srcpad,
487 GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_src_query));
488 gst_element_add_pad (GST_ELEMENT (scope), scope->srcpad);
490 scope->adapter = gst_adapter_new ();
491 scope->inbuf = gst_buffer_new ();
494 scope->shader_type = DEFAULT_SHADER;
495 gst_audio_base_visualizer_change_shader (scope);
496 scope->shade_amount = DEFAULT_SHADE_AMOUNT;
498 /* reset the initial video state */
501 scope->fps_n = 25; /* desired frame rate */
503 scope->frame_duration = GST_CLOCK_TIME_NONE;
505 /* reset the initial state */
506 gst_audio_info_init (&scope->ainfo);
507 gst_video_info_init (&scope->vinfo);
509 g_mutex_init (&scope->config_lock);
513 gst_audio_base_visualizer_set_property (GObject * object, guint prop_id,
514 const GValue * value, GParamSpec * pspec)
516 GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
520 scope->shader_type = g_value_get_enum (value);
521 gst_audio_base_visualizer_change_shader (scope);
523 case PROP_SHADE_AMOUNT:
524 scope->shade_amount = g_value_get_uint (value);
527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
533 gst_audio_base_visualizer_get_property (GObject * object, guint prop_id,
534 GValue * value, GParamSpec * pspec)
536 GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
540 g_value_set_enum (value, scope->shader_type);
542 case PROP_SHADE_AMOUNT:
543 g_value_set_uint (value, scope->shade_amount);
546 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
552 gst_audio_base_visualizer_dispose (GObject * object)
554 GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
556 if (scope->adapter) {
557 g_object_unref (scope->adapter);
558 scope->adapter = NULL;
561 gst_buffer_unref (scope->inbuf);
564 if (scope->pixelbuf) {
565 g_free (scope->pixelbuf);
566 scope->pixelbuf = NULL;
568 if (scope->config_lock.p) {
569 g_mutex_clear (&scope->config_lock);
570 scope->config_lock.p = NULL;
572 G_OBJECT_CLASS (parent_class)->dispose (object);
576 gst_audio_base_visualizer_reset (GstAudioBaseVisualizer * scope)
578 gst_adapter_clear (scope->adapter);
579 gst_segment_init (&scope->segment, GST_FORMAT_UNDEFINED);
581 GST_OBJECT_LOCK (scope);
582 scope->proportion = 1.0;
583 scope->earliest_time = -1;
584 GST_OBJECT_UNLOCK (scope);
588 gst_audio_base_visualizer_sink_setcaps (GstAudioBaseVisualizer * scope,
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_base_visualizer_src_setcaps (GstAudioBaseVisualizer * scope,
619 GstAudioBaseVisualizerClass *klass;
620 GstStructure *structure;
623 if (!gst_video_info_from_caps (&info, caps))
626 structure = gst_caps_get_structure (caps, 0);
627 if (!gst_structure_get_int (structure, "width", &scope->width) ||
628 !gst_structure_get_int (structure, "height", &scope->height) ||
629 !gst_structure_get_fraction (structure, "framerate", &scope->fps_n,
633 klass = GST_AUDIO_BASE_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
636 scope->video_format = info.finfo->format;
638 scope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
639 scope->fps_d, scope->fps_n);
640 scope->spf = gst_util_uint64_scale_int (GST_AUDIO_INFO_RATE (&scope->ainfo),
641 scope->fps_d, scope->fps_n);
642 scope->req_spf = scope->spf;
644 scope->bpf = scope->width * scope->height * 4;
647 g_free (scope->pixelbuf);
648 scope->pixelbuf = g_malloc0 (scope->bpf);
651 res = klass->setup (scope);
653 GST_DEBUG_OBJECT (scope, "video: dimension %dx%d, framerate %d/%d",
654 scope->width, scope->height, scope->fps_n, scope->fps_d);
655 GST_DEBUG_OBJECT (scope, "blocks: spf %u, req_spf %u",
656 scope->spf, scope->req_spf);
658 res = gst_pad_set_caps (scope->srcpad, caps);
665 GST_DEBUG_OBJECT (scope, "error parsing caps");
671 gst_audio_base_visualizer_src_negotiate (GstAudioBaseVisualizer * scope)
673 GstCaps *othercaps, *target;
674 GstStructure *structure;
678 GstStructure *config;
679 guint size, min, max;
681 templ = gst_pad_get_pad_template_caps (scope->srcpad);
683 GST_DEBUG_OBJECT (scope, "performing negotiation");
685 /* see what the peer can do */
686 othercaps = gst_pad_peer_query_caps (scope->srcpad, NULL);
688 target = gst_caps_intersect (othercaps, templ);
689 gst_caps_unref (othercaps);
690 gst_caps_unref (templ);
692 if (gst_caps_is_empty (target))
695 target = gst_caps_truncate (target);
700 target = gst_caps_make_writable (target);
701 structure = gst_caps_get_structure (target, 0);
702 gst_structure_fixate_field_nearest_int (structure, "width", scope->width);
703 gst_structure_fixate_field_nearest_int (structure, "height", scope->height);
704 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
705 scope->fps_n, scope->fps_d);
707 GST_DEBUG_OBJECT (scope, "final caps are %" GST_PTR_FORMAT, target);
709 gst_audio_base_visualizer_src_setcaps (scope, target);
711 /* try to get a bufferpool now */
712 /* find a pool for the negotiated caps now */
713 query = gst_query_new_allocation (target, TRUE);
715 if (!gst_pad_peer_query (scope->srcpad, query)) {
716 /* not a problem, we use the query defaults */
717 GST_DEBUG_OBJECT (scope, "allocation query failed");
720 if (gst_query_get_n_allocation_pools (query) > 0) {
721 /* we got configuration from our peer, parse them */
722 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
730 /* we did not get a pool, make one ourselves then */
731 pool = gst_buffer_pool_new ();
734 config = gst_buffer_pool_get_config (pool);
735 gst_buffer_pool_config_set_params (config, target, size, min, max);
736 gst_buffer_pool_set_config (pool, config);
739 gst_buffer_pool_set_active (scope->pool, FALSE);
740 gst_object_unref (scope->pool);
745 gst_buffer_pool_set_active (pool, TRUE);
747 gst_caps_unref (target);
753 gst_caps_unref (target);
758 /* make sure we are negotiated */
760 gst_audio_base_visualizer_ensure_negotiated (GstAudioBaseVisualizer * scope)
762 gboolean reconfigure;
764 reconfigure = gst_pad_check_reconfigure (scope->srcpad);
766 /* we don't know an output format yet, pick one */
767 if (reconfigure || !gst_pad_has_current_caps (scope->srcpad)) {
768 if (!gst_audio_base_visualizer_src_negotiate (scope))
769 return GST_FLOW_NOT_NEGOTIATED;
775 gst_audio_base_visualizer_chain (GstPad * pad, GstObject * parent,
778 GstFlowReturn ret = GST_FLOW_OK;
779 GstAudioBaseVisualizer *scope;
780 GstAudioBaseVisualizerClass *klass;
785 gboolean (*render) (GstAudioBaseVisualizer * scope, GstBuffer * audio,
787 gint bps, channels, rate;
789 scope = GST_AUDIO_BASE_VISUALIZER (parent);
790 klass = GST_AUDIO_BASE_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
792 render = klass->render;
794 GST_LOG_OBJECT (scope, "chainfunc called");
796 /* resync on DISCONT */
797 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
798 gst_adapter_clear (scope->adapter);
801 /* Make sure have an output format */
802 ret = gst_audio_base_visualizer_ensure_negotiated (scope);
803 if (ret != GST_FLOW_OK) {
804 gst_buffer_unref (buffer);
807 channels = GST_AUDIO_INFO_CHANNELS (&scope->ainfo);
808 rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
809 bps = GST_AUDIO_INFO_BPS (&scope->ainfo);
812 ret = GST_FLOW_NOT_NEGOTIATED;
816 gst_adapter_push (scope->adapter, buffer);
818 g_mutex_lock (&scope->config_lock);
820 /* this is what we want */
821 sbpf = scope->req_spf * channels * sizeof (gint16);
823 inbuf = scope->inbuf;
824 /* FIXME: the timestamp in the adapter would be different */
825 gst_buffer_copy_into (inbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
827 /* this is what we have */
828 avail = gst_adapter_available (scope->adapter);
829 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
830 while (avail >= sbpf) {
834 /* get timestamp of the current adapter content */
835 ts = gst_adapter_prev_timestamp (scope->adapter, &dist);
836 if (GST_CLOCK_TIME_IS_VALID (ts)) {
837 /* convert bytes to time */
839 ts += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
842 if (GST_CLOCK_TIME_IS_VALID (ts)) {
847 gst_segment_to_running_time (&scope->segment, GST_FORMAT_TIME, ts) +
848 scope->frame_duration;
850 GST_OBJECT_LOCK (scope);
851 /* check for QoS, don't compute buffers that are known to be late */
852 need_skip = scope->earliest_time != -1 && qostime <= scope->earliest_time;
853 GST_OBJECT_UNLOCK (scope);
856 GST_WARNING_OBJECT (scope,
857 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
858 GST_TIME_ARGS (qostime), GST_TIME_ARGS (scope->earliest_time));
863 g_mutex_unlock (&scope->config_lock);
864 ret = gst_buffer_pool_acquire_buffer (scope->pool, &outbuf, NULL);
865 g_mutex_lock (&scope->config_lock);
866 /* recheck as the value could have changed */
867 sbpf = scope->req_spf * channels * sizeof (gint16);
869 /* no buffer allocated, we don't care why. */
870 if (ret != GST_FLOW_OK)
873 /* sync controlled properties */
874 gst_object_sync_values (GST_OBJECT (scope), ts);
876 GST_BUFFER_TIMESTAMP (outbuf) = ts;
877 GST_BUFFER_DURATION (outbuf) = scope->frame_duration;
879 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
881 memcpy (map.data, scope->pixelbuf, scope->bpf);
883 memset (map.data, 0, scope->bpf);
886 /* this can fail as the data size we need could have changed */
887 if (!(adata = (gpointer) gst_adapter_map (scope->adapter, sbpf)))
890 gst_buffer_replace_all_memory (inbuf,
891 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, adata, sbpf, 0,
894 /* call class->render() vmethod */
896 if (!render (scope, inbuf, outbuf)) {
897 ret = GST_FLOW_ERROR;
899 /* run various post processing (shading and geometri transformation */
901 scope->shader (scope, map.data, scope->pixelbuf);
906 gst_buffer_unmap (outbuf, &map);
907 gst_buffer_resize (outbuf, 0, scope->bpf);
909 g_mutex_unlock (&scope->config_lock);
910 ret = gst_pad_push (scope->srcpad, outbuf);
912 g_mutex_lock (&scope->config_lock);
915 /* recheck as the value could have changed */
916 sbpf = scope->req_spf * channels * sizeof (gint16);
917 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
918 /* we want to take less or more, depending on spf : req_spf */
919 if (avail - sbpf >= sbpf) {
920 gst_adapter_flush (scope->adapter, sbpf);
921 gst_adapter_unmap (scope->adapter);
922 } else if (avail >= sbpf) {
923 /* just flush a bit and stop */
924 gst_adapter_flush (scope->adapter, (avail - sbpf));
925 gst_adapter_unmap (scope->adapter);
928 avail = gst_adapter_available (scope->adapter);
930 if (ret != GST_FLOW_OK)
934 g_mutex_unlock (&scope->config_lock);
941 gst_audio_base_visualizer_src_event (GstPad * pad, GstObject * parent,
945 GstAudioBaseVisualizer *scope;
947 scope = GST_AUDIO_BASE_VISUALIZER (parent);
949 switch (GST_EVENT_TYPE (event)) {
953 GstClockTimeDiff diff;
954 GstClockTime timestamp;
956 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
958 /* save stuff for the _chain() function */
959 GST_OBJECT_LOCK (scope);
960 scope->proportion = proportion;
962 /* we're late, this is a good estimate for next displayable
963 * frame (see part-qos.txt) */
964 scope->earliest_time = timestamp + 2 * diff + scope->frame_duration;
966 scope->earliest_time = timestamp + diff;
967 GST_OBJECT_UNLOCK (scope);
969 res = gst_pad_push_event (scope->sinkpad, event);
972 case GST_EVENT_RECONFIGURE:
974 gst_event_unref (event);
978 res = gst_pad_push_event (scope->sinkpad, event);
986 gst_audio_base_visualizer_sink_event (GstPad * pad, GstObject * parent,
990 GstAudioBaseVisualizer *scope;
992 scope = GST_AUDIO_BASE_VISUALIZER (parent);
994 switch (GST_EVENT_TYPE (event)) {
999 gst_event_parse_caps (event, &caps);
1000 res = gst_audio_base_visualizer_sink_setcaps (scope, caps);
1003 case GST_EVENT_FLUSH_START:
1004 res = gst_pad_push_event (scope->srcpad, event);
1006 case GST_EVENT_FLUSH_STOP:
1007 gst_audio_base_visualizer_reset (scope);
1008 res = gst_pad_push_event (scope->srcpad, event);
1010 case GST_EVENT_SEGMENT:
1012 /* the newsegment values are used to clip the input samples
1013 * and to convert the incomming timestamps to running time so
1015 gst_event_copy_segment (event, &scope->segment);
1017 res = gst_pad_push_event (scope->srcpad, event);
1021 res = gst_pad_push_event (scope->srcpad, event);
1029 gst_audio_base_visualizer_src_query (GstPad * pad, GstObject * parent,
1032 gboolean res = FALSE;
1033 GstAudioBaseVisualizer *scope;
1035 scope = GST_AUDIO_BASE_VISUALIZER (parent);
1037 switch (GST_QUERY_TYPE (query)) {
1038 case GST_QUERY_LATENCY:
1040 /* We need to send the query upstream and add the returned latency to our
1042 GstClockTime min_latency, max_latency;
1044 GstClockTime our_latency;
1046 gint rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
1051 if ((res = gst_pad_peer_query (scope->sinkpad, query))) {
1052 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1054 GST_DEBUG_OBJECT (scope, "Peer latency: min %"
1055 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1056 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1058 /* the max samples we must buffer buffer */
1059 max_samples = MAX (scope->req_spf, scope->spf);
1060 our_latency = gst_util_uint64_scale_int (max_samples, GST_SECOND, rate);
1062 GST_DEBUG_OBJECT (scope, "Our latency: %" GST_TIME_FORMAT,
1063 GST_TIME_ARGS (our_latency));
1065 /* we add some latency but only if we need to buffer more than what
1066 * upstream gives us */
1067 min_latency += our_latency;
1068 if (max_latency != -1)
1069 max_latency += our_latency;
1071 GST_DEBUG_OBJECT (scope, "Calculated total latency : min %"
1072 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1073 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1075 gst_query_set_latency (query, TRUE, min_latency, max_latency);
1080 res = gst_pad_query_default (pad, parent, query);
1088 gst_audio_base_visualizer_sink_query (GstPad * pad, GstObject * parent,
1091 gboolean res = FALSE;
1093 switch (GST_QUERY_TYPE (query)) {
1095 res = gst_pad_query_default (pad, parent, query);
1101 static GstStateChangeReturn
1102 gst_audio_base_visualizer_change_state (GstElement * element,
1103 GstStateChange transition)
1105 GstStateChangeReturn ret;
1106 GstAudioBaseVisualizer *scope;
1108 scope = GST_AUDIO_BASE_VISUALIZER (element);
1110 switch (transition) {
1111 case GST_STATE_CHANGE_READY_TO_PAUSED:
1112 gst_audio_base_visualizer_reset (scope);
1118 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1120 switch (transition) {
1121 case GST_STATE_CHANGE_PAUSED_TO_READY:
1123 gst_buffer_pool_set_active (scope->pool, FALSE);
1124 gst_object_replace ((GstObject **) & scope->pool, NULL);
1127 case GST_STATE_CHANGE_READY_TO_NULL: