2 * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gst/base/gstadapter.h>
26 #include <gst/video/video.h>
27 #include <gst/video/gstvideopool.h>
28 #include <gst/audio/audio.h>
29 #include <libvisual/libvisual.h>
31 #define GST_TYPE_VISUAL (gst_visual_get_type())
32 #define GST_IS_VISUAL(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VISUAL))
33 #define GST_VISUAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VISUAL,GstVisual))
34 #define GST_IS_VISUAL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VISUAL))
35 #define GST_VISUAL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VISUAL,GstVisualClass))
36 #define GST_VISUAL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_VISUAL, GstVisualClass))
38 typedef struct _GstVisual GstVisual;
39 typedef struct _GstVisualClass GstVisualClass;
41 GST_DEBUG_CATEGORY_STATIC (libvisual_debug);
42 #define GST_CAT_DEFAULT (libvisual_debug)
44 /* amounf of samples before we can feed libvisual */
45 #define VISUAL_SAMPLES 512
47 #define DEFAULT_WIDTH 320
48 #define DEFAULT_HEIGHT 240
49 #define DEFAULT_FPS_N 25
50 #define DEFAULT_FPS_D 1
66 /* audio/video state */
69 /* framerate numerator & denominator */
74 GstClockTime duration;
78 /* samples per frame based on caps */
85 /* QoS stuff *//* with LOCK */
87 GstClockTime earliest_time;
90 struct _GstVisualClass
92 GstElementClass parent_class;
97 GType gst_visual_get_type (void);
100 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
103 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (" { "
104 #if G_BYTE_ORDER == G_BIG_ENDIAN
105 "\"xRGB\", " "\"RGB\", "
107 "\"BGRx\", " "\"BGR\", "
112 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
115 GST_STATIC_CAPS ("audio/x-raw, "
116 "format = (string) " GST_AUDIO_NE (S16) ", "
117 "layout = (string) interleaved, " "channels = (int) { 1, 2 }, "
118 #if defined(VISUAL_API_VERSION) && VISUAL_API_VERSION >= 4000 && VISUAL_API_VERSION < 5000
119 "rate = (int) { 8000, 11250, 22500, 32000, 44100, 48000, 96000 }"
121 "rate = (int) [ 1000, MAX ]"
127 static void gst_visual_class_init (gpointer g_class, gpointer class_data);
128 static void gst_visual_init (GstVisual * visual);
129 static void gst_visual_finalize (GObject * object);
131 static GstStateChangeReturn gst_visual_change_state (GstElement * element,
132 GstStateChange transition);
133 static GstFlowReturn gst_visual_chain (GstPad * pad, GstObject * parent,
135 static gboolean gst_visual_sink_event (GstPad * pad, GstObject * parent,
137 static gboolean gst_visual_src_event (GstPad * pad, GstObject * parent,
140 static gboolean gst_visual_src_query (GstPad * pad, GstObject * parent,
143 static gboolean gst_visual_sink_setcaps (GstPad * pad, GstCaps * caps);
144 static GstCaps *gst_visual_getcaps (GstPad * pad, GstCaps * filter);
145 static void libvisual_log_handler (const char *message, const char *funcname,
148 static GstElementClass *parent_class = NULL;
151 gst_visual_get_type (void)
153 static GType type = 0;
155 if (G_UNLIKELY (type == 0)) {
156 static const GTypeInfo info = {
157 sizeof (GstVisualClass),
160 gst_visual_class_init,
165 (GInstanceInitFunc) gst_visual_init,
168 type = g_type_register_static (GST_TYPE_ELEMENT, "GstVisual", &info, 0);
174 libvisual_log_handler (const char *message, const char *funcname, void *priv)
176 GST_CAT_LEVEL_LOG (libvisual_debug, (GstDebugLevel) (priv), NULL, "%s - %s",
181 gst_visual_class_init (gpointer g_class, gpointer class_data)
183 GstVisualClass *klass = GST_VISUAL_CLASS (g_class);
184 GstElementClass *element = GST_ELEMENT_CLASS (g_class);
185 GObjectClass *object = G_OBJECT_CLASS (g_class);
187 klass->plugin = class_data;
189 element->change_state = gst_visual_change_state;
191 if (class_data == NULL) {
192 parent_class = g_type_class_peek_parent (g_class);
194 char *longname = g_strdup_printf ("libvisual %s plugin v.%s",
195 klass->plugin->info->name, klass->plugin->info->version);
197 /* FIXME: improve to only register what plugin supports? */
198 gst_element_class_add_pad_template (element,
199 gst_static_pad_template_get (&src_template));
200 gst_element_class_add_pad_template (element,
201 gst_static_pad_template_get (&sink_template));
203 gst_element_class_set_details_simple (element,
204 longname, "Visualization",
205 klass->plugin->info->about, "Benjamin Otte <otte@gnome.org>");
210 object->finalize = gst_visual_finalize;
214 gst_visual_init (GstVisual * visual)
216 /* create the sink and src pads */
217 visual->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
218 gst_pad_set_chain_function (visual->sinkpad, gst_visual_chain);
219 gst_pad_set_event_function (visual->sinkpad, gst_visual_sink_event);
220 gst_element_add_pad (GST_ELEMENT (visual), visual->sinkpad);
222 visual->srcpad = gst_pad_new_from_static_template (&src_template, "src");
223 gst_pad_set_event_function (visual->srcpad, gst_visual_src_event);
224 gst_pad_set_query_function (visual->srcpad, gst_visual_src_query);
225 gst_element_add_pad (GST_ELEMENT (visual), visual->srcpad);
227 visual->adapter = gst_adapter_new ();
231 gst_visual_clear_actors (GstVisual * visual)
234 visual_object_unref (VISUAL_OBJECT (visual->actor));
235 visual->actor = NULL;
238 visual_object_unref (VISUAL_OBJECT (visual->video));
239 visual->video = NULL;
242 visual_object_unref (VISUAL_OBJECT (visual->audio));
243 visual->audio = NULL;
248 gst_visual_finalize (GObject * object)
250 GstVisual *visual = GST_VISUAL (object);
252 g_object_unref (visual->adapter);
254 gst_object_unref (visual->pool);
255 gst_visual_clear_actors (visual);
257 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
261 gst_visual_reset (GstVisual * visual)
263 gst_adapter_clear (visual->adapter);
264 gst_segment_init (&visual->segment, GST_FORMAT_UNDEFINED);
266 GST_OBJECT_LOCK (visual);
267 visual->proportion = 1.0;
268 visual->earliest_time = -1;
269 GST_OBJECT_UNLOCK (visual);
273 gst_visual_getcaps (GstPad * pad, GstCaps * filter)
276 GstVisual *visual = GST_VISUAL (GST_PAD_PARENT (pad));
279 if (!visual->actor) {
280 ret = gst_pad_get_pad_template_caps (visual->srcpad);
284 ret = gst_caps_new_empty ();
285 depths = visual_actor_get_supported_depth (visual->actor);
287 /* FIXME: set an error */
290 if (depths == VISUAL_VIDEO_DEPTH_GL) {
291 /* We can't handle GL only plugins */
295 GST_DEBUG_OBJECT (visual, "libvisual plugin supports depths %u (0x%04x)",
297 /* if (depths & VISUAL_VIDEO_DEPTH_32BIT) Always supports 32bit output */
298 #if G_BYTE_ORDER == G_BIG_ENDIAN
299 gst_caps_append (ret, gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("xRGB")));
301 gst_caps_append (ret, gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGRx")));
304 if (depths & VISUAL_VIDEO_DEPTH_24BIT) {
305 #if G_BYTE_ORDER == G_BIG_ENDIAN
306 gst_caps_append (ret, gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("RGB")));
308 gst_caps_append (ret, gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGR")));
311 if (depths & VISUAL_VIDEO_DEPTH_16BIT) {
312 gst_caps_append (ret, gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("RGB16")));
318 GstCaps *intersection;
321 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
322 gst_caps_unref (ret);
326 GST_DEBUG_OBJECT (visual, "returning caps %" GST_PTR_FORMAT, ret);
332 gst_visual_src_setcaps (GstVisual * visual, GstCaps * caps)
335 GstStructure *structure;
336 gint depth, pitch, rate;
339 structure = gst_caps_get_structure (caps, 0);
341 GST_DEBUG_OBJECT (visual, "src pad got caps %" GST_PTR_FORMAT, caps);
343 if (!gst_structure_get_int (structure, "width", &visual->width))
345 if (!gst_structure_get_int (structure, "height", &visual->height))
347 if (!(fmt = gst_structure_get_string (structure, "format")))
349 if (!gst_structure_get_fraction (structure, "framerate", &visual->fps_n,
353 if (!strcmp (fmt, "BGR") || !strcmp (fmt, "RGB"))
355 else if (!strcmp (fmt, "BGRx") || !strcmp (fmt, "xRGB"))
360 visual_video_set_depth (visual->video,
361 visual_video_depth_enum_from_value (depth));
362 visual_video_set_dimension (visual->video, visual->width, visual->height);
363 pitch = GST_ROUND_UP_4 (visual->width * visual->video->bpp);
364 visual_video_set_pitch (visual->video, pitch);
365 visual_actor_video_negotiate (visual->actor, 0, FALSE, FALSE);
367 rate = GST_AUDIO_INFO_RATE (&visual->info);
369 /* precalc some values */
370 visual->outsize = visual->video->height * pitch;
371 visual->spf = gst_util_uint64_scale_int (rate, visual->fps_d, visual->fps_n);
373 gst_util_uint64_scale_int (GST_SECOND, visual->fps_d, visual->fps_n);
375 res = gst_pad_push_event (visual->srcpad, gst_event_new_caps (caps));
382 GST_DEBUG_OBJECT (visual, "error parsing caps");
388 gst_visual_sink_setcaps (GstPad * pad, GstCaps * caps)
390 GstVisual *visual = GST_VISUAL (GST_PAD_PARENT (pad));
394 if (!gst_audio_info_from_caps (&info, caps))
399 rate = GST_AUDIO_INFO_RATE (&info);
401 /* this is how many samples we need to fill one frame at the requested
403 if (visual->fps_n != 0) {
405 gst_util_uint64_scale_int (rate, visual->fps_d, visual->fps_n);
413 GST_ERROR_OBJECT (visual, "invalid caps received");
419 gst_vis_src_negotiate (GstVisual * visual)
421 GstCaps *othercaps, *target;
422 GstStructure *structure;
425 GstBufferPool *pool = NULL;
426 GstStructure *config;
427 guint size, min, max;
429 caps = gst_pad_query_caps (visual->srcpad, NULL);
431 /* see what the peer can do */
432 othercaps = gst_pad_peer_query_caps (visual->srcpad, caps);
435 gst_caps_unref (caps);
437 if (gst_caps_is_empty (target))
440 target = gst_caps_truncate (target);
442 /* need a copy, we'll be modifying it when fixating */
443 target = gst_caps_ref (caps);
445 GST_DEBUG_OBJECT (visual, "before fixate caps %" GST_PTR_FORMAT, target);
447 target = gst_caps_make_writable (target);
448 /* fixate in case something is not fixed. This does nothing if the value is
449 * already fixed. For video we always try to fixate to something like
450 * 320x240x25 by convention. */
451 structure = gst_caps_get_structure (target, 0);
452 gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
453 gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
454 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
455 DEFAULT_FPS_N, DEFAULT_FPS_D);
456 target = gst_caps_fixate (target);
458 GST_DEBUG_OBJECT (visual, "after fixate caps %" GST_PTR_FORMAT, target);
460 gst_visual_src_setcaps (visual, target);
462 /* try to get a bufferpool now */
463 /* find a pool for the negotiated caps now */
464 query = gst_query_new_allocation (target, TRUE);
466 if (!gst_pad_peer_query (visual->srcpad, query)) {
467 /* not a problem, we deal with the defaults of the query */
468 GST_DEBUG_OBJECT (visual, "allocation query failed");
471 if (gst_query_get_n_allocation_pools (query) > 0) {
472 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
474 size = MAX (size, visual->outsize);
477 size = visual->outsize;
482 /* no pool, just parameters, we can make our own */
483 GST_DEBUG_OBJECT (visual, "no pool, making new pool");
484 pool = gst_video_buffer_pool_new ();
488 config = gst_buffer_pool_get_config (pool);
489 gst_buffer_pool_config_set_params (config, target, size, min, max);
490 gst_buffer_pool_set_config (pool, config);
493 gst_object_unref (visual->pool);
497 gst_buffer_pool_set_active (pool, TRUE);
499 gst_caps_unref (target);
506 GST_ELEMENT_ERROR (visual, STREAM, FORMAT, (NULL),
507 ("could not negotiate output format"));
508 gst_caps_unref (target);
514 gst_visual_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
519 visual = GST_VISUAL (parent);
521 switch (GST_EVENT_TYPE (event)) {
522 case GST_EVENT_FLUSH_START:
523 res = gst_pad_push_event (visual->srcpad, event);
525 case GST_EVENT_FLUSH_STOP:
526 /* reset QoS and adapter. */
527 gst_visual_reset (visual);
528 res = gst_pad_push_event (visual->srcpad, event);
534 gst_event_parse_caps (event, &caps);
535 res = gst_visual_sink_setcaps (pad, caps);
536 gst_event_unref (event);
539 case GST_EVENT_SEGMENT:
541 /* the newsegment values are used to clip the input samples
542 * and to convert the incomming timestamps to running time so
544 gst_event_copy_segment (event, &visual->segment);
547 res = gst_pad_push_event (visual->srcpad, event);
551 res = gst_pad_event_default (pad, parent, event);
559 gst_visual_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
564 visual = GST_VISUAL (parent);
566 switch (GST_EVENT_TYPE (event)) {
570 GstClockTimeDiff diff;
571 GstClockTime timestamp;
573 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
575 /* save stuff for the _chain function */
576 GST_OBJECT_LOCK (visual);
577 visual->proportion = proportion;
579 /* we're late, this is a good estimate for next displayable
580 * frame (see part-qos.txt) */
581 visual->earliest_time = timestamp + 2 * diff + visual->duration;
583 visual->earliest_time = timestamp + diff;
585 GST_OBJECT_UNLOCK (visual);
587 res = gst_pad_push_event (visual->sinkpad, event);
590 case GST_EVENT_RECONFIGURE:
592 gst_event_unref (event);
596 res = gst_pad_event_default (pad, parent, event);
604 gst_visual_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
609 visual = GST_VISUAL (parent);
611 switch (GST_QUERY_TYPE (query)) {
612 case GST_QUERY_LATENCY:
614 /* We need to send the query upstream and add the returned latency to our
616 GstClockTime min_latency, max_latency;
618 GstClockTime our_latency;
621 if ((res = gst_pad_peer_query (visual->sinkpad, query))) {
622 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
624 GST_DEBUG_OBJECT (visual, "Peer latency: min %"
625 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
626 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
628 /* the max samples we must buffer */
629 max_samples = MAX (VISUAL_SAMPLES, visual->spf);
631 gst_util_uint64_scale_int (max_samples, GST_SECOND,
632 GST_AUDIO_INFO_RATE (&visual->info));
634 GST_DEBUG_OBJECT (visual, "Our latency: %" GST_TIME_FORMAT,
635 GST_TIME_ARGS (our_latency));
637 /* we add some latency but only if we need to buffer more than what
638 * upstream gives us */
639 min_latency += our_latency;
640 if (max_latency != -1)
641 max_latency += our_latency;
643 GST_DEBUG_OBJECT (visual, "Calculated total latency : min %"
644 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
645 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
647 gst_query_set_latency (query, TRUE, min_latency, max_latency);
653 GstCaps *filter, *caps;
655 gst_query_parse_caps (query, &filter);
656 caps = gst_visual_getcaps (pad, filter);
657 gst_query_set_caps_result (query, caps);
658 gst_caps_unref (caps);
662 res = gst_pad_query_default (pad, parent, query);
669 /* Make sure we are negotiated */
671 ensure_negotiated (GstVisual * visual)
673 gboolean reconfigure;
675 reconfigure = gst_pad_check_reconfigure (visual->srcpad);
677 /* we don't know an output format yet, pick one */
678 if (reconfigure || !gst_pad_has_current_caps (visual->srcpad)) {
679 if (!gst_vis_src_negotiate (visual))
680 return GST_FLOW_NOT_NEGOTIATED;
686 gst_visual_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
688 GstBuffer *outbuf = NULL;
690 GstVisual *visual = GST_VISUAL (parent);
691 GstFlowReturn ret = GST_FLOW_OK;
693 gint bpf, rate, channels;
695 GST_DEBUG_OBJECT (visual, "chain function called");
697 /* Make sure have an output format */
698 ret = ensure_negotiated (visual);
699 if (ret != GST_FLOW_OK) {
700 gst_buffer_unref (buffer);
704 /* resync on DISCONT */
705 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
706 gst_adapter_clear (visual->adapter);
709 rate = GST_AUDIO_INFO_RATE (&visual->info);
710 bpf = GST_AUDIO_INFO_BPF (&visual->info);
711 channels = GST_AUDIO_INFO_CHANNELS (&visual->info);
713 GST_DEBUG_OBJECT (visual,
714 "Input buffer has %" G_GSIZE_FORMAT " samples, time=%" G_GUINT64_FORMAT,
715 gst_buffer_get_size (buffer) / bpf, GST_BUFFER_TIMESTAMP (buffer));
717 gst_adapter_push (visual->adapter, buffer);
722 guint64 dist, timestamp;
725 GST_DEBUG_OBJECT (visual, "processing buffer");
727 avail = gst_adapter_available (visual->adapter);
728 GST_DEBUG_OBJECT (visual, "avail now %u", avail);
730 /* we need at least VISUAL_SAMPLES samples */
731 if (avail < VISUAL_SAMPLES * bpf)
734 /* we need at least enough samples to make one frame */
735 if (avail < visual->spf * bpf)
738 /* get timestamp of the current adapter byte */
739 timestamp = gst_adapter_prev_timestamp (visual->adapter, &dist);
740 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
741 /* convert bytes to time */
743 timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
746 if (timestamp != -1) {
749 /* QoS is done on running time */
750 qostime = gst_segment_to_running_time (&visual->segment, GST_FORMAT_TIME,
752 qostime += visual->duration;
754 GST_OBJECT_LOCK (visual);
755 /* check for QoS, don't compute buffers that are known to be late */
756 need_skip = visual->earliest_time != -1 &&
757 qostime <= visual->earliest_time;
758 GST_OBJECT_UNLOCK (visual);
761 GST_WARNING_OBJECT (visual,
762 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
763 GST_TIME_ARGS (qostime), GST_TIME_ARGS (visual->earliest_time));
768 /* Read VISUAL_SAMPLES samples per channel */
770 (const guint16 *) gst_adapter_map (visual->adapter,
771 VISUAL_SAMPLES * bpf);
773 #if defined(VISUAL_API_VERSION) && VISUAL_API_VERSION >= 4000 && VISUAL_API_VERSION < 5000
775 VisBuffer *lbuf, *rbuf;
776 guint16 ldata[VISUAL_SAMPLES], rdata[VISUAL_SAMPLES];
777 VisAudioSampleRateType vrate;
779 lbuf = visual_buffer_new_with_buffer (ldata, sizeof (ldata), NULL);
780 rbuf = visual_buffer_new_with_buffer (rdata, sizeof (rdata), NULL);
783 for (i = 0; i < VISUAL_SAMPLES; i++) {
788 for (i = 0; i < VISUAL_SAMPLES; i++) {
796 vrate = VISUAL_AUDIO_SAMPLE_RATE_8000;
799 vrate = VISUAL_AUDIO_SAMPLE_RATE_11250;
802 vrate = VISUAL_AUDIO_SAMPLE_RATE_22500;
805 vrate = VISUAL_AUDIO_SAMPLE_RATE_32000;
808 vrate = VISUAL_AUDIO_SAMPLE_RATE_44100;
811 vrate = VISUAL_AUDIO_SAMPLE_RATE_48000;
814 vrate = VISUAL_AUDIO_SAMPLE_RATE_96000;
817 visual_object_unref (VISUAL_OBJECT (lbuf));
818 visual_object_unref (VISUAL_OBJECT (rbuf));
819 GST_ERROR_OBJECT (visual, "unsupported rate %d", rate);
820 ret = GST_FLOW_ERROR;
825 visual_audio_samplepool_input_channel (visual->audio->samplepool,
827 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
828 (char *) VISUAL_AUDIO_CHANNEL_LEFT);
829 visual_audio_samplepool_input_channel (visual->audio->samplepool, rbuf,
830 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
831 (char *) VISUAL_AUDIO_CHANNEL_RIGHT);
833 visual_object_unref (VISUAL_OBJECT (lbuf));
834 visual_object_unref (VISUAL_OBJECT (rbuf));
838 if (visual->channels == 2) {
839 for (i = 0; i < VISUAL_SAMPLES; i++) {
840 visual->audio->plugpcm[0][i] = *data++;
841 visual->audio->plugpcm[1][i] = *data++;
844 for (i = 0; i < VISUAL_SAMPLES; i++) {
845 visual->audio->plugpcm[0][i] = *data;
846 visual->audio->plugpcm[1][i] = *data++;
851 /* alloc a buffer if we don't have one yet, this happens
852 * when we pushed a buffer in this while loop before */
853 if (outbuf == NULL) {
854 GST_DEBUG_OBJECT (visual, "allocating output buffer");
855 ret = gst_buffer_pool_acquire_buffer (visual->pool, &outbuf, NULL);
856 if (ret != GST_FLOW_OK) {
857 gst_adapter_unmap (visual->adapter);
861 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
862 visual_video_set_buffer (visual->video, outmap.data);
863 visual_audio_analyze (visual->audio);
864 visual_actor_run (visual->actor, visual->audio);
865 visual_video_set_buffer (visual->video, NULL);
866 gst_buffer_unmap (outbuf, &outmap);
867 GST_DEBUG_OBJECT (visual, "rendered one frame");
869 gst_adapter_unmap (visual->adapter);
871 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
872 GST_BUFFER_DURATION (outbuf) = visual->duration;
874 ret = gst_pad_push (visual->srcpad, outbuf);
878 GST_DEBUG_OBJECT (visual, "finished frame, flushing %u samples from input",
881 /* Flush out the number of samples per frame */
882 gst_adapter_flush (visual->adapter, visual->spf * bpf);
884 /* quit the loop if something was wrong */
885 if (ret != GST_FLOW_OK)
892 gst_buffer_unref (outbuf);
897 static GstStateChangeReturn
898 gst_visual_change_state (GstElement * element, GstStateChange transition)
900 GstVisual *visual = GST_VISUAL (element);
901 GstStateChangeReturn ret;
903 switch (transition) {
904 case GST_STATE_CHANGE_NULL_TO_READY:
906 visual_actor_new (GST_VISUAL_GET_CLASS (visual)->plugin->
908 visual->video = visual_video_new ();
909 visual->audio = visual_audio_new ();
910 /* can't have a play without actors */
911 if (!visual->actor || !visual->video)
914 if (visual_actor_realize (visual->actor) != 0)
917 visual_actor_set_video (visual->actor, visual->video);
919 case GST_STATE_CHANGE_READY_TO_PAUSED:
920 gst_visual_reset (visual);
922 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
928 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
930 switch (transition) {
931 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
933 case GST_STATE_CHANGE_PAUSED_TO_READY:
935 gst_buffer_pool_set_active (visual->pool, FALSE);
936 gst_object_unref (visual->pool);
940 case GST_STATE_CHANGE_READY_TO_NULL:
941 gst_visual_clear_actors (visual);
952 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
953 ("could not create actors"));
954 gst_visual_clear_actors (visual);
955 return GST_STATE_CHANGE_FAILURE;
959 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
960 ("could not realize actor"));
961 gst_visual_clear_actors (visual);
962 return GST_STATE_CHANGE_FAILURE;
967 make_valid_name (char *name)
970 * Replace invalid chars with _ in the type name
972 static const gchar extra_chars[] = "-_+";
976 int valid = ((p[0] >= 'A' && p[0] <= 'Z') ||
977 (p[0] >= 'a' && p[0] <= 'z') ||
978 (p[0] >= '0' && p[0] <= '9') || strchr (extra_chars, p[0]));
985 gst_visual_actor_plugin_is_gl (VisObject * plugin, const gchar * name)
990 #if !defined(VISUAL_API_VERSION)
992 depth = VISUAL_PLUGIN_ACTOR (plugin)->depth;
993 is_gl = (depth == VISUAL_VIDEO_DEPTH_GL);
995 #elif VISUAL_API_VERSION >= 4000 && VISUAL_API_VERSION < 5000
997 depth = VISUAL_ACTOR_PLUGIN (plugin)->vidoptions.depth;
998 /* FIXME: how to figure this out correctly in 0.4? */
999 is_gl = (depth & VISUAL_VIDEO_DEPTH_GL) == VISUAL_VIDEO_DEPTH_GL;
1002 # error what libvisual version is this?
1006 GST_DEBUG ("plugin %s is not a GL plugin (%d), registering", name, depth);
1008 GST_DEBUG ("plugin %s is a GL plugin (%d), ignoring", name, depth);
1015 plugin_init (GstPlugin * plugin)
1020 GST_DEBUG_CATEGORY_INIT (libvisual_debug, "libvisual", 0,
1021 "libvisual audio visualisations");
1023 #ifdef LIBVISUAL_PLUGINSBASEDIR
1024 gst_plugin_add_dependency_simple (plugin, "HOME/.libvisual/actor",
1025 LIBVISUAL_PLUGINSBASEDIR "/actor", NULL, GST_PLUGIN_DEPENDENCY_FLAG_NONE);
1028 visual_log_set_verboseness (VISUAL_LOG_VERBOSENESS_LOW);
1029 visual_log_set_info_handler (libvisual_log_handler, (void *) GST_LEVEL_INFO);
1030 visual_log_set_warning_handler (libvisual_log_handler,
1031 (void *) GST_LEVEL_WARNING);
1032 visual_log_set_critical_handler (libvisual_log_handler,
1033 (void *) GST_LEVEL_ERROR);
1034 visual_log_set_error_handler (libvisual_log_handler,
1035 (void *) GST_LEVEL_ERROR);
1037 if (!visual_is_initialized ())
1038 if (visual_init (NULL, NULL) != 0)
1041 list = visual_actor_get_list ();
1043 #if !defined(VISUAL_API_VERSION)
1044 count = visual_list_count (list);
1045 #elif VISUAL_API_VERSION >= 4000 && VISUAL_API_VERSION < 5000
1046 count = visual_collection_size (VISUAL_COLLECTION (list));
1049 for (i = 0; i < count; i++) {
1050 VisPluginRef *ref = visual_list_get (list, i);
1051 VisPluginData *visplugin = NULL;
1052 gboolean skip = FALSE;
1056 sizeof (GstVisualClass),
1059 gst_visual_class_init,
1067 visplugin = visual_plugin_load (ref);
1069 if (ref->info->plugname == NULL)
1072 /* Blacklist some plugins */
1073 if (strcmp (ref->info->plugname, "gstreamer") == 0 ||
1074 strcmp (ref->info->plugname, "gdkpixbuf") == 0) {
1077 /* Ignore plugins that only support GL output for now */
1078 skip = gst_visual_actor_plugin_is_gl (visplugin->info->plugin,
1079 visplugin->info->plugname);
1082 visual_plugin_unload (visplugin);
1085 name = g_strdup_printf ("GstVisual%s", ref->info->plugname);
1086 make_valid_name (name);
1087 type = g_type_register_static (GST_TYPE_VISUAL, name, &info, 0);
1090 name = g_strdup_printf ("libvisual_%s", ref->info->plugname);
1091 make_valid_name (name);
1092 if (!gst_element_register (plugin, name, GST_RANK_NONE, type)) {
1103 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1106 "libvisual visualization plugins",
1107 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)