1 /* RTP DTMF muxer element for GStreamer
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>
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.
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.
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.
29 * SECTION:element-rtpdtmfmux
31 * @see_also: rtpdtmfsrc, dtmfsrc, rtpmux
33 * The RTP "DTMF" Muxer muxes multiple RTP streams into a valid RTP
34 * stream. It does exactly what its parent (#rtpmux) does, except
35 * that it prevent buffers coming over a regular sink_\%u pad from going through
36 * for the duration of buffers that came in a priority_sink_\%u pad.
38 * This is especially useful if a discontinuous source like dtmfsrc or
39 * rtpdtmfsrc are connected to the priority sink pads. This way, the generated
40 * DTMF signal can replace the recorded audio while the tone is being sent.
50 #include "gstrtpdtmfmux.h"
52 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
53 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
55 static GstStaticPadTemplate priority_sink_factory =
56 GST_STATIC_PAD_TEMPLATE ("priority_sink_%u",
59 GST_STATIC_CAPS ("application/x-rtp"));
61 static GstPad *gst_rtp_dtmf_mux_request_new_pad (GstElement * element,
62 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
63 static GstStateChangeReturn gst_rtp_dtmf_mux_change_state (GstElement * element,
64 GstStateChange transition);
66 static gboolean gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
67 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer);
68 static gboolean gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux,
71 G_DEFINE_TYPE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GST_TYPE_RTP_MUX);
74 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * mux)
80 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
82 GstElementClass *gstelement_class;
83 GstRTPMuxClass *gstrtpmux_class;
85 gstelement_class = (GstElementClass *) klass;
86 gstrtpmux_class = (GstRTPMuxClass *) klass;
88 gst_element_class_add_static_pad_template (gstelement_class,
89 &priority_sink_factory);
91 gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
93 "mixes RTP DTMF streams into other RTP streams",
94 "Zeeshan Ali <first.last@nokia.com>");
96 gstelement_class->request_new_pad =
97 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_request_new_pad);
98 gstelement_class->change_state =
99 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_change_state);
100 gstrtpmux_class->accept_buffer_locked = gst_rtp_dtmf_mux_accept_buffer_locked;
101 gstrtpmux_class->src_event = gst_rtp_dtmf_mux_src_event;
105 gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
106 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
108 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (rtp_mux);
109 GstClockTime running_ts;
111 running_ts = GST_BUFFER_PTS (rtpbuffer->buffer);
113 if (GST_CLOCK_TIME_IS_VALID (running_ts)) {
114 if (padpriv && padpriv->segment.format == GST_FORMAT_TIME)
115 running_ts = gst_segment_to_running_time (&padpriv->segment,
116 GST_FORMAT_TIME, GST_BUFFER_PTS (rtpbuffer->buffer));
118 if (padpriv && padpriv->priority) {
119 if (GST_BUFFER_PTS_IS_VALID (rtpbuffer->buffer)) {
120 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end))
121 mux->last_priority_end =
122 MAX (running_ts + GST_BUFFER_DURATION (rtpbuffer->buffer),
123 mux->last_priority_end);
125 mux->last_priority_end = running_ts +
126 GST_BUFFER_DURATION (rtpbuffer->buffer);
127 GST_LOG_OBJECT (mux, "Got buffer %p on priority pad, "
128 " blocking regular pads until %" GST_TIME_FORMAT, rtpbuffer->buffer,
129 GST_TIME_ARGS (mux->last_priority_end));
131 GST_WARNING_OBJECT (mux, "Buffer %p has an invalid duration,"
132 " not blocking other pad", rtpbuffer->buffer);
135 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end) &&
136 running_ts < mux->last_priority_end) {
137 GST_LOG_OBJECT (mux, "Dropping buffer %p because running time"
138 " %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, rtpbuffer->buffer,
139 GST_TIME_ARGS (running_ts), GST_TIME_ARGS (mux->last_priority_end));
144 GST_LOG_OBJECT (mux, "Buffer %p has an invalid timestamp,"
145 " letting through", rtpbuffer->buffer);
153 gst_rtp_dtmf_mux_request_new_pad (GstElement * element, GstPadTemplate * templ,
154 const gchar * name, const GstCaps * caps)
159 GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->request_new_pad
160 (element, templ, name, caps);
163 GstRTPMuxPadPrivate *padpriv;
165 GST_OBJECT_LOCK (element);
166 padpriv = gst_pad_get_element_private (pad);
168 if (gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
169 "priority_sink_%u") == GST_PAD_PAD_TEMPLATE (pad))
170 padpriv->priority = TRUE;
171 GST_OBJECT_UNLOCK (element);
178 gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux, GstEvent * event)
180 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
181 const GstStructure *s = gst_event_get_structure (event);
183 if (s && gst_structure_has_name (s, "dtmf-event")) {
184 GST_OBJECT_LOCK (rtp_mux);
185 if (GST_CLOCK_TIME_IS_VALID (rtp_mux->last_stop)) {
187 gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (event));
188 s = gst_event_get_structure (event);
189 gst_structure_set ((GstStructure *) s,
190 "last-stop", G_TYPE_UINT64, rtp_mux->last_stop, NULL);
192 GST_OBJECT_UNLOCK (rtp_mux);
196 return GST_RTP_MUX_CLASS (gst_rtp_dtmf_mux_parent_class)->src_event (rtp_mux,
201 static GstStateChangeReturn
202 gst_rtp_dtmf_mux_change_state (GstElement * element, GstStateChange transition)
204 GstStateChangeReturn ret;
205 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
207 switch (transition) {
208 case GST_STATE_CHANGE_READY_TO_PAUSED:
210 GST_OBJECT_LOCK (mux);
211 mux->last_priority_end = GST_CLOCK_TIME_NONE;
212 GST_OBJECT_UNLOCK (mux);
220 GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->change_state (element,
227 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
229 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
232 return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
233 GST_TYPE_RTP_DTMF_MUX);