[MOVED FROM GST-P-FARSIGHT] Fix leak
[platform/upstream/gstreamer.git] / gst / rtpmux / gstrtpdtmfmux.c
1 /* RTP DTMF muxer element for GStreamer
2  *
3  * gstrtpdtmfmux.c:
4  *
5  * Copyright (C) <2007> Nokia Corporation.
6  *   Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
7  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
8  *               2000,2005 Wim Taymans <wim@fluendo.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /**
27  * SECTION:element-rtpdtmfmux
28  * @short_description: mixes RTP DTMF streams into other RTP streams
29  * <refsect2>
30  * <para>
31  * The RTPDTMFMuxer mixes/muxes RTP DTMF stream(s) into other RTP
32  * streams. It does exactly what it's parent (RTPMuxer) does, except
33  * that it allows upstream peer elements to request exclusive access
34  * to the stream, which is required by the RTP DTMF standards (see RFC
35  * 2833, section 3.2, para 1 for details). The peer upstream element
36  * requests the acquisition and release of a stream lock beginning
37  * using custom downstream gstreamer events. To request the acquisition
38  * of the lock, the peer element must send an event of type
39  * GST_EVENT_CUSTOM_DOWNSTREAM_OOB, having a
40  * structure of name "stream-lock" with only one boolean field:
41  * "lock". If this field is set to TRUE, the request is for the
42  * acquisition of the lock, otherwise it is for release of the lock.
43  * </para>
44  * <para>For example, the following code in an upstream peer element
45  * requests the acquisition of the stream lock:
46  * </para>
47  * <para>
48  * <programlisting>
49  * GstEvent *event;
50  * GstStructure *structure;
51  * GstPad *srcpad;
52  *
53  * ... /\* srcpad initialization goes here \*\/
54  *
55  * structure = gst_structure_new ("stream-lock",
56  *                    "lock", G_TYPE_BOOLEAN, TRUE, NULL);
57  *
58  * event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
59  * gst_pad_push_event (dtmfsrc->srcpad, event);
60  * </programlisting>
61  * </para>
62  * </refsect2>
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include <gst/gst.h>
70 #include <gstrtpdtmfmux.h>
71 #include <string.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
74 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
75
76 /* elementfactory information */
77 static const GstElementDetails gst_rtp_dtmf_mux_details =
78 GST_ELEMENT_DETAILS ("RTP muxer",
79     "Codec/Muxer",
80     "mixes RTP DTMF streams into other RTP streams",
81     "Zeeshan Ali <first.last@nokia.com>");
82
83 static void gst_rtp_dtmf_mux_base_init (gpointer g_class);
84 static void gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass);
85 static void gst_rtp_dtmf_mux_finalize (GObject * object);
86
87 static gboolean gst_rtp_dtmf_mux_sink_event (GstPad * pad,
88     GstEvent * event);
89 static GstFlowReturn gst_rtp_dtmf_mux_chain (GstPad * pad,
90     GstBuffer * buffer);
91
92 static GstRTPMuxClass *parent_class = NULL;
93
94 GType
95 gst_rtp_dtmf_mux_get_type (void)
96 {
97   static GType mux_type = 0;
98
99   if (!mux_type) {
100     static const GTypeInfo mux_info = {
101       sizeof (GstRTPDTMFMuxClass),
102       gst_rtp_dtmf_mux_base_init,
103       NULL,
104       (GClassInitFunc) gst_rtp_dtmf_mux_class_init,
105       NULL,
106       NULL,
107       sizeof (GstRTPDTMFMux),
108       0,
109       (GInstanceInitFunc) NULL,
110     };
111
112     mux_type =
113         g_type_register_static (GST_TYPE_RTP_MUX, "GstRTPDTMFMux",
114         &mux_info, 0);
115   }
116   return mux_type;
117 }
118
119 static void
120 gst_rtp_dtmf_mux_base_init (gpointer g_class)
121 {
122   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
123
124   gst_element_class_set_details (element_class, &gst_rtp_dtmf_mux_details);
125 }
126
127 static void
128 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
129 {
130   GObjectClass *gobject_class;
131   GstElementClass *gstelement_class;
132   GstRTPMuxClass *gstrtpmux_class;
133
134   gobject_class = (GObjectClass *) klass;
135   gstelement_class = (GstElementClass *) klass;
136   gstrtpmux_class = (GstRTPMuxClass *) klass;
137
138   parent_class = g_type_class_peek_parent (klass);
139
140   gobject_class->finalize = gst_rtp_dtmf_mux_finalize;
141   gstrtpmux_class->chain_func = gst_rtp_dtmf_mux_chain;
142   gstrtpmux_class->sink_event_func = gst_rtp_dtmf_mux_sink_event;
143 }
144
145 static void
146 gst_rtp_dtmf_mux_finalize (GObject * object)
147 {
148   GstRTPDTMFMux *mux;
149
150   mux = GST_RTP_DTMF_MUX (object);
151
152   G_OBJECT_CLASS (parent_class)->finalize (object);
153 }
154
155 static GstFlowReturn
156 gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer)
157 {
158   GstRTPDTMFMux *mux;
159   GstFlowReturn ret;
160
161   mux = GST_RTP_DTMF_MUX (gst_pad_get_parent (pad));
162
163   GST_OBJECT_LOCK (mux);
164   if (mux->special_pad != NULL && mux->special_pad != pad) {
165     /* Drop the buffer */
166     gst_buffer_unref (buffer);
167     ret = GST_FLOW_OK;
168     GST_OBJECT_UNLOCK (mux);
169   }
170
171   else {
172     GST_OBJECT_UNLOCK (mux);
173     if (parent_class->chain_func)
174       ret = parent_class->chain_func (pad, buffer);
175     else
176       ret = GST_FLOW_ERROR;
177   }
178
179   gst_object_unref (mux);
180   return ret;
181 }
182
183 static void
184 gst_rtp_dtmf_mux_lock_stream (GstRTPDTMFMux *mux, GstPad * pad)
185 {
186   if (mux->special_pad != NULL)
187     GST_WARNING_OBJECT (mux,
188             "Stream lock already acquired by pad %s",
189             GST_ELEMENT_NAME (mux->special_pad));
190
191   else {
192     GST_DEBUG_OBJECT (mux,
193             "Stream lock acquired by pad %s",
194             GST_ELEMENT_NAME (pad));
195     mux->special_pad = gst_object_ref (pad);
196   }
197 }
198
199 static void
200 gst_rtp_dtmf_mux_unlock_stream (GstRTPDTMFMux *mux, GstPad * pad)
201 {
202   if (mux->special_pad == NULL)
203     GST_WARNING_OBJECT (mux,
204             "Stream lock not acquired, can't release it");
205
206   else if (pad != mux->special_pad)
207     GST_WARNING_OBJECT (mux,
208             "pad %s attempted to release Stream lock"
209             " which was acquired by pad %s", GST_ELEMENT_NAME (pad),
210             GST_ELEMENT_NAME (mux->special_pad));
211   else {
212     GST_DEBUG_OBJECT (mux,
213             "Stream lock released by pad %s",
214             GST_ELEMENT_NAME (mux->special_pad));
215     gst_object_unref (mux->special_pad);
216     mux->special_pad = NULL;
217   }
218 }
219
220 static gboolean
221 gst_rtp_dtmf_mux_handle_stream_lock_event (GstRTPDTMFMux *mux, GstPad * pad,
222         const GstStructure * event_structure)
223 {
224   gboolean lock;
225
226   if (!gst_structure_get_boolean (event_structure, "lock", &lock))
227     return FALSE;
228
229   GST_OBJECT_LOCK (mux);
230   if (lock)
231     gst_rtp_dtmf_mux_lock_stream (mux, pad);
232   else
233     gst_rtp_dtmf_mux_unlock_stream (mux, pad);
234   GST_OBJECT_UNLOCK (mux);
235
236   return TRUE;
237 }
238
239 static gboolean
240 gst_rtp_dtmf_mux_handle_downstream_event (GstRTPDTMFMux *mux,
241         GstPad * pad, GstEvent * event)
242 {
243   const GstStructure *structure;
244   gboolean ret = FALSE;
245
246   structure = gst_event_get_structure (event);
247   /* FIXME: is this event generic enough to be given a generic name? */
248   if (structure && gst_structure_has_name (structure, "stream-lock"))
249     ret = gst_rtp_dtmf_mux_handle_stream_lock_event (mux, pad, structure);
250
251   return ret;
252 }
253
254 static gboolean
255 gst_rtp_dtmf_mux_ignore_event (GstPad * pad, GstEvent * event)
256 {
257   gboolean ret;
258
259   if (parent_class->sink_event_func)
260     /* Give the parent a chance to handle the event first */
261     ret = parent_class->sink_event_func (pad, event);
262
263   else
264     ret = gst_pad_event_default (pad, event);
265
266   return ret;
267 }
268
269 static gboolean
270 gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event)
271 {
272   GstRTPDTMFMux *mux;
273   GstEventType type;
274   gboolean ret = FALSE;
275
276   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
277
278   mux = (GstRTPDTMFMux *) gst_pad_get_parent (pad);
279
280   switch (type) {
281     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
282       ret = gst_rtp_dtmf_mux_handle_downstream_event (mux, pad, event);
283       gst_event_unref (event);
284       break;
285     default:
286       ret = gst_rtp_dtmf_mux_ignore_event (pad, event);
287       break;
288   }
289
290   gst_object_unref (mux);
291   return ret;
292 }
293
294 gboolean
295 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
296 {
297   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
298       "rtp dtmf muxer");
299
300   return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
301       GST_TYPE_RTP_DTMF_MUX);
302 }
303