2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
24 #include <glib/gi18n.h>
27 #include <gst/gsterror.h>
28 #include <gst/gstplugin.h>
29 #include <gst/interfaces/xoverlay.h>
31 //#include <gst/pbutils/pbutils.h>
32 #include "gstplaybinmaemo.h"
35 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_maemo_debug);
36 #define GST_CAT_DEFAULT gst_play_bin_maemo_debug
38 #define DEFAULT_VOLUME 10
39 #define DEFAULT_XID -1
47 ARG_QUEUE_MIN_THRESHOLD,
54 static const GstElementDetails gst_play_bin_maemo_details =
55 GST_ELEMENT_DETAILS("Nuv demuxer",
57 "Autoplug and play media from an uri used on maemo plataform",
58 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>");
60 static void gst_play_bin_maemo_dispose (GObject * object);
61 static void gst_play_bin_maemo_finalize (GObject * object);
62 static void gst_play_bin_maemo_set_property (GObject * object, guint prop_id,
63 const GValue * value, GParamSpec * spec);
64 static void gst_play_bin_maemo_get_property (GObject * object, guint prop_id,
65 GValue * value, GParamSpec * spec);
66 static GstStateChangeReturn
67 gst_play_bin_maemo_change_state (GstElement *element,
68 GstStateChange transition);
69 static gboolean factory_filter_sinks (GstPluginFeature *feature,
70 GstPlayBinMaemo *pbm);
71 static gint compare_ranks (GstPluginFeature * f1,
72 GstPluginFeature * f2);
73 static GList *find_compatibles (GstPlayBinMaemo *pbm,
75 static GstPad *find_sink_pad (GstElement * element);
76 static void update_volume (GstPlayBinMaemo *pbm);
77 static void update_xid (GstPlayBinMaemo *pbm);
78 static void new_decoded_pad_cb (GstElement *object,
82 static void unknown_type_cb (GstElement *object,
86 static gboolean autoplug_continue_cb (GstElement* object,
89 static void decode_new_pad_cb (GstElement *element,
92 static void queue_underrun_cb (GstElement* queue,
94 static void queue_sink_underrun_cb (GstElement* queue,
96 static void queue_sink_overrun_cb (GstElement* queue,
103 GST_BOILERPLATE(GstPlayBinMaemo, gst_play_bin_maemo, GstPipeline, GST_TYPE_PIPELINE)
107 gst_play_bin_maemo_base_init (gpointer klass)
109 GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
111 gst_element_class_set_details (element_class, &gst_play_bin_maemo_details);
115 gst_play_bin_maemo_class_init (GstPlayBinMaemoClass * klass)
117 GObjectClass *gobject_klass;
118 GstElementClass *gstelement_klass;
119 GstBinClass *gstbin_klass;
121 gobject_klass = (GObjectClass *) klass;
122 gstelement_klass = (GstElementClass *) klass;
123 gstbin_klass = (GstBinClass *) klass;
125 parent_class = g_type_class_peek_parent (klass);
127 gobject_klass->set_property = gst_play_bin_maemo_set_property;
128 gobject_klass->get_property = gst_play_bin_maemo_get_property;
130 g_object_class_install_property (gobject_klass, ARG_URI,
131 g_param_spec_string ("uri", "URI", "URI of the media to play",
132 NULL, G_PARAM_READWRITE));
134 g_object_class_install_property (gobject_klass, ARG_VOLUME,
135 g_param_spec_uint ("volume", "Audio volume", "volume",
136 0, 10, (guint) DEFAULT_VOLUME, G_PARAM_READWRITE));
138 g_object_class_install_property (gobject_klass, ARG_XID,
139 g_param_spec_long ("xid", "xid", "X windown ID",
140 -1, G_MAXLONG, DEFAULT_XID, G_PARAM_READWRITE));
142 g_object_class_install_property (gobject_klass, ARG_SOURCE,
143 g_param_spec_object ("source", "Source", "Source element",
144 GST_TYPE_ELEMENT, G_PARAM_READABLE));
146 g_object_class_install_property (gobject_klass, ARG_PARSE_METADATA,
147 g_param_spec_boolean ("parse-metadata", "Parse Metadata", "Parse metadata info",
148 TRUE, G_PARAM_READWRITE));
151 GST_DEBUG_CATEGORY_INIT (gst_play_bin_maemo_debug, "playbinmaemo", 0,
154 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_maemo_dispose);
155 gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_play_bin_maemo_finalize);
157 gstelement_klass->change_state =
158 GST_DEBUG_FUNCPTR (gst_play_bin_maemo_change_state);
162 gst_play_bin_maemo_init (GstPlayBinMaemo * play_bin_maemo, GstPlayBinMaemoClass *class)
166 play_bin_maemo->uri = NULL;
167 play_bin_maemo->source = NULL;
169 play_bin_maemo->volume = DEFAULT_VOLUME * 65535 / 10;
170 play_bin_maemo->xid = DEFAULT_XID;
171 play_bin_maemo->parse_metadata = TRUE;
173 factories = gst_default_registry_feature_filter ((GstPluginFeatureFilter) factory_filter_sinks,
174 FALSE, play_bin_maemo);
176 play_bin_maemo->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
180 gst_play_bin_maemo_dispose (GObject * object)
182 GstPlayBinMaemo *play_bin_maemo;
184 play_bin_maemo = GST_PLAY_BIN_MAEMO (object);
185 g_free (play_bin_maemo->uri);
186 play_bin_maemo->uri = NULL;
188 G_OBJECT_CLASS (parent_class)->dispose (object);
192 gst_play_bin_maemo_finalize (GObject * object)
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 array_has_value (const gchar * values[], const gchar * value)
202 for (i = 0; values[i]; i++) {
203 if (g_str_has_prefix (value, values[i]))
209 /* list of URIs that we consider to be streams and that need buffering.
210 * We have no mechanism yet to figure this out with a query. */
211 static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
212 "mmsu://", "mmst://", NULL
215 /* blacklisted URIs, we know they will always fail. */
216 static const gchar *blacklisted_uris[] = { NULL };
218 /* mime types that we don't consider to be media types */
219 static const gchar *no_media_mimes[] = {
220 "application/x-executable", "application/x-bzip", "application/x-gzip",
221 "application/zip", "application/x-compress", NULL
224 /* mime types we consider raw media */
225 static const gchar *raw_mimes[] = {
226 "audio/x-raw", "video/x-raw", NULL
229 #define IS_STREAM_URI(uri) (array_has_value (stream_uris, uri))
230 #define IS_BLACKLISTED_URI(uri) (array_has_value (blacklisted_uris, uri))
231 #define IS_NO_MEDIA_MIME(mime) (array_has_value (no_media_mimes, mime))
232 #define IS_RAW_MIME(mime) (array_has_value (raw_mimes, mime))
235 * Generate and configure a source element.
238 gen_source_element (GstPlayBinMaemo * play_bin_maemo)
242 if (!play_bin_maemo->uri)
245 if (!gst_uri_is_valid (play_bin_maemo->uri))
248 if (IS_BLACKLISTED_URI (play_bin_maemo->uri))
249 goto uri_blacklisted;
251 source = gst_element_make_from_uri (GST_URI_SRC, play_bin_maemo->uri,
256 play_bin_maemo->is_stream = IS_STREAM_URI (play_bin_maemo->uri);
258 /* make HTTP sources send extra headers so we get icecast
259 * metadata in case the stream is an icecast stream */
260 if (!strncmp (play_bin_maemo->uri, "http://", 7) &&
261 g_object_class_find_property (G_OBJECT_GET_CLASS (source),
263 g_object_set (source, "iradio-mode", TRUE, NULL);
270 GST_ELEMENT_ERROR (play_bin_maemo, RESOURCE, NOT_FOUND,
271 (_("No URI specified to play from.")), (NULL));
276 GST_ELEMENT_ERROR (play_bin_maemo, RESOURCE, NOT_FOUND,
277 (_("Invalid URI \"%s\"."), play_bin_maemo->uri), (NULL));
282 GST_ELEMENT_ERROR (play_bin_maemo, RESOURCE, FAILED,
283 (_("RTSP streams cannot be played yet.")), (NULL));
288 gchar *prot = gst_uri_get_protocol (play_bin_maemo->uri);
290 /* whoops, could not create the source element, dig a little deeper to
291 * figure out what might be wrong. */
296 gst_element_post_message (GST_ELEMENT (play_bin_maemo),
297 gst_missing_uri_source_message_new (GST_ELEMENT (play_bin_maemo),
300 desc = gst_pb_utils_get_source_description (prot);
301 GST_ELEMENT_ERROR (play_bin_maemo, CORE, MISSING_PLUGIN,
302 (_("A %s plugin is required to play this stream, but not installed."),
303 desc), ("No URI handler for %s", prot));
315 remove_source (GstPlayBinMaemo *pbm)
317 GstElement *source = pbm->source;
320 GST_DEBUG_OBJECT (pbm, "removing old src element");
321 gst_element_set_state (source, GST_STATE_NULL);
322 gst_bin_remove (GST_BIN_CAST (pbm), source);
328 remove_decoders (GstPlayBinMaemo *pbm)
330 if (pbm->queue != NULL) {
331 gst_element_set_state (pbm->queue, GST_STATE_NULL);
332 gst_bin_remove (GST_BIN_CAST (pbm), pbm->queue);
336 if (pbm->decoder != NULL) {
337 gst_element_set_state (pbm->decoder, GST_STATE_NULL);
338 gst_bin_remove (GST_BIN_CAST (pbm), pbm->decoder);
344 remove_sinks (GstPlayBinMaemo *pbm)
348 for(walk=pbm->sinks; walk != NULL; walk = walk->next) {
349 GstElement *element = (GstElement *) walk->data;
351 gst_element_set_state (element, GST_STATE_NULL);
352 gst_bin_remove (GST_BIN_CAST (pbm), element);
355 g_slist_free (pbm->sinks);
360 prepare_elements (GstPlayBinMaemo *pbm)
362 if (pbm->decoder == NULL) {
363 pbm->decoder = gst_element_factory_make ("decodebin2", "decode");
364 gst_bin_add (GST_BIN (pbm), pbm->decoder);
365 g_signal_connect (G_OBJECT (pbm->decoder),
367 G_CALLBACK (autoplug_continue_cb),
369 g_signal_connect (G_OBJECT (pbm->decoder),
371 G_CALLBACK (unknown_type_cb),
373 g_signal_connect (G_OBJECT (pbm->decoder),
375 G_CALLBACK (new_decoded_pad_cb),
379 if (pbm->queue == NULL) {
380 pbm->queue = gst_element_factory_make ("queue", NULL);
381 gst_bin_add (GST_BIN (pbm), pbm->queue);
384 if (gst_element_link_many (pbm->source, pbm->queue, pbm->decoder, NULL) == FALSE) {
385 g_warning ("FAIL TO LINK SRC WITH DECODEBIN2");
390 setup_source (GstPlayBinMaemo *pbm)
392 if (!pbm->need_rebuild)
395 GST_DEBUG_OBJECT (pbm, "setup source");
397 pbm->has_metadata = FALSE;
402 /* create and configure an element that can handle the uri */
403 if (!(pbm->source = gen_source_element (pbm)))
407 gst_bin_add (GST_BIN_CAST (pbm), pbm->source);
409 remove_decoders (pbm);
414 if (verify_src_have_sink (pbm)) {
415 /* source can be linked with sinks directly */
420 prepare_elements (pbm);
429 gst_play_bin_maemo_set_property (GObject *object,
434 GstPlayBinMaemo *play_bin_maemo;
436 g_return_if_fail (GST_IS_PLAY_BIN_MAEMO (object));
438 play_bin_maemo = GST_PLAY_BIN_MAEMO (object);
443 const gchar *uri = g_value_get_string (value);
446 g_warning ("cannot set NULL uri");
449 /* if we have no previous uri, or the new uri is different from the
451 if (play_bin_maemo->uri == NULL || strcmp (play_bin_maemo->uri, uri) != 0) {
452 g_free (play_bin_maemo->uri);
453 play_bin_maemo->uri = g_strdup (uri);
455 GST_DEBUG ("setting new uri to %s", uri);
457 play_bin_maemo->need_rebuild = TRUE;
464 volume = g_value_get_uint (value);
466 volume = (guint) (65535 * volume / 10);
469 if (play_bin_maemo->volume != volume) {
470 play_bin_maemo->volume = volume;
471 update_volume (play_bin_maemo);
478 xid = g_value_get_long (value);
479 if (play_bin_maemo->xid != xid) {
480 play_bin_maemo->xid = xid;
481 update_xid (play_bin_maemo);
485 case ARG_PARSE_METADATA:
486 play_bin_maemo->parse_metadata = g_value_get_boolean (value);
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gst_play_bin_maemo_get_property (GObject * object, guint prop_id, GValue * value,
498 GstPlayBinMaemo *play_bin_maemo;
500 g_return_if_fail (GST_IS_PLAY_BIN_MAEMO (object));
502 play_bin_maemo = GST_PLAY_BIN_MAEMO (object);
506 g_value_set_string (value, play_bin_maemo->uri);
509 g_value_set_object (value, play_bin_maemo->source);
512 g_value_set_uint (value, play_bin_maemo->volume);
515 g_value_set_long (value, play_bin_maemo->xid);
517 case ARG_PARSE_METADATA:
518 g_value_set_boolean (value, play_bin_maemo->parse_metadata);
521 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
526 static GstStateChangeReturn
527 gst_play_bin_maemo_change_state (GstElement * element, GstStateChange transition)
529 GstStateChangeReturn ret;
530 GstPlayBinMaemo *play_bin_maemo;
532 play_bin_maemo = GST_PLAY_BIN_MAEMO (element);
534 switch (transition) {
535 case GST_STATE_CHANGE_READY_TO_PAUSED:
536 if (!setup_source (play_bin_maemo))
543 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
545 switch (transition) {
546 case GST_STATE_CHANGE_READY_TO_PAUSED:
547 if (ret == GST_STATE_CHANGE_FAILURE) {
548 play_bin_maemo->need_rebuild = TRUE;
549 return GST_STATE_CHANGE_FAILURE;
552 /* clean-up in both cases, READY=>NULL clean-up is if there was an error */
553 case GST_STATE_CHANGE_PAUSED_TO_READY:
554 case GST_STATE_CHANGE_READY_TO_NULL:
555 play_bin_maemo->need_rebuild = TRUE;
556 remove_decoders (play_bin_maemo);
557 remove_source (play_bin_maemo);
567 play_bin_maemo->need_rebuild = TRUE;
569 return GST_STATE_CHANGE_FAILURE;
574 factory_filter_sinks (GstPluginFeature *feature,
575 GstPlayBinMaemo *pbm)
580 if (!GST_IS_ELEMENT_FACTORY (feature))
583 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
585 if ((strstr (klass, "Sink/Video") == NULL) && (strstr (klass, "Sink/Audio") == NULL))
588 g_debug ("Fitered: %s", gst_element_factory_get_longname ((GST_ELEMENT_FACTORY (feature))));
589 rank = gst_plugin_feature_get_rank (feature);
590 if (rank < GST_RANK_MARGINAL)
597 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
600 const gchar *rname1, *rname2;
602 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
606 rname1 = gst_plugin_feature_get_name (f1);
607 rname2 = gst_plugin_feature_get_name (f2);
609 diff = strcmp (rname2, rname1);
616 find_compatibles (GstPlayBinMaemo *pbm, const GstCaps *caps)
619 GList *to_try = NULL;
621 /* loop over all the factories */
622 for (factories = pbm->factories; factories; factories = g_list_next (factories)) {
623 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
624 const GList *templates;
627 /* get the templates from the element factory */
628 templates = gst_element_factory_get_static_pad_templates (factory);
629 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
630 GstStaticPadTemplate *templ = walk->data;
632 /* we only care about the sink templates */
633 if (templ->direction == GST_PAD_SINK) {
637 /* try to intersect the caps with the caps of the template */
638 tmpl_caps = gst_static_caps_get (&templ->static_caps);
640 intersect = gst_caps_intersect (caps, tmpl_caps);
641 gst_caps_unref (tmpl_caps);
643 /* check if the intersection is empty */
644 if (!gst_caps_is_empty (intersect)) {
645 /* non empty intersection, we can use this element */
646 to_try = g_list_prepend (to_try, factory);
647 gst_caps_unref (intersect);
650 gst_caps_unref (intersect);
654 to_try = g_list_reverse (to_try);
661 autoplug_continue_cb (GstElement* object,
667 GstPlayBinMaemo *pbm;
669 pbm = GST_PLAY_BIN_MAEMO (user_data);
671 //TODO: fix this for work with all metada elements
672 if (pbm->parse_metadata) {
673 gchar *caps_str = gst_caps_to_string (caps);
674 if ((strstr (caps_str, "id3") != NULL) &&
675 (pbm->has_metadata == FALSE)) {
678 pbm->has_metadata = TRUE;
684 comp = find_compatibles (GST_PLAY_BIN_MAEMO (user_data), caps);
694 unknown_type_cb (GstElement *object,
699 g_debug ("unknown_type_cb: %s", gst_caps_to_string (caps));
703 find_sink_pad (GstElement * element)
709 it = gst_element_iterate_sink_pads (element);
711 if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
712 pad = (GstPad *) point;
714 gst_iterator_free (it);
720 create_element (GstPlayBinMaemo *pbm, GstElementFactory *factory)
722 GstElement *ret = NULL;
725 element = gst_element_factory_create (factory, NULL);
729 if (strstr (gst_element_factory_get_klass (factory), "Sink/Video") != NULL) {
731 pbm->sink_video = element;
733 } else if (strstr (gst_element_factory_get_klass (factory), "Sink/Audio") != NULL) {
734 GParamSpec *vol_spec;
736 vol_spec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), "volume");
737 if (vol_spec == NULL) {
741 bin = gst_bin_new (NULL);
742 volume = gst_element_factory_make ("volume", "volume");
743 gst_bin_add (GST_BIN (bin), volume);
744 gst_bin_add (GST_BIN (bin), element);
745 if (gst_element_link (volume, element) == FALSE) {
746 GST_WARNING_OBJECT (pbm, "Fail to link volume and sink audio: %s", GST_ELEMENT_NAME (element));
747 gst_element_set_state (bin, GST_STATE_NULL);
748 gst_object_unref (bin);
751 pbm->volume_element = volume;
755 pbm->volume_element = element;
756 g_param_spec_unref (vol_spec);
766 new_decoded_pad_cb (GstElement *object,
775 GstPlayBinMaemo *pbm;
777 pbm = GST_PLAY_BIN_MAEMO (user_data);
778 caps = gst_pad_get_caps (pad);
780 g_debug ("new_decoded_pad_cb: %s", gst_caps_to_string (caps));
782 comp = find_compatibles (GST_PLAY_BIN_MAEMO (user_data), caps);
786 g_warning ("flow error: dont find comaptible");
790 GST_PAD_STREAM_LOCK (pad);
793 for (walk=comp; walk != NULL; walk = walk->next) {
794 GstElementFactory *factory = (GstElementFactory *) walk->data;
798 if ((element = create_element (pbm, factory)) == NULL) {
799 GST_WARNING_OBJECT (pbm, "Could not create an element from %s",
800 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
804 if (!(gst_bin_add (GST_BIN (user_data), element))) {
805 GST_WARNING_OBJECT (pbm, "Couldn't set %s to READY", GST_ELEMENT_NAME (element));
806 gst_object_unref (element);
810 if ((gst_element_set_state (element, GST_STATE_READY))
811 == GST_STATE_CHANGE_FAILURE) {
812 gst_element_set_state (element, GST_STATE_NULL);
813 gst_object_unref (sinkpad);
814 gst_bin_remove (GST_BIN (user_data), element);
818 if (!(sinkpad = find_sink_pad (element))) {
819 GST_WARNING_OBJECT (pbm, "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
820 gst_object_unref (element);
825 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
826 GST_WARNING_OBJECT (pbm, "Link failed on pad %s:%s", GST_DEBUG_PAD_NAME (sinkpad));
827 gst_element_set_state (element, GST_STATE_NULL);
828 gst_object_unref (sinkpad);
829 gst_bin_remove (GST_BIN (user_data), element);
833 gst_object_unref (sinkpad);
835 if ((gst_element_set_state (element, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
836 gst_element_set_state (element, GST_STATE_NULL);
837 gst_bin_remove (GST_BIN (user_data), element);
842 pbm->sinks = g_slist_append (pbm->sinks, element);
848 if (linked == FALSE) {
849 g_warning ("GstFlow ERROR");
851 GST_PAD_STREAM_UNLOCK (pad);
855 update_volume (GstPlayBinMaemo *pbm)
857 if (pbm->volume_element != NULL) {
858 if (pbm->volume > 0) {
859 g_object_set (G_OBJECT (pbm->volume_element),
860 "volume", pbm->volume,
863 g_object_set (G_OBJECT (pbm->volume_element),
871 update_xid (GstPlayBinMaemo *pbm)
873 if ((pbm->sink_video != NULL) &&
875 (GST_IS_X_OVERLAY (pbm->sink_video))) {
877 g_object_set (G_OBJECT (pbm->sink_video),
878 "force-aspect-ratio", TRUE, NULL);
879 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (pbm->sink_video),
885 plugin_init(GstPlugin * plugin)
888 setlocale(LC_ALL, "");
889 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
890 #endif /* ENABLE_NLS */
892 if (!gst_element_register(plugin, "playbinmaemo", GST_RANK_SECONDARY,
893 GST_TYPE_PLAY_BIN_MAEMO)) {
900 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR,
903 "Demuxes and muxes audio and video",
904 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,