1 /* RTP DTMF muxer element for GStreamer
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>
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.
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.
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.
27 * SECTION:element-rtpdtmfmux
28 * @see_also: rtpdtmfsrc, dtmfsrc
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.
43 * For example, the following code in an upstream peer element
44 * requests the acquisition of the stream lock:
48 * GstStructure *structure;
51 * ... /\* srcpad initialization goes here \*\/
53 * structure = gst_structure_new ("stream-lock",
54 * "lock", G_TYPE_BOOLEAN, TRUE, NULL);
56 * event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
57 * gst_pad_push_event (dtmfsrc->srcpad, event);
69 #include "gstrtpdtmfmux.h"
71 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
72 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
74 /* elementfactory information */
75 static const GstElementDetails gst_rtp_dtmf_mux_details =
76 GST_ELEMENT_DETAILS ("RTP muxer",
78 "mixes RTP DTMF streams into other RTP streams",
79 "Zeeshan Ali <first.last@nokia.com>");
83 SIGNAL_LOCKING_STREAM,
84 SIGNAL_UNLOCKED_STREAM,
88 static guint gst_rtpdtmfmux_signals[LAST_SIGNAL] = { 0 };
90 static void gst_rtp_dtmf_mux_dispose (GObject * object);
92 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
94 static gboolean gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event);
95 static GstFlowReturn gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer);
97 GST_BOILERPLATE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GstRTPMux, GST_TYPE_RTP_MUX);
100 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * object, GstRTPDTMFMuxClass * g_class)
105 gst_rtp_dtmf_mux_base_init (gpointer g_class)
107 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
109 gst_element_class_set_details (element_class, &gst_rtp_dtmf_mux_details);
113 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
117 GstRTPMuxClass *gstrtpmux_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
121 gstrtpmux_class = (GstRTPMuxClass *) klass;
123 gst_rtpdtmfmux_signals[SIGNAL_LOCKING_STREAM] =
124 g_signal_new ("locking", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
125 G_STRUCT_OFFSET (GstRTPDTMFMuxClass, locking), NULL, NULL,
126 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
128 gst_rtpdtmfmux_signals[SIGNAL_UNLOCKED_STREAM] =
129 g_signal_new ("unlocked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
130 G_STRUCT_OFFSET (GstRTPDTMFMuxClass, unlocked), NULL, NULL,
131 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
133 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_dispose);
134 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
135 gstrtpmux_class->chain_func = GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_chain);
136 gstrtpmux_class->sink_event_func =
137 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_sink_event);
141 gst_rtp_dtmf_mux_dispose (GObject * object)
145 mux = GST_RTP_DTMF_MUX (object);
147 GST_OBJECT_LOCK (mux);
148 if (mux->special_pad != NULL) {
149 gst_object_unref (mux->special_pad);
150 mux->special_pad = NULL;
152 GST_OBJECT_UNLOCK (mux);
154 G_OBJECT_CLASS (parent_class)->dispose (object);
158 gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer)
163 mux = GST_RTP_DTMF_MUX (gst_pad_get_parent (pad));
165 GST_OBJECT_LOCK (mux);
166 if (mux->special_pad != NULL && mux->special_pad != pad) {
167 /* Drop the buffer */
168 gst_buffer_unref (buffer);
170 GST_OBJECT_UNLOCK (mux);
174 GST_OBJECT_UNLOCK (mux);
175 if (parent_class->chain_func)
176 ret = parent_class->chain_func (pad, buffer);
178 gst_buffer_unref (buffer);
179 ret = GST_FLOW_ERROR;
183 gst_object_unref (mux);
188 gst_rtp_dtmf_mux_lock_stream (GstRTPDTMFMux * mux, GstPad * pad)
190 if (mux->special_pad != NULL)
191 GST_WARNING_OBJECT (mux,
192 "Stream lock already acquired by pad %s",
193 GST_ELEMENT_NAME (mux->special_pad));
196 GST_DEBUG_OBJECT (mux,
197 "Stream lock acquired by pad %s", GST_ELEMENT_NAME (pad));
198 mux->special_pad = gst_object_ref (pad);
203 gst_rtp_dtmf_mux_unlock_stream (GstRTPDTMFMux * mux, GstPad * pad)
205 if (mux->special_pad == NULL)
206 GST_WARNING_OBJECT (mux, "Stream lock not acquired, can't release it");
208 else if (pad != mux->special_pad)
209 GST_WARNING_OBJECT (mux,
210 "pad %s attempted to release Stream lock"
211 " which was acquired by pad %s", GST_ELEMENT_NAME (pad),
212 GST_ELEMENT_NAME (mux->special_pad));
214 GST_DEBUG_OBJECT (mux,
215 "Stream lock released by pad %s", GST_ELEMENT_NAME (mux->special_pad));
216 gst_object_unref (mux->special_pad);
217 mux->special_pad = NULL;
222 gst_rtp_dtmf_mux_handle_stream_lock_event (GstRTPDTMFMux * mux, GstPad * pad,
223 const GstStructure * event_structure)
227 if (!gst_structure_get_boolean (event_structure, "lock", &lock))
231 g_signal_emit (G_OBJECT (mux),
232 gst_rtpdtmfmux_signals[SIGNAL_LOCKING_STREAM], 0, pad);
234 GST_OBJECT_LOCK (mux);
236 gst_rtp_dtmf_mux_lock_stream (mux, pad);
238 gst_rtp_dtmf_mux_unlock_stream (mux, pad);
239 GST_OBJECT_UNLOCK (mux);
242 g_signal_emit (G_OBJECT (mux),
243 gst_rtpdtmfmux_signals[SIGNAL_UNLOCKED_STREAM], 0, pad);
249 gst_rtp_dtmf_mux_handle_downstream_event (GstRTPDTMFMux * mux,
250 GstPad * pad, GstEvent * event)
252 const GstStructure *structure;
253 gboolean ret = FALSE;
255 structure = gst_event_get_structure (event);
256 /* FIXME: is this event generic enough to be given a generic name? */
257 if (structure && gst_structure_has_name (structure, "stream-lock"))
258 ret = gst_rtp_dtmf_mux_handle_stream_lock_event (mux, pad, structure);
264 gst_rtp_dtmf_mux_ignore_event (GstPad * pad, GstEvent * event)
268 if (parent_class->sink_event_func)
269 /* Give the parent a chance to handle the event first */
270 ret = parent_class->sink_event_func (pad, event);
273 ret = gst_pad_event_default (pad, event);
279 gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event)
283 gboolean ret = FALSE;
285 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
287 mux = (GstRTPDTMFMux *) gst_pad_get_parent (pad);
290 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
291 ret = gst_rtp_dtmf_mux_handle_downstream_event (mux, pad, event);
292 gst_event_unref (event);
295 ret = gst_rtp_dtmf_mux_ignore_event (pad, event);
299 gst_object_unref (mux);
304 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
306 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
308 GST_OBJECT_LOCK (mux);
309 if (mux->special_pad == pad) {
310 gst_object_unref (mux->special_pad);
311 mux->special_pad = NULL;
313 GST_OBJECT_UNLOCK (mux);
315 GST_CALL_PARENT (GST_ELEMENT_CLASS, release_pad, (element, pad));
319 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
321 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
324 return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
325 GST_TYPE_RTP_DTMF_MUX);