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 #define DEFAULT_MAX_BUFFERS 1
31 GstElementDetails gst_type_find_details = {
35 "Finds the media type of a stream",
37 "Erik Walthinsen <omega@cse.ogi.edu>,"
38 "Wim Taymans <wim.taymans@chello.be>",
43 /* TypeFind signals and args */
56 static void gst_type_find_class_init (GstTypeFindClass *klass);
57 static void gst_type_find_init (GstTypeFind *typefind);
59 static void gst_type_find_set_property (GObject *object, guint prop_id,
62 static void gst_type_find_get_property (GObject *object, guint prop_id,
66 static void gst_type_find_chain (GstPad *pad, GstBuffer *buf);
67 static GstElementStateReturn
68 gst_type_find_change_state (GstElement *element);
70 static GstElementClass *parent_class = NULL;
71 static guint gst_type_find_signals[LAST_SIGNAL] = { 0 };
74 gst_type_find_get_type (void)
76 static GType typefind_type = 0;
79 static const GTypeInfo typefind_info = {
80 sizeof(GstTypeFindClass),
83 (GClassInitFunc)gst_type_find_class_init,
88 (GInstanceInitFunc)gst_type_find_init,
91 typefind_type = g_type_register_static (GST_TYPE_ELEMENT, "GstTypeFind", &typefind_info, 0);
97 gst_type_find_class_init (GstTypeFindClass *klass)
99 GObjectClass *gobject_class;
100 GstElementClass *gstelement_class;
102 gobject_class = (GObjectClass*)klass;
103 gstelement_class = (GstElementClass*)klass;
105 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
107 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CAPS,
108 g_param_spec_pointer ("caps", "Caps", "Found capabilities", G_PARAM_READABLE));
109 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_BUFFERS,
110 g_param_spec_int ("max_buffers",
112 "Maximal amount of buffers before giving en error (0 == unlimited)",
113 0, G_MAXINT, DEFAULT_MAX_BUFFERS, G_PARAM_READWRITE));
115 gst_type_find_signals[HAVE_TYPE] =
116 g_signal_new ("have_type", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
117 G_STRUCT_OFFSET (GstTypeFindClass, have_type), NULL, NULL,
118 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
121 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_type_find_set_property);
122 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_type_find_get_property);
124 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_type_find_change_state);
128 gst_type_find_init (GstTypeFind *typefind)
130 typefind->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
131 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sinkpad);
132 gst_pad_set_chain_function (typefind->sinkpad, gst_type_find_chain);
134 typefind->num_buffer = 0;
135 typefind->max_buffers = DEFAULT_MAX_BUFFERS;
136 typefind->caps = NULL;
140 gst_type_find_set_property (GObject *object, guint prop_id,
141 const GValue *value, GParamSpec *pspec)
143 GstTypeFind *typefind;
145 g_return_if_fail (GST_IS_TYPE_FIND (object));
147 typefind = GST_TYPE_FIND (object);
150 case ARG_MAX_BUFFERS:
151 typefind->max_buffers = g_value_get_int (value);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 gst_type_find_get_property (GObject *object, guint prop_id,
161 GValue *value, GParamSpec *pspec)
163 GstTypeFind *typefind;
165 g_return_if_fail (GST_IS_TYPE_FIND (object));
167 typefind = GST_TYPE_FIND (object);
171 g_value_set_pointer (value, typefind->caps);
173 case ARG_MAX_BUFFERS:
174 g_value_set_int (value, typefind->max_buffers);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gst_type_find_chain (GstPad *pad, GstBuffer *buf)
185 GstTypeFind *typefind;
186 const GList *type_list;
189 g_return_if_fail (GST_IS_PAD (pad));
191 typefind = GST_TYPE_FIND (GST_OBJECT_PARENT (pad));
193 GST_DEBUG (0,"got buffer of %d bytes in '%s'",
194 GST_BUFFER_SIZE (buf), GST_OBJECT_NAME (typefind));
196 type_list = gst_type_get_list ();
200 type = (GstType *) type_list->data;
202 factories = type->factories;
205 GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
206 GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
209 GST_DEBUG (GST_CAT_TYPES, "try type (%p) :%d \"%s\" %p",
210 factory, type->id, type->mime, typefindfunc);
211 if (typefindfunc && (caps = typefindfunc (buf, factory))) {
212 GST_DEBUG (GST_CAT_TYPES, "found type: %d \"%s\" \"%s\"",
213 caps->id, type->mime, gst_caps_get_name (caps));
214 typefind->caps = caps;
216 if (gst_pad_try_set_caps (pad, caps) <= 0) {
217 g_warning ("typefind: found type but peer didn't accept it");
221 gst_object_ref (GST_OBJECT (typefind));
222 g_signal_emit (G_OBJECT (typefind), gst_type_find_signals[HAVE_TYPE], 0,
224 gst_object_unref (GST_OBJECT (typefind));
228 factories = g_slist_next (factories);
230 type_list = g_list_next (type_list);
233 typefind->num_buffer++;
236 gst_buffer_unref (buf);
238 if (typefind->num_buffer >= typefind->max_buffers) {
239 gst_element_error (GST_ELEMENT (typefind),
240 "typefind could not determine type after %d buffers", typefind->num_buffer);
244 static GstElementStateReturn
245 gst_type_find_change_state (GstElement *element)
247 GstTypeFind *typefind;
248 GstElementStateReturn ret;
250 typefind = GST_TYPE_FIND (element);
252 switch (GST_STATE_TRANSITION (element)) {
253 case GST_STATE_NULL_TO_READY:
255 case GST_STATE_READY_TO_PAUSED:
256 typefind->num_buffer = 0;
257 gst_caps_unref (typefind->caps);
258 typefind->caps = NULL;
260 case GST_STATE_PAUSED_TO_PLAYING:
261 case GST_STATE_PLAYING_TO_PAUSED:
262 case GST_STATE_PAUSED_TO_READY:
263 case GST_STATE_READY_TO_NULL:
267 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);