- Removed unused locking from the cothreads
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gsttypefind.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23
24 #include "gst_private.h"
25
26 #include "gsttype.h"
27
28 #include "gstlog.h"
29 #include "gsttypefind.h"
30
31 /* #define GST_DEBUG_ENABLED */
32
33 GstElementDetails gst_type_find_details = {
34   "TypeFind",
35   "Generic",
36   "Finds the media type of a stream",
37   VERSION,
38   "Erik Walthinsen <omega@cse.ogi.edu>"
39   "Wim Taymans <wim.taymans@chello.be>",
40   "(C) 1999",
41 };
42
43
44 /* TypeFind signals and args */
45 enum {
46   HAVE_TYPE,
47   LAST_SIGNAL
48 };
49
50 enum {
51   ARG_0,
52   ARG_CAPS,
53 };
54
55
56 static void     gst_type_find_class_init                (GstTypeFindClass *klass);
57 static void     gst_type_find_init              (GstTypeFind *typefind);
58
59 static void     gst_type_find_set_property      (GObject *object, guint prop_id, 
60                                                  const GValue *value, GParamSpec *pspec);
61 static void     gst_type_find_get_property      (GObject *object, guint prop_id, 
62                                                  GValue *value, GParamSpec *pspec);
63
64 static void     gst_type_find_chain             (GstPad *pad, GstBuffer *buf);
65
66 static GstElementClass *parent_class = NULL;
67 static guint gst_type_find_signals[LAST_SIGNAL] = { 0 };
68
69 GType
70 gst_type_find_get_type (void)
71 {
72   static GType typefind_type = 0;
73
74   if (!typefind_type) {
75     static const GTypeInfo typefind_info = {
76       sizeof(GstTypeFindClass),
77       NULL,
78       NULL,
79       (GClassInitFunc)gst_type_find_class_init,
80       NULL,
81       NULL,
82       sizeof(GstTypeFind),
83       0,
84       (GInstanceInitFunc)gst_type_find_init,
85       NULL
86     };
87     typefind_type = g_type_register_static (GST_TYPE_ELEMENT, "GstTypeFind", &typefind_info, 0);
88   }
89   return typefind_type;
90 }
91
92 static void
93 gst_type_find_class_init (GstTypeFindClass *klass)
94 {
95   GObjectClass *gobject_class;
96
97   gobject_class = (GObjectClass*)klass;
98
99   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
100
101   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CAPS,
102     g_param_spec_pointer("caps", "Caps", "Found capabilities", G_PARAM_READABLE));
103
104   gst_type_find_signals[HAVE_TYPE] =
105       g_signal_new ("have_type", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
106                      G_STRUCT_OFFSET (GstTypeFindClass, have_type), NULL, NULL,
107                      g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
108                      G_TYPE_POINTER);
109
110   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_type_find_set_property);
111   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_type_find_get_property);
112 }
113
114 static void
115 gst_type_find_init (GstTypeFind *typefind)
116 {
117   typefind->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
118   gst_element_add_pad (GST_ELEMENT (typefind), typefind->sinkpad);
119   gst_pad_set_chain_function (typefind->sinkpad, gst_type_find_chain);
120 }
121
122 static void
123 gst_type_find_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
124 {
125   GstTypeFind *typefind;
126
127   /* it's not null if we got it, but it might not be ours */
128   g_return_if_fail (GST_IS_TYPE_FIND (object));
129
130   typefind = GST_TYPE_FIND (object);
131
132   switch (prop_id) {
133     default:
134       break;
135   }
136 }
137
138 static void
139 gst_type_find_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
140 {
141   GstTypeFind *typefind;
142
143   /* it's not null if we got it, but it might not be ours */
144   g_return_if_fail (GST_IS_TYPE_FIND (object));
145
146   typefind = GST_TYPE_FIND (object);
147
148   switch (prop_id) {
149     case ARG_CAPS:
150       g_value_set_pointer(value, typefind->caps);
151       break;
152     default:
153       break;
154   }
155 }
156
157 static void
158 gst_type_find_chain (GstPad *pad, GstBuffer *buf)
159 {
160   GstTypeFind *typefind;
161   GList *type_list;
162   GstType *type;
163
164   g_return_if_fail (pad != NULL);
165   g_return_if_fail (GST_IS_PAD (pad));
166   g_return_if_fail (buf != NULL);
167
168   typefind = GST_TYPE_FIND (GST_OBJECT_PARENT (pad));
169   GST_DEBUG (0,"got buffer of %d bytes in '%s'",
170         GST_BUFFER_SIZE (buf), GST_OBJECT_NAME (typefind));
171
172   type_list = (GList *) gst_type_get_list ();
173
174   while (type_list) {
175     GSList *factories;
176     type = (GstType *)type_list->data;
177
178     factories = type->factories;
179
180     while (factories) {
181       GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
182       GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
183       GstCaps *caps;
184
185       GST_DEBUG (0,"try type (%p) :%d \"%s\" %p", factory, type->id, type->mime, typefindfunc);
186       if (typefindfunc && (caps = typefindfunc (buf, factory))) {
187         GST_DEBUG (0,"found type :%d \"%s\" \"%s\"", caps->id, type->mime, 
188                         gst_caps_get_name (caps));
189         typefind->caps = caps;
190
191         if (!gst_pad_try_set_caps (pad, caps)) {
192           g_warning ("typefind: found type but peer didn't accept it");
193         }
194
195         {
196           /* int oldstate = GST_STATE(typefind);*/
197           gst_object_ref (GST_OBJECT (typefind));
198           g_signal_emit (G_OBJECT (typefind), gst_type_find_signals[HAVE_TYPE], 0,
199                               typefind->caps);
200 /*          if (GST_STATE(typefind) != oldstate) {
201             GST_DEBUG(0, "state changed during signal, aborting");
202             gst_element_interrupt (GST_ELEMENT (typefind));
203             } */
204           gst_object_unref (GST_OBJECT (typefind));
205
206           goto end;
207         }
208       }
209       factories = g_slist_next (factories);
210     }
211
212     type_list = g_list_next (type_list);
213   }
214 end:
215   gst_buffer_unref (buf);
216 }