2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstbaseaudiovisualizer.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:gstbaseaudiovisualizer
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 "gstbaseaudiovisualizer.h"
44 GST_DEBUG_CATEGORY_STATIC (base_audio_visualizer_debug);
45 #define GST_CAT_DEFAULT (base_audio_visualizer_debug)
47 #define DEFAULT_SHADER GST_BASE_AUDIO_VISUALIZER_SHADER_FADE
48 #define DEFAULT_SHADE_AMOUNT 0x000a0a0a
57 static GstBaseTransformClass *parent_class = NULL;
59 static void gst_base_audio_visualizer_class_init (GstBaseAudioVisualizerClass *
61 static void gst_base_audio_visualizer_init (GstBaseAudioVisualizer * scope,
62 GstBaseAudioVisualizerClass * g_class);
63 static void gst_base_audio_visualizer_set_property (GObject * object,
64 guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_base_audio_visualizer_get_property (GObject * object,
66 guint prop_id, GValue * value, GParamSpec * pspec);
67 static void gst_base_audio_visualizer_dispose (GObject * object);
69 static gboolean gst_base_audio_visualizer_src_negotiate (GstBaseAudioVisualizer
71 static gboolean gst_base_audio_visualizer_src_setcaps (GstBaseAudioVisualizer *
72 scope, GstCaps * caps);
73 static gboolean gst_base_audio_visualizer_sink_setcaps (GstBaseAudioVisualizer *
74 scope, GstCaps * caps);
76 static GstFlowReturn gst_base_audio_visualizer_chain (GstPad * pad,
77 GstObject * parent, GstBuffer * buffer);
79 static gboolean gst_base_audio_visualizer_src_event (GstPad * pad,
80 GstObject * parent, GstEvent * event);
81 static gboolean gst_base_audio_visualizer_sink_event (GstPad * pad,
82 GstObject * parent, GstEvent * event);
84 static gboolean gst_base_audio_visualizer_src_query (GstPad * pad,
85 GstObject * parent, GstQuery * query);
86 static gboolean gst_base_audio_visualizer_sink_query (GstPad * pad,
87 GstObject * parent, GstQuery * query);
89 static GstStateChangeReturn gst_base_audio_visualizer_change_state (GstElement *
90 element, GstStateChange transition);
92 /* shading functions */
94 #define GST_TYPE_BASE_AUDIO_VISUALIZER_SHADER (gst_base_audio_visualizer_shader_get_type())
96 gst_base_audio_visualizer_shader_get_type (void)
98 static GType shader_type = 0;
99 static const GEnumValue shaders[] = {
100 {GST_BASE_AUDIO_VISUALIZER_SHADER_NONE, "None", "none"},
101 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE, "Fade", "fade"},
102 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP, "Fade and move up",
104 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN, "Fade and move down",
105 "fade-and-move-down"},
106 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT, "Fade and move left",
107 "fade-and-move-left"},
108 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT,
109 "Fade and move right",
110 "fade-and-move-right"},
111 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT,
112 "Fade and move horizontally out", "fade-and-move-horiz-out"},
113 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN,
114 "Fade and move horizontally in", "fade-and-move-horiz-in"},
115 {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT,
116 "Fade and move vertically out", "fade-and-move-vert-out"},
117 {GST_BASE_AUDIO_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 ("GstBaseAudioVisualizerShader", 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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_base_audio_visualizer_change_shader (GstBaseAudioVisualizer * scope)
364 switch (scope->shader_type) {
365 case GST_BASE_AUDIO_VISUALIZER_SHADER_NONE:
366 scope->shader = NULL;
368 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE:
369 scope->shader = shader_fade;
371 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP:
372 scope->shader = shader_fade_and_move_up;
374 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN:
375 scope->shader = shader_fade_and_move_down;
377 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT:
378 scope->shader = shader_fade_and_move_left;
380 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT:
381 scope->shader = shader_fade_and_move_right;
383 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT:
384 scope->shader = shader_fade_and_move_horiz_out;
386 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN:
387 scope->shader = shader_fade_and_move_horiz_in;
389 case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT:
390 scope->shader = shader_fade_and_move_vert_out;
392 case GST_BASE_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_base_audio_visualizer_get_type (void)
407 static volatile gsize base_audio_visualizer_type = 0;
409 if (g_once_init_enter (&base_audio_visualizer_type)) {
410 static const GTypeInfo base_audio_visualizer_info = {
411 sizeof (GstBaseAudioVisualizerClass),
414 (GClassInitFunc) gst_base_audio_visualizer_class_init,
417 sizeof (GstBaseAudioVisualizer),
419 (GInstanceInitFunc) gst_base_audio_visualizer_init,
423 _type = g_type_register_static (GST_TYPE_ELEMENT,
424 "GstBaseAudioVisualizer", &base_audio_visualizer_info,
425 G_TYPE_FLAG_ABSTRACT);
426 g_once_init_leave (&base_audio_visualizer_type, _type);
428 return (GType) base_audio_visualizer_type;
432 gst_base_audio_visualizer_class_init (GstBaseAudioVisualizerClass * 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 (base_audio_visualizer_debug, "baseaudiovisualizer",
440 0, "scope audio visualisation base class");
442 gobject_class->set_property = gst_base_audio_visualizer_set_property;
443 gobject_class->get_property = gst_base_audio_visualizer_get_property;
444 gobject_class->dispose = gst_base_audio_visualizer_dispose;
446 element_class->change_state =
447 GST_DEBUG_FUNCPTR (gst_base_audio_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_BASE_AUDIO_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_base_audio_visualizer_init (GstBaseAudioVisualizer * scope,
463 GstBaseAudioVisualizerClass * 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_base_audio_visualizer_chain));
474 gst_pad_set_event_function (scope->sinkpad,
475 GST_DEBUG_FUNCPTR (gst_base_audio_visualizer_sink_event));
476 gst_pad_set_query_function (scope->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_base_audio_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_base_audio_visualizer_src_event));
486 gst_pad_set_query_function (scope->srcpad,
487 GST_DEBUG_FUNCPTR (gst_base_audio_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_base_audio_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 audio state */
506 gst_audio_info_init (&scope->ainfo);
508 g_mutex_init (&scope->config_lock);
512 gst_base_audio_visualizer_set_property (GObject * object, guint prop_id,
513 const GValue * value, GParamSpec * pspec)
515 GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
519 scope->shader_type = g_value_get_enum (value);
520 gst_base_audio_visualizer_change_shader (scope);
522 case PROP_SHADE_AMOUNT:
523 scope->shade_amount = g_value_get_uint (value);
526 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 gst_base_audio_visualizer_get_property (GObject * object, guint prop_id,
533 GValue * value, GParamSpec * pspec)
535 GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
539 g_value_set_enum (value, scope->shader_type);
541 case PROP_SHADE_AMOUNT:
542 g_value_set_uint (value, scope->shade_amount);
545 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551 gst_base_audio_visualizer_dispose (GObject * object)
553 GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
555 if (scope->adapter) {
556 g_object_unref (scope->adapter);
557 scope->adapter = NULL;
560 gst_buffer_unref (scope->inbuf);
563 if (scope->pixelbuf) {
564 g_free (scope->pixelbuf);
565 scope->pixelbuf = NULL;
567 if (scope->config_lock.p) {
568 g_mutex_clear (&scope->config_lock);
569 scope->config_lock.p = NULL;
571 G_OBJECT_CLASS (parent_class)->dispose (object);
575 gst_base_audio_visualizer_reset (GstBaseAudioVisualizer * scope)
577 gst_adapter_clear (scope->adapter);
578 gst_segment_init (&scope->segment, GST_FORMAT_UNDEFINED);
580 GST_OBJECT_LOCK (scope);
581 scope->proportion = 1.0;
582 scope->earliest_time = -1;
583 GST_OBJECT_UNLOCK (scope);
587 gst_base_audio_visualizer_sink_setcaps (GstBaseAudioVisualizer * scope,
593 if (!gst_audio_info_from_caps (&info, caps))
598 GST_DEBUG_OBJECT (scope, "audio: channels %d, rate %d",
599 GST_AUDIO_INFO_CHANNELS (&info), GST_AUDIO_INFO_RATE (&info));
607 GST_WARNING_OBJECT (scope, "could not parse caps");
614 gst_base_audio_visualizer_src_setcaps (GstBaseAudioVisualizer * scope,
617 GstBaseAudioVisualizerClass *klass;
618 GstStructure *structure;
621 structure = gst_caps_get_structure (caps, 0);
622 if (!gst_structure_get_int (structure, "width", &scope->width) ||
623 !gst_structure_get_int (structure, "height", &scope->height) ||
624 !gst_structure_get_fraction (structure, "framerate", &scope->fps_n,
628 klass = GST_BASE_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
630 //scope->video_format = format; ??
632 scope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
633 scope->fps_d, scope->fps_n);
634 scope->spf = gst_util_uint64_scale_int (GST_AUDIO_INFO_RATE (&scope->ainfo),
635 scope->fps_d, scope->fps_n);
636 scope->req_spf = scope->spf;
638 scope->bpf = scope->width * scope->height * 4;
641 g_free (scope->pixelbuf);
642 scope->pixelbuf = g_malloc0 (scope->bpf);
645 res = klass->setup (scope);
647 GST_DEBUG_OBJECT (scope, "video: dimension %dx%d, framerate %d/%d",
648 scope->width, scope->height, scope->fps_n, scope->fps_d);
649 GST_DEBUG_OBJECT (scope, "blocks: spf %u, req_spf %u",
650 scope->spf, scope->req_spf);
652 res = gst_pad_push_event (scope->srcpad, gst_event_new_caps (caps));
659 GST_DEBUG_OBJECT (scope, "error parsing caps");
665 gst_base_audio_visualizer_src_negotiate (GstBaseAudioVisualizer * scope)
667 GstCaps *othercaps, *target;
668 GstStructure *structure;
672 GstStructure *config;
673 guint size, min, max;
675 templ = gst_pad_get_pad_template_caps (scope->srcpad);
677 GST_DEBUG_OBJECT (scope, "performing negotiation");
679 /* see what the peer can do */
680 othercaps = gst_pad_peer_query_caps (scope->srcpad, NULL);
682 target = gst_caps_intersect (othercaps, templ);
683 gst_caps_unref (othercaps);
684 gst_caps_unref (templ);
686 if (gst_caps_is_empty (target))
689 target = gst_caps_truncate (target);
694 target = gst_caps_make_writable (target);
695 structure = gst_caps_get_structure (target, 0);
696 gst_structure_fixate_field_nearest_int (structure, "width", scope->width);
697 gst_structure_fixate_field_nearest_int (structure, "height", scope->height);
698 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
699 scope->fps_n, scope->fps_d);
701 GST_DEBUG_OBJECT (scope, "final caps are %" GST_PTR_FORMAT, target);
703 gst_base_audio_visualizer_src_setcaps (scope, target);
705 /* try to get a bufferpool now */
706 /* find a pool for the negotiated caps now */
707 query = gst_query_new_allocation (target, TRUE);
709 if (!gst_pad_peer_query (scope->srcpad, query)) {
710 /* not a problem, we use the query defaults */
711 GST_DEBUG_OBJECT (scope, "allocation query failed");
714 if (gst_query_get_n_allocation_pools (query) > 0) {
715 /* we got configuration from our peer, parse them */
716 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
724 /* we did not get a pool, make one ourselves then */
725 pool = gst_buffer_pool_new ();
728 config = gst_buffer_pool_get_config (pool);
729 gst_buffer_pool_config_set_params (config, target, size, min, max);
730 gst_buffer_pool_set_config (pool, config);
733 gst_buffer_pool_set_active (scope->pool, FALSE);
734 gst_object_unref (scope->pool);
739 gst_buffer_pool_set_active (pool, TRUE);
741 gst_caps_unref (target);
747 gst_caps_unref (target);
752 /* make sure we are negotiated */
754 gst_base_audio_visualizer_ensure_negotiated (GstBaseAudioVisualizer * scope)
756 gboolean reconfigure;
758 reconfigure = gst_pad_check_reconfigure (scope->srcpad);
760 /* we don't know an output format yet, pick one */
761 if (reconfigure || !gst_pad_has_current_caps (scope->srcpad)) {
762 if (!gst_base_audio_visualizer_src_negotiate (scope))
763 return GST_FLOW_NOT_NEGOTIATED;
769 gst_base_audio_visualizer_chain (GstPad * pad, GstObject * parent,
772 GstFlowReturn ret = GST_FLOW_OK;
773 GstBaseAudioVisualizer *scope;
774 GstBaseAudioVisualizerClass *klass;
779 gboolean (*render) (GstBaseAudioVisualizer * scope, GstBuffer * audio,
781 gint bps, channels, rate;
783 scope = GST_BASE_AUDIO_VISUALIZER (parent);
784 klass = GST_BASE_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
786 render = klass->render;
788 GST_LOG_OBJECT (scope, "chainfunc called");
790 /* resync on DISCONT */
791 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
792 gst_adapter_clear (scope->adapter);
795 /* Make sure have an output format */
796 ret = gst_base_audio_visualizer_ensure_negotiated (scope);
797 if (ret != GST_FLOW_OK) {
798 gst_buffer_unref (buffer);
801 channels = GST_AUDIO_INFO_CHANNELS (&scope->ainfo);
802 rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
803 bps = GST_AUDIO_INFO_BPS (&scope->ainfo);
806 ret = GST_FLOW_NOT_NEGOTIATED;
810 gst_adapter_push (scope->adapter, buffer);
812 g_mutex_lock (&scope->config_lock);
814 /* this is what we want */
815 sbpf = scope->req_spf * channels * sizeof (gint16);
817 inbuf = scope->inbuf;
818 /* FIXME: the timestamp in the adapter would be different */
819 gst_buffer_copy_into (inbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
821 /* this is what we have */
822 avail = gst_adapter_available (scope->adapter);
823 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
824 while (avail >= sbpf) {
828 /* get timestamp of the current adapter content */
829 ts = gst_adapter_prev_timestamp (scope->adapter, &dist);
830 if (GST_CLOCK_TIME_IS_VALID (ts)) {
831 /* convert bytes to time */
833 ts += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
836 if (GST_CLOCK_TIME_IS_VALID (ts)) {
841 gst_segment_to_running_time (&scope->segment, GST_FORMAT_TIME, ts) +
842 scope->frame_duration;
844 GST_OBJECT_LOCK (scope);
845 /* check for QoS, don't compute buffers that are known to be late */
846 need_skip = scope->earliest_time != -1 && qostime <= scope->earliest_time;
847 GST_OBJECT_UNLOCK (scope);
850 GST_WARNING_OBJECT (scope,
851 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
852 GST_TIME_ARGS (qostime), GST_TIME_ARGS (scope->earliest_time));
857 g_mutex_unlock (&scope->config_lock);
858 ret = gst_buffer_pool_acquire_buffer (scope->pool, &outbuf, NULL);
859 g_mutex_lock (&scope->config_lock);
860 /* recheck as the value could have changed */
861 sbpf = scope->req_spf * channels * sizeof (gint16);
863 /* no buffer allocated, we don't care why. */
864 if (ret != GST_FLOW_OK)
867 /* sync controlled properties */
868 gst_object_sync_values (GST_OBJECT (scope), ts);
870 GST_BUFFER_TIMESTAMP (outbuf) = ts;
871 GST_BUFFER_DURATION (outbuf) = scope->frame_duration;
873 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
875 memcpy (map.data, scope->pixelbuf, scope->bpf);
877 memset (map.data, 0, scope->bpf);
880 /* this can fail as the data size we need could have changed */
881 if (!(adata = (gpointer) gst_adapter_map (scope->adapter, sbpf)))
884 gst_buffer_append_memory (inbuf,
885 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, adata, sbpf, 0,
888 /* call class->render() vmethod */
890 if (!render (scope, inbuf, outbuf)) {
891 ret = GST_FLOW_ERROR;
893 /* run various post processing (shading and geometri transformation */
895 scope->shader (scope, map.data, scope->pixelbuf);
900 gst_buffer_unmap (outbuf, &map);
901 gst_buffer_resize (outbuf, 0, scope->bpf);
903 g_mutex_unlock (&scope->config_lock);
904 ret = gst_pad_push (scope->srcpad, outbuf);
906 g_mutex_lock (&scope->config_lock);
909 /* recheck as the value could have changed */
910 sbpf = scope->req_spf * channels * sizeof (gint16);
911 GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
912 /* we want to take less or more, depending on spf : req_spf */
913 if (avail - sbpf >= sbpf) {
914 gst_adapter_flush (scope->adapter, sbpf);
915 gst_adapter_unmap (scope->adapter);
916 } else if (avail >= sbpf) {
917 /* just flush a bit and stop */
918 gst_adapter_flush (scope->adapter, (avail - sbpf));
919 gst_adapter_unmap (scope->adapter);
922 avail = gst_adapter_available (scope->adapter);
924 if (ret != GST_FLOW_OK)
928 g_mutex_unlock (&scope->config_lock);
935 gst_base_audio_visualizer_src_event (GstPad * pad, GstObject * parent,
939 GstBaseAudioVisualizer *scope;
941 scope = GST_BASE_AUDIO_VISUALIZER (parent);
943 switch (GST_EVENT_TYPE (event)) {
947 GstClockTimeDiff diff;
948 GstClockTime timestamp;
950 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
952 /* save stuff for the _chain() function */
953 GST_OBJECT_LOCK (scope);
954 scope->proportion = proportion;
956 /* we're late, this is a good estimate for next displayable
957 * frame (see part-qos.txt) */
958 scope->earliest_time = timestamp + 2 * diff + scope->frame_duration;
960 scope->earliest_time = timestamp + diff;
961 GST_OBJECT_UNLOCK (scope);
963 res = gst_pad_push_event (scope->sinkpad, event);
967 res = gst_pad_push_event (scope->sinkpad, event);
975 gst_base_audio_visualizer_sink_event (GstPad * pad, GstObject * parent,
979 GstBaseAudioVisualizer *scope;
981 scope = GST_BASE_AUDIO_VISUALIZER (parent);
983 switch (GST_EVENT_TYPE (event)) {
988 gst_event_parse_caps (event, &caps);
989 res = gst_base_audio_visualizer_sink_setcaps (scope, caps);
992 case GST_EVENT_FLUSH_START:
993 res = gst_pad_push_event (scope->srcpad, event);
995 case GST_EVENT_FLUSH_STOP:
996 gst_base_audio_visualizer_reset (scope);
997 res = gst_pad_push_event (scope->srcpad, event);
999 case GST_EVENT_SEGMENT:
1001 /* the newsegment values are used to clip the input samples
1002 * and to convert the incomming timestamps to running time so
1004 gst_event_copy_segment (event, &scope->segment);
1006 res = gst_pad_push_event (scope->srcpad, event);
1010 res = gst_pad_push_event (scope->srcpad, event);
1018 gst_base_audio_visualizer_src_query (GstPad * pad, GstObject * parent,
1021 gboolean res = FALSE;
1022 GstBaseAudioVisualizer *scope;
1024 scope = GST_BASE_AUDIO_VISUALIZER (parent);
1026 switch (GST_QUERY_TYPE (query)) {
1027 case GST_QUERY_LATENCY:
1029 /* We need to send the query upstream and add the returned latency to our
1031 GstClockTime min_latency, max_latency;
1033 GstClockTime our_latency;
1035 gint rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
1040 if ((res = gst_pad_peer_query (scope->sinkpad, query))) {
1041 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1043 GST_DEBUG_OBJECT (scope, "Peer latency: min %"
1044 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1045 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1047 /* the max samples we must buffer buffer */
1048 max_samples = MAX (scope->req_spf, scope->spf);
1049 our_latency = gst_util_uint64_scale_int (max_samples, GST_SECOND, rate);
1051 GST_DEBUG_OBJECT (scope, "Our latency: %" GST_TIME_FORMAT,
1052 GST_TIME_ARGS (our_latency));
1054 /* we add some latency but only if we need to buffer more than what
1055 * upstream gives us */
1056 min_latency += our_latency;
1057 if (max_latency != -1)
1058 max_latency += our_latency;
1060 GST_DEBUG_OBJECT (scope, "Calculated total latency : min %"
1061 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1062 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1064 gst_query_set_latency (query, TRUE, min_latency, max_latency);
1069 res = gst_pad_query_default (pad, parent, query);
1077 gst_base_audio_visualizer_sink_query (GstPad * pad, GstObject * parent,
1080 gboolean res = FALSE;
1082 switch (GST_QUERY_TYPE (query)) {
1084 res = gst_pad_query_default (pad, parent, query);
1090 static GstStateChangeReturn
1091 gst_base_audio_visualizer_change_state (GstElement * element,
1092 GstStateChange transition)
1094 GstStateChangeReturn ret;
1095 GstBaseAudioVisualizer *scope;
1097 scope = GST_BASE_AUDIO_VISUALIZER (element);
1099 switch (transition) {
1100 case GST_STATE_CHANGE_READY_TO_PAUSED:
1101 gst_base_audio_visualizer_reset (scope);
1107 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1109 switch (transition) {
1110 case GST_STATE_CHANGE_PAUSED_TO_READY:
1112 gst_buffer_pool_set_active (scope->pool, FALSE);
1113 gst_object_replace ((GstObject **) & scope->pool, NULL);
1116 case GST_STATE_CHANGE_READY_TO_NULL: