1 /* RTP DTMF muxer element for GStreamer
2 *
3 * gstrtpdtmfmux.c:
4 *
5 * Copyright (C) <2007-2010> Nokia Corporation.
6 * Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
7 * Copyright (C) <2007-2010> Collabora Ltd
8 * Contact: Olivier Crete <olivier.crete@collabora.co.uk>
9 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
10 * 2000,2005 Wim Taymans <wim@fluendo.com>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
26 */
27
28 /**
29 * SECTION:element-rtpdtmfmux
30 * @see_also: rtpdtmfsrc, dtmfsrc, rtpmux
31 *
32 * The RTP "DTMF" Muxer muxes multiple RTP streams into a valid RTP
33 * stream. It does exactly what its parent (#rtpmux) does, except
34 * that it prevent buffers coming over a regular sink_\%u pad from going through
35 * for the duration of buffers that came in a priority_sink_\%u pad.
36 *
37 * This is especially useful if a discontinuous source like dtmfsrc or
38 * rtpdtmfsrc are connected to the priority sink pads. This way, the generated
39 * DTMF signal can replace the recorded audio while the tone is being sent.
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <gst/gst.h>
47 #include <string.h>
48
49 #include "gstrtpdtmfmux.h"
50
51 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
52 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
53
54 static GstStaticPadTemplate priority_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("priority_sink_%u",
56 GST_PAD_SINK,
57 GST_PAD_REQUEST,
58 GST_STATIC_CAPS ("application/x-rtp"));
59
60 static GstPad *gst_rtp_dtmf_mux_request_new_pad (GstElement * element,
61 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
62 static GstStateChangeReturn gst_rtp_dtmf_mux_change_state (GstElement * element,
63 GstStateChange transition);
64
65 static gboolean gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
66 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer);
67 static gboolean gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux,
68 GstEvent * event);
69
70 G_DEFINE_TYPE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GST_TYPE_RTP_MUX);
71
72 static void
gst_rtp_dtmf_mux_init(GstRTPDTMFMux * mux)73 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * mux)
74 {
75 }
76
77
78 static void
gst_rtp_dtmf_mux_class_init(GstRTPDTMFMuxClass * klass)79 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
80 {
81 GstElementClass *gstelement_class;
82 GstRTPMuxClass *gstrtpmux_class;
83
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpmux_class = (GstRTPMuxClass *) klass;
86
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &priority_sink_factory);
89
90 gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
91 "Codec/Muxer",
92 "mixes RTP DTMF streams into other RTP streams",
93 "Zeeshan Ali <first.last@nokia.com>");
94
95 gstelement_class->request_new_pad =
96 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_request_new_pad);
97 gstelement_class->change_state =
98 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_change_state);
99 gstrtpmux_class->accept_buffer_locked = gst_rtp_dtmf_mux_accept_buffer_locked;
100 gstrtpmux_class->src_event = gst_rtp_dtmf_mux_src_event;
101 }
102
103 static gboolean
gst_rtp_dtmf_mux_accept_buffer_locked(GstRTPMux * rtp_mux,GstRTPMuxPadPrivate * padpriv,GstRTPBuffer * rtpbuffer)104 gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
105 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
106 {
107 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (rtp_mux);
108 GstClockTime running_ts;
109
110 running_ts = GST_BUFFER_PTS (rtpbuffer->buffer);
111
112 if (GST_CLOCK_TIME_IS_VALID (running_ts)) {
113 if (padpriv && padpriv->segment.format == GST_FORMAT_TIME)
114 running_ts = gst_segment_to_running_time (&padpriv->segment,
115 GST_FORMAT_TIME, GST_BUFFER_PTS (rtpbuffer->buffer));
116
117 if (padpriv && padpriv->priority) {
118 if (GST_BUFFER_PTS_IS_VALID (rtpbuffer->buffer)) {
119 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end))
120 mux->last_priority_end =
121 MAX (running_ts + GST_BUFFER_DURATION (rtpbuffer->buffer),
122 mux->last_priority_end);
123 else
124 mux->last_priority_end = running_ts +
125 GST_BUFFER_DURATION (rtpbuffer->buffer);
126 GST_LOG_OBJECT (mux, "Got buffer %p on priority pad, "
127 " blocking regular pads until %" GST_TIME_FORMAT, rtpbuffer->buffer,
128 GST_TIME_ARGS (mux->last_priority_end));
129 } else {
130 GST_WARNING_OBJECT (mux, "Buffer %p has an invalid duration,"
131 " not blocking other pad", rtpbuffer->buffer);
132 }
133 } else {
134 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end) &&
135 running_ts < mux->last_priority_end) {
136 GST_LOG_OBJECT (mux, "Dropping buffer %p because running time"
137 " %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, rtpbuffer->buffer,
138 GST_TIME_ARGS (running_ts), GST_TIME_ARGS (mux->last_priority_end));
139 return FALSE;
140 }
141 }
142 } else {
143 GST_LOG_OBJECT (mux, "Buffer %p has an invalid timestamp,"
144 " letting through", rtpbuffer->buffer);
145 }
146
147 return TRUE;
148 }
149
150
151 static GstPad *
gst_rtp_dtmf_mux_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)152 gst_rtp_dtmf_mux_request_new_pad (GstElement * element, GstPadTemplate * templ,
153 const gchar * name, const GstCaps * caps)
154 {
155 GstPad *pad;
156
157 pad =
158 GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->request_new_pad
159 (element, templ, name, caps);
160
161 if (pad) {
162 GstRTPMuxPadPrivate *padpriv;
163
164 GST_OBJECT_LOCK (element);
165 padpriv = gst_pad_get_element_private (pad);
166
167 if (gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
168 "priority_sink_%u") == GST_PAD_PAD_TEMPLATE (pad))
169 padpriv->priority = TRUE;
170 GST_OBJECT_UNLOCK (element);
171 }
172
173 return pad;
174 }
175
176 static gboolean
gst_rtp_dtmf_mux_src_event(GstRTPMux * rtp_mux,GstEvent * event)177 gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux, GstEvent * event)
178 {
179 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
180 const GstStructure *s = gst_event_get_structure (event);
181
182 if (s && gst_structure_has_name (s, "dtmf-event")) {
183 GST_OBJECT_LOCK (rtp_mux);
184 if (GST_CLOCK_TIME_IS_VALID (rtp_mux->last_stop)) {
185 event = (GstEvent *)
186 gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (event));
187 s = gst_event_get_structure (event);
188 gst_structure_set ((GstStructure *) s,
189 "last-stop", G_TYPE_UINT64, rtp_mux->last_stop, NULL);
190 }
191 GST_OBJECT_UNLOCK (rtp_mux);
192 }
193 }
194
195 return GST_RTP_MUX_CLASS (gst_rtp_dtmf_mux_parent_class)->src_event (rtp_mux,
196 event);
197 }
198
199
200 static GstStateChangeReturn
gst_rtp_dtmf_mux_change_state(GstElement * element,GstStateChange transition)201 gst_rtp_dtmf_mux_change_state (GstElement * element, GstStateChange transition)
202 {
203 GstStateChangeReturn ret;
204 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
205
206 switch (transition) {
207 case GST_STATE_CHANGE_READY_TO_PAUSED:
208 {
209 GST_OBJECT_LOCK (mux);
210 mux->last_priority_end = GST_CLOCK_TIME_NONE;
211 GST_OBJECT_UNLOCK (mux);
212 break;
213 }
214 default:
215 break;
216 }
217
218 ret =
219 GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->change_state (element,
220 transition);
221
222 return ret;
223 }
224
225 gboolean
gst_rtp_dtmf_mux_plugin_init(GstPlugin * plugin)226 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
227 {
228 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
229 "rtp dtmf muxer");
230
231 return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
232 GST_TYPE_RTP_DTMF_MUX);
233 }
234