rtpmux: Use GST_BOILERPLATE
[platform/upstream/gst-plugins-good.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 /* elementfactory information */
75 static const GstElementDetails gst_rtp_dtmf_mux_details =
76 GST_ELEMENT_DETAILS ("RTP muxer",
77     "Codec/Muxer",
78     "mixes RTP DTMF streams into other RTP streams",
79     "Zeeshan Ali <first.last@nokia.com>");
80
81 enum
82 {
83   SIGNAL_LOCKING_STREAM,
84   SIGNAL_UNLOCKED_STREAM,
85   LAST_SIGNAL
86 };
87
88 static guint gst_rtpdtmfmux_signals[LAST_SIGNAL] = { 0 };
89
90 static void gst_rtp_dtmf_mux_dispose (GObject * object);
91
92 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
93
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);
96
97 GST_BOILERPLATE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GstRTPMux, GST_TYPE_RTP_MUX);
98
99 static void
100 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * object, GstRTPDTMFMuxClass * g_class)
101 {
102 }
103
104 static void
105 gst_rtp_dtmf_mux_base_init (gpointer g_class)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108
109   gst_element_class_set_details (element_class, &gst_rtp_dtmf_mux_details);
110 }
111
112 static void
113 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
114 {
115   GObjectClass *gobject_class;
116   GstElementClass *gstelement_class;
117   GstRTPMuxClass *gstrtpmux_class;
118
119   gobject_class = (GObjectClass *) klass;
120   gstelement_class = (GstElementClass *) klass;
121   gstrtpmux_class = (GstRTPMuxClass *) klass;
122
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);
127
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);
132
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);
138 }
139
140 static void
141 gst_rtp_dtmf_mux_dispose (GObject * object)
142 {
143   GstRTPDTMFMux *mux;
144
145   mux = GST_RTP_DTMF_MUX (object);
146
147   GST_OBJECT_LOCK (mux);
148   if (mux->special_pad != NULL) {
149     gst_object_unref (mux->special_pad);
150     mux->special_pad = NULL;
151   }
152   GST_OBJECT_UNLOCK (mux);
153
154   G_OBJECT_CLASS (parent_class)->dispose (object);
155 }
156
157 static GstFlowReturn
158 gst_rtp_dtmf_mux_chain (GstPad * pad, GstBuffer * buffer)
159 {
160   GstRTPDTMFMux *mux;
161   GstFlowReturn ret;
162
163   mux = GST_RTP_DTMF_MUX (gst_pad_get_parent (pad));
164
165   GST_OBJECT_LOCK (mux);
166   if (mux->special_pad != NULL && mux->special_pad != pad) {
167     /* Drop the buffer */
168     gst_buffer_unref (buffer);
169     ret = GST_FLOW_OK;
170     GST_OBJECT_UNLOCK (mux);
171   }
172
173   else {
174     GST_OBJECT_UNLOCK (mux);
175     if (parent_class->chain_func)
176       ret = parent_class->chain_func (pad, buffer);
177     else {
178       gst_buffer_unref (buffer);
179       ret = GST_FLOW_ERROR;
180     }
181   }
182
183   gst_object_unref (mux);
184   return ret;
185 }
186
187 static void
188 gst_rtp_dtmf_mux_lock_stream (GstRTPDTMFMux * mux, GstPad * pad)
189 {
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));
194
195   else {
196     GST_DEBUG_OBJECT (mux,
197         "Stream lock acquired by pad %s", GST_ELEMENT_NAME (pad));
198     mux->special_pad = gst_object_ref (pad);
199   }
200 }
201
202 static void
203 gst_rtp_dtmf_mux_unlock_stream (GstRTPDTMFMux * mux, GstPad * pad)
204 {
205   if (mux->special_pad == NULL)
206     GST_WARNING_OBJECT (mux, "Stream lock not acquired, can't release it");
207
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));
213   else {
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;
218   }
219 }
220
221 static gboolean
222 gst_rtp_dtmf_mux_handle_stream_lock_event (GstRTPDTMFMux * mux, GstPad * pad,
223     const GstStructure * event_structure)
224 {
225   gboolean lock;
226
227   if (!gst_structure_get_boolean (event_structure, "lock", &lock))
228     return FALSE;
229
230   if (lock)
231     g_signal_emit (G_OBJECT (mux),
232         gst_rtpdtmfmux_signals[SIGNAL_LOCKING_STREAM], 0, pad);
233
234   GST_OBJECT_LOCK (mux);
235   if (lock)
236     gst_rtp_dtmf_mux_lock_stream (mux, pad);
237   else
238     gst_rtp_dtmf_mux_unlock_stream (mux, pad);
239   GST_OBJECT_UNLOCK (mux);
240
241   if (!lock)
242     g_signal_emit (G_OBJECT (mux),
243         gst_rtpdtmfmux_signals[SIGNAL_UNLOCKED_STREAM], 0, pad);
244
245   return TRUE;
246 }
247
248 static gboolean
249 gst_rtp_dtmf_mux_handle_downstream_event (GstRTPDTMFMux * mux,
250     GstPad * pad, GstEvent * event)
251 {
252   const GstStructure *structure;
253   gboolean ret = FALSE;
254
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);
259
260   return ret;
261 }
262
263 static gboolean
264 gst_rtp_dtmf_mux_ignore_event (GstPad * pad, GstEvent * event)
265 {
266   gboolean ret;
267
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);
271
272   else
273     ret = gst_pad_event_default (pad, event);
274
275   return ret;
276 }
277
278 static gboolean
279 gst_rtp_dtmf_mux_sink_event (GstPad * pad, GstEvent * event)
280 {
281   GstRTPDTMFMux *mux;
282   GstEventType type;
283   gboolean ret = FALSE;
284
285   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
286
287   mux = (GstRTPDTMFMux *) gst_pad_get_parent (pad);
288
289   switch (type) {
290     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
291       ret = gst_rtp_dtmf_mux_handle_downstream_event (mux, pad, event);
292       gst_event_unref (event);
293       break;
294     default:
295       ret = gst_rtp_dtmf_mux_ignore_event (pad, event);
296       break;
297   }
298
299   gst_object_unref (mux);
300   return ret;
301 }
302
303 static void
304 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
305 {
306   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
307
308   GST_OBJECT_LOCK (mux);
309   if (mux->special_pad == pad) {
310     gst_object_unref (mux->special_pad);
311     mux->special_pad = NULL;
312   }
313   GST_OBJECT_UNLOCK (mux);
314
315   GST_CALL_PARENT (GST_ELEMENT_CLASS, release_pad, (element, pad));
316 }
317
318 gboolean
319 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
320 {
321   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
322       "rtp dtmf muxer");
323
324   return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
325       GST_TYPE_RTP_DTMF_MUX);
326 }