2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 #include "gst_private.h"
27 #include "gsttypefind.h"
29 GstElementDetails gst_type_find_details = {
33 "Finds the media type of a stream",
35 "Erik Walthinsen <omega@cse.ogi.edu>,"
36 "Wim Taymans <wim.taymans@chello.be>",
40 /* generic templates */
41 GST_PAD_TEMPLATE_FACTORY (type_find_sink_factory,
48 /* TypeFind signals and args */
60 static void gst_type_find_class_init (GstTypeFindClass *klass);
61 static void gst_type_find_init (GstTypeFind *typefind);
63 static void gst_type_find_set_property (GObject *object, guint prop_id,
66 static void gst_type_find_get_property (GObject *object, guint prop_id,
70 static void gst_type_find_loopfunc (GstElement *element);
71 static GstElementStateReturn
72 gst_type_find_change_state (GstElement *element);
74 static GstElementClass *parent_class = NULL;
75 static guint gst_type_find_signals[LAST_SIGNAL] = { 0 };
78 gst_type_find_get_type (void)
80 static GType typefind_type = 0;
83 static const GTypeInfo typefind_info = {
84 sizeof(GstTypeFindClass),
87 (GClassInitFunc)gst_type_find_class_init,
92 (GInstanceInitFunc)gst_type_find_init,
95 typefind_type = g_type_register_static (GST_TYPE_ELEMENT,
103 gst_type_find_class_init (GstTypeFindClass *klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass*)klass;
109 gstelement_class = (GstElementClass*)klass;
111 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
113 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CAPS,
114 g_param_spec_pointer ("caps", "Caps", "Found capabilities", G_PARAM_READABLE));
116 gst_type_find_signals[HAVE_TYPE] =
117 g_signal_new ("have_type", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
118 G_STRUCT_OFFSET (GstTypeFindClass, have_type), NULL, NULL,
119 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
122 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_type_find_set_property);
123 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_type_find_get_property);
125 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_type_find_change_state);
129 gst_type_find_init (GstTypeFind *typefind)
131 typefind->sinkpad = gst_pad_new_from_template (
132 GST_PAD_TEMPLATE_GET (type_find_sink_factory), "sink");
133 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sinkpad);
135 gst_element_set_loop_function (GST_ELEMENT (typefind),
136 gst_type_find_loopfunc);
138 typefind->caps = NULL;
142 gst_type_find_set_property (GObject *object, guint prop_id,
143 const GValue *value, GParamSpec *pspec)
145 GstTypeFind *typefind;
147 g_return_if_fail (GST_IS_TYPE_FIND (object));
149 typefind = GST_TYPE_FIND (object);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_type_find_get_property (GObject *object, guint prop_id,
160 GValue *value, GParamSpec *pspec)
162 GstTypeFind *typefind;
164 g_return_if_fail (GST_IS_TYPE_FIND (object));
166 typefind = GST_TYPE_FIND (object);
170 g_value_set_pointer (value, typefind->caps);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gst_type_find_loopfunc (GstElement *element)
181 GstTypeFind *typefind;
182 const GList *type_list;
185 typefind = GST_TYPE_FIND (element);
187 GST_DEBUG ("Started typefinding loop in '%s'",
188 GST_OBJECT_NAME (typefind));
190 type_list = gst_type_get_list ();
194 type = (GstType *) type_list->data;
196 factories = type->factories;
199 GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
200 GstTypeFindFunc typefindfunc = (GstTypeFindFunc) factory->typefindfunc;
203 GST_CAT_DEBUG (GST_CAT_TYPES, "try type (%p) :%d \"%s\" %p",
204 factory, type->id, type->mime, typefindfunc);
205 if (typefindfunc && (caps = typefindfunc (typefind->bs, factory))) {
206 GST_CAT_DEBUG (GST_CAT_TYPES, "found type: %d \"%s\" \"%s\"",
207 caps->id, type->mime, gst_caps_get_name (caps));
208 gst_caps_replace (&typefind->caps, caps);
210 if (gst_pad_try_set_caps (typefind->sinkpad, caps) <= 0) {
211 g_warning ("typefind: found type but peer didn't accept it");
214 gst_object_ref (GST_OBJECT (typefind));
215 g_signal_emit (G_OBJECT (typefind), gst_type_find_signals[HAVE_TYPE],
217 gst_object_unref (GST_OBJECT (typefind));
220 factories = g_slist_next (factories);
222 type_list = g_list_next (type_list);
225 /* if we get here, nothing worked... :'(. */
226 gst_element_error (GST_ELEMENT (typefind),
227 "media type could not be detected");
230 static GstElementStateReturn
231 gst_type_find_change_state (GstElement *element)
233 GstTypeFind *typefind;
234 GstElementStateReturn ret;
236 typefind = GST_TYPE_FIND (element);
238 switch (GST_STATE_TRANSITION (element)) {
239 case GST_STATE_READY_TO_PAUSED:
240 typefind->bs = gst_bytestream_new (typefind->sinkpad);
242 case GST_STATE_PAUSED_TO_READY:
243 gst_bytestream_destroy (typefind->bs);
244 gst_caps_replace (&typefind->caps, NULL);
250 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);