rtpmux: gst_element_class_set_details => gst_element_class_set_details_simple
[platform/upstream/gstreamer.git] / gst / rtpmanager / 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  * @see_also: rtpdtmfsrc, dtmfsrc
29  *
30  * The RTPDTMFMuxer mixes/muxes RTP DTMF stream(s) into other RTP
31  * streams. It does exactly what it's parent (RTPMuxer) does, except
32  * that it allows upstream peer elements to request exclusive access
33  * to the stream, which is required by the RTP DTMF standards (see RFC
34  * 2833, section 3.2, para 1 for details). The peer upstream element
35  * requests the acquisition and release of a stream lock beginning
36  * using custom downstream gstreamer events. To request the acquisition
37  * of the lock, the peer element must send an event of type
38  * GST_EVENT_CUSTOM_DOWNSTREAM_OOB, having a
39  * structure of name "stream-lock" with only one boolean field:
40  * "lock". If this field is set to TRUE, the request is for the
41  * acquisition of the lock, otherwise it is for release of the lock.
42  *
43  * For example, the following code in an upstream peer element
44  * requests the acquisition of the stream lock:
45  *
46  * <programlisting>
47  * GstEvent *event;
48  * GstStructure *structure;
49  * GstPad *srcpad;
50  *
51  * ... /\* srcpad initialization goes here \*\/
52  *
53  * structure = gst_structure_new ("stream-lock",
54  *                    "lock", G_TYPE_BOOLEAN, TRUE, NULL);
55  *
56  * event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
57  * gst_pad_push_event (dtmfsrc->srcpad, event);
58  * </programlisting>
59  *
60  */
61
62 #ifdef HAVE_CONFIG_H
63 #include "config.h"
64 #endif
65
66 #include <gst/gst.h>
67 #include <string.h>
68
69 #include "gstrtpdtmfmux.h"
70
71 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
72 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
73
74 enum
75 {
76   SIGNAL_LOCKING_STREAM,
77   SIGNAL_UNLOCKED_STREAM,
78   LAST_SIGNAL
79 };
80
81 static guint gst_rtpdtmfmux_signals[LAST_SIGNAL] = { 0 };
82
83 static void gst_rtp_dtmf_mux_dispose (GObject * object);
84
85 static void gst_rtp_dtmf_mux_release_pad (GstElement * element, GstPad * pad);
86
87 static gboolean gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event);
88 static GstFlowReturn gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer);
89
90 GST_BOILERPLATE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GstRTPMux, GST_TYPE_RTP_MUX);
91
92 static void
93 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * object, GstRTPDTMFMuxClass * g_class)
94 {
95 }
96
97 static void
98 gst_rtp_dtmf_mux_base_init (gpointer g_class)
99 {
100   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
101
102   gst_element_class_set_details_simple (element_class, "RTP muxer",
103       "Codec/Muxer",
104       "mixes RTP DTMF streams into other RTP streams",
105       "Zeeshan Ali <first.last@nokia.com>");
106 }
107
108 static void
109 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
110 {
111   GObjectClass *gobject_class;
112   GstElementClass *gstelement_class;
113   GstRTPMuxClass *gstrtpmux_class;
114
115   gobject_class = (GObjectClass *) klass;
116   gstelement_class = (GstElementClass *) klass;
117   gstrtpmux_class = (GstRTPMuxClass *) klass;
118
119   gst_rtpdtmfmux_signals[SIGNAL_LOCKING_STREAM] =
120       g_signal_new ("locking", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
121       G_STRUCT_OFFSET (GstRTPDTMFMuxClass, locking), NULL, NULL,
122       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
123
124   gst_rtpdtmfmux_signals[SIGNAL_UNLOCKED_STREAM] =
125       g_signal_new ("unlocked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
126       G_STRUCT_OFFSET (GstRTPDTMFMuxClass, unlocked), NULL, NULL,
127       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
128
129   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_dispose);
130   gstelement_class->release_pad =
131       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_release_pad);
132   gstrtpmux_class->chain_func = GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_chain);
133   gstrtpmux_class->sink_event_func =
134       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_sink_event);
135 }
136
137 static void
138 gst_rtp_dtmf_mux_dispose (GObject * object)
139 {
140   GstRTPDTMFMux *mux;
141
142   mux = GST_RTP_DTMF_MUX (object);
143
144   GST_OBJECT_LOCK (mux);
145   if (mux->special_pad != NULL) {
146     gst_object_unref (mux->special_pad);
147     mux->special_pad = NULL;
148   }
149   GST_OBJECT_UNLOCK (mux);
150
151   G_OBJECT_CLASS (parent_class)->dispose (object);
152 }
153
154 static GstFlowReturn
155 gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer)
156 {
157   GstRTPDTMFMux *mux;
158   GstFlowReturn ret;
159
160   mux = GST_RTP_DTMF_MUX (gst_pad_get_parent (pad));
161
162   GST_OBJECT_LOCK (mux);
163   if (mux->special_pad != NULL && mux->special_pad != pad) {
164     /* Drop the buffer */
165     gst_buffer_unref (buffer);
166     ret = GST_FLOW_OK;
167     GST_OBJECT_UNLOCK (mux);
168   }
169
170   else {
171     GST_OBJECT_UNLOCK (mux);
172     if (parent_class->chain_func)
173       ret = parent_class->chain_func (pad, buffer);
174     else {
175       gst_buffer_unref (buffer);
176       ret = GST_FLOW_ERROR;
177     }
178   }
179
180   gst_object_unref (mux);
181   return ret;
182 }
183
184 static void
185 gst_rtp_dtmf_mux_lock_stream (GstRTPDTMFMux * mux, GstPad * pad)
186 {
187   if (mux->special_pad != NULL)
188     GST_WARNING_OBJECT (mux,
189         "Stream lock already acquired by pad %s",
190         GST_ELEMENT_NAME (mux->special_pad));
191
192   else {
193     GST_DEBUG_OBJECT (mux,
194         "Stream lock acquired by pad %s", 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, "Stream lock not acquired, can't release it");
204
205   else if (pad != mux->special_pad)
206     GST_WARNING_OBJECT (mux,
207         "pad %s attempted to release Stream lock"
208         " which was acquired by pad %s", GST_ELEMENT_NAME (pad),
209         GST_ELEMENT_NAME (mux->special_pad));
210   else {
211     GST_DEBUG_OBJECT (mux,
212         "Stream lock released by pad %s", GST_ELEMENT_NAME (mux->special_pad));
213     gst_object_unref (mux->special_pad);
214     mux->special_pad = NULL;
215   }
216 }
217
218 static gboolean
219 gst_rtp_dtmf_mux_handle_stream_lock_event (GstRTPDTMFMux * mux, GstPad * pad,
220     const GstStructure * event_structure)
221 {
222   gboolean lock;
223
224   if (!gst_structure_get_boolean (event_structure, "lock", &lock))
225     return FALSE;
226
227   if (lock)
228     g_signal_emit (G_OBJECT (mux),
229         gst_rtpdtmfmux_signals[SIGNAL_LOCKING_STREAM], 0, pad);
230
231   GST_OBJECT_LOCK (mux);
232   if (lock)
233     gst_rtp_dtmf_mux_lock_stream (mux, pad);
234   else
235     gst_rtp_dtmf_mux_unlock_stream (mux, pad);
236   GST_OBJECT_UNLOCK (mux);
237
238   if (!lock)
239     g_signal_emit (G_OBJECT (mux),
240         gst_rtpdtmfmux_signals[SIGNAL_UNLOCKED_STREAM], 0, pad);
241
242   return TRUE;
243 }
244
245 static gboolean
246 gst_rtp_dtmf_mux_handle_downstream_event (GstRTPDTMFMux * mux,
247     GstPad * pad, GstEvent * event)
248 {
249   const GstStructure *structure;
250   gboolean ret = FALSE;
251
252   structure = gst_event_get_structure (event);
253   /* FIXME: is this event generic enough to be given a generic name? */
254   if (structure && gst_structure_has_name (structure, "stream-lock"))
255     ret = gst_rtp_dtmf_mux_handle_stream_lock_event (mux, pad, structure);
256
257   return ret;
258 }
259
260 static gboolean
261 gst_rtp_dtmf_mux_ignore_event (GstPad * pad, GstEvent * event)
262 {
263   gboolean ret;
264
265   if (parent_class->sink_event_func)
266     /* Give the parent a chance to handle the event first */
267     ret = parent_class->sink_event_func (pad, event);
268
269   else
270     ret = gst_pad_event_default (pad, event);
271
272   return ret;
273 }
274
275 static gboolean
276 gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event)
277 {
278   GstRTPDTMFMux *mux;
279   GstEventType type;
280   gboolean ret = FALSE;
281
282   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
283
284   mux = (GstRTPDTMFMux *) gst_pad_get_parent (pad);
285
286   switch (type) {
287     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
288       ret = gst_rtp_dtmf_mux_handle_downstream_event (mux, pad, event);
289       gst_event_unref (event);
290       break;
291     default:
292       ret = gst_rtp_dtmf_mux_ignore_event (pad, event);
293       break;
294   }
295
296   gst_object_unref (mux);
297   return ret;
298 }
299
300 static void
301 gst_rtp_dtmf_mux_release_pad (GstElement * element, GstPad * pad)
302 {
303   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
304
305   GST_OBJECT_LOCK (mux);
306   if (mux->special_pad == pad) {
307     gst_object_unref (mux->special_pad);
308     mux->special_pad = NULL;
309   }
310   GST_OBJECT_UNLOCK (mux);
311
312   GST_CALL_PARENT (GST_ELEMENT_CLASS, release_pad, (element, pad));
313 }
314
315 gboolean
316 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
317 {
318   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
319       "rtp dtmf muxer");
320
321   return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
322       GST_TYPE_RTP_DTMF_MUX);
323 }