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., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
29 * SECTION:element-rtpdtmfmux
30 * @see_also: rtpdtmfsrc, dtmfsrc, rtpmux
32 * The RTP "DTMF" Muxer muxes multiple RTP streams into a valid RTP
33 * stream. It does exactly what it's 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.
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.
49 #include "gstrtpdtmfmux.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
52 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
54 static GstStaticPadTemplate priority_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("priority_sink_%u",
58 GST_STATIC_CAPS ("application/x-rtp"));
60 static GstPad *gst_rtp_dtmf_mux_request_new_pad (GstElement * element,
61 GstPadTemplate * templ, const gchar * name);
62 static GstStateChangeReturn gst_rtp_dtmf_mux_change_state (GstElement * element,
63 GstStateChange transition);
65 static gboolean gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
66 GstRTPMuxPadPrivate * padpriv, GstBuffer * buffer);
67 static gboolean gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux,
70 GST_BOILERPLATE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GstRTPMux, GST_TYPE_RTP_MUX);
73 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * object, GstRTPDTMFMuxClass * g_class)
78 gst_rtp_dtmf_mux_base_init (gpointer g_class)
80 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
82 gst_element_class_add_pad_template (element_class,
83 gst_static_pad_template_get (&priority_sink_factory));
85 gst_element_class_set_details_simple (element_class, "RTP muxer",
87 "mixes RTP DTMF streams into other RTP streams",
88 "Zeeshan Ali <first.last@nokia.com>");
92 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
94 GstElementClass *gstelement_class;
95 GstRTPMuxClass *gstrtpmux_class;
97 gstelement_class = (GstElementClass *) klass;
98 gstrtpmux_class = (GstRTPMuxClass *) klass;
100 gstelement_class->request_new_pad =
101 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_request_new_pad);
102 gstelement_class->change_state =
103 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_mux_change_state);
104 gstrtpmux_class->accept_buffer_locked = gst_rtp_dtmf_mux_accept_buffer_locked;
105 gstrtpmux_class->src_event = gst_rtp_dtmf_mux_src_event;
109 gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
110 GstRTPMuxPadPrivate * padpriv, GstBuffer * buffer)
112 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (rtp_mux);
113 GstClockTime running_ts;
115 running_ts = GST_BUFFER_TIMESTAMP (buffer);
117 if (GST_CLOCK_TIME_IS_VALID (running_ts)) {
118 if (padpriv && padpriv->segment.format == GST_FORMAT_TIME)
119 running_ts = gst_segment_to_running_time (&padpriv->segment,
120 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buffer));
122 if (padpriv && padpriv->priority) {
123 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
124 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end))
125 mux->last_priority_end =
126 MAX (running_ts + GST_BUFFER_DURATION (buffer),
127 mux->last_priority_end);
129 mux->last_priority_end = running_ts + GST_BUFFER_DURATION (buffer);
130 GST_LOG_OBJECT (mux, "Got buffer %p on priority pad, "
131 " blocking regular pads until %" GST_TIME_FORMAT, buffer,
132 GST_TIME_ARGS (mux->last_priority_end));
134 GST_WARNING_OBJECT (mux, "Buffer %p has an invalid duration,"
135 " not blocking other pad", buffer);
138 if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end) &&
139 running_ts < mux->last_priority_end) {
140 GST_LOG_OBJECT (mux, "Dropping buffer %p because running time"
141 " %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, buffer,
142 GST_TIME_ARGS (running_ts), GST_TIME_ARGS (mux->last_priority_end));
147 GST_LOG_OBJECT (mux, "Buffer %p has an invalid timestamp,"
148 " letting through", buffer);
156 gst_rtp_dtmf_mux_request_new_pad (GstElement * element, GstPadTemplate * templ,
161 pad = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, request_new_pad,
162 (element, templ, name), NULL);
165 GstRTPMuxPadPrivate *padpriv;
167 GST_OBJECT_LOCK (element);
168 padpriv = gst_pad_get_element_private (pad);
170 if (gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
171 "priority_sink_%u") == gst_pad_get_pad_template (pad))
172 padpriv->priority = TRUE;
173 GST_OBJECT_UNLOCK (element);
180 gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux, GstEvent * event)
182 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
183 const GstStructure *s = gst_event_get_structure (event);
185 if (s && gst_structure_has_name (s, "dtmf-event")) {
186 GST_OBJECT_LOCK (rtp_mux);
187 if (GST_CLOCK_TIME_IS_VALID (rtp_mux->last_stop)) {
189 gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (event));
190 s = gst_event_get_structure (event);
191 gst_structure_set ((GstStructure *) s,
192 "last-stop", G_TYPE_UINT64, rtp_mux->last_stop, NULL);
194 GST_OBJECT_UNLOCK (rtp_mux);
198 return GST_RTP_MUX_CLASS (parent_class)->src_event (rtp_mux, event);
202 static GstStateChangeReturn
203 gst_rtp_dtmf_mux_change_state (GstElement * element, GstStateChange transition)
205 GstStateChangeReturn ret;
206 GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
208 switch (transition) {
209 case GST_STATE_CHANGE_READY_TO_PAUSED:
211 GST_OBJECT_LOCK (mux);
212 mux->last_priority_end = GST_CLOCK_TIME_NONE;
213 GST_OBJECT_UNLOCK (mux);
220 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
226 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
228 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
231 return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
232 GST_TYPE_RTP_DTMF_MUX);