• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: rtpdtmfmux
31  * @see_also: rtpdtmfsrc, dtmfsrc, rtpmux
32  *
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.
37  *
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.
41  */
42 
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 
47 #include <gst/gst.h>
48 #include <string.h>
49 
50 #include "gstrtpdtmfmux.h"
51 
52 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_mux_debug);
53 #define GST_CAT_DEFAULT gst_rtp_dtmf_mux_debug
54 
55 static GstStaticPadTemplate priority_sink_factory =
56 GST_STATIC_PAD_TEMPLATE ("priority_sink_%u",
57     GST_PAD_SINK,
58     GST_PAD_REQUEST,
59     GST_STATIC_CAPS ("application/x-rtp"));
60 
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);
65 
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,
69     GstEvent * event);
70 
71 G_DEFINE_TYPE_WITH_CODE (GstRTPDTMFMux, gst_rtp_dtmf_mux, GST_TYPE_RTP_MUX,
72     GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
73         "rtp dtmf muxer"););
74 GST_ELEMENT_REGISTER_DEFINE (rtpdtmfmux, "rtpdtmfmux", GST_RANK_NONE,
75     GST_TYPE_RTP_DTMF_MUX);
76 
77 static void
gst_rtp_dtmf_mux_init(GstRTPDTMFMux * mux)78 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * mux)
79 {
80 }
81 
82 
83 static void
gst_rtp_dtmf_mux_class_init(GstRTPDTMFMuxClass * klass)84 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
85 {
86   GstElementClass *gstelement_class;
87   GstRTPMuxClass *gstrtpmux_class;
88 
89   gstelement_class = (GstElementClass *) klass;
90   gstrtpmux_class = (GstRTPMuxClass *) klass;
91 
92   gst_element_class_add_static_pad_template (gstelement_class,
93       &priority_sink_factory);
94 
95   gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
96       "Codec/Muxer",
97       "mixes RTP DTMF streams into other RTP streams",
98       "Zeeshan Ali <first.last@nokia.com>");
99 
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;
106 }
107 
108 static gboolean
gst_rtp_dtmf_mux_accept_buffer_locked(GstRTPMux * rtp_mux,GstRTPMuxPadPrivate * padpriv,GstRTPBuffer * rtpbuffer)109 gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
110     GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
111 {
112   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (rtp_mux);
113   GstClockTime running_ts;
114 
115   running_ts = GST_BUFFER_PTS (rtpbuffer->buffer);
116 
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_PTS (rtpbuffer->buffer));
121 
122     if (padpriv && padpriv->priority) {
123       if (GST_BUFFER_PTS_IS_VALID (rtpbuffer->buffer)) {
124         if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end))
125           mux->last_priority_end =
126               MAX (running_ts + GST_BUFFER_DURATION (rtpbuffer->buffer),
127               mux->last_priority_end);
128         else
129           mux->last_priority_end = running_ts +
130               GST_BUFFER_DURATION (rtpbuffer->buffer);
131         GST_LOG_OBJECT (mux, "Got buffer %p on priority pad, "
132             " blocking regular pads until %" GST_TIME_FORMAT, rtpbuffer->buffer,
133             GST_TIME_ARGS (mux->last_priority_end));
134       } else {
135         GST_WARNING_OBJECT (mux, "Buffer %p has an invalid duration,"
136             " not blocking other pad", rtpbuffer->buffer);
137       }
138     } else {
139       if (GST_CLOCK_TIME_IS_VALID (mux->last_priority_end) &&
140           running_ts < mux->last_priority_end) {
141         GST_LOG_OBJECT (mux, "Dropping buffer %p because running time"
142             " %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, rtpbuffer->buffer,
143             GST_TIME_ARGS (running_ts), GST_TIME_ARGS (mux->last_priority_end));
144         return FALSE;
145       }
146     }
147   } else {
148     GST_LOG_OBJECT (mux, "Buffer %p has an invalid timestamp,"
149         " letting through", rtpbuffer->buffer);
150   }
151 
152   return TRUE;
153 }
154 
155 
156 static GstPad *
gst_rtp_dtmf_mux_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)157 gst_rtp_dtmf_mux_request_new_pad (GstElement * element, GstPadTemplate * templ,
158     const gchar * name, const GstCaps * caps)
159 {
160   GstPad *pad;
161 
162   pad =
163       GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->request_new_pad
164       (element, templ, name, caps);
165 
166   if (pad) {
167     GstRTPMuxPadPrivate *padpriv;
168 
169     GST_OBJECT_LOCK (element);
170     padpriv = gst_pad_get_element_private (pad);
171 
172     if (gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
173             "priority_sink_%u") == GST_PAD_PAD_TEMPLATE (pad))
174       padpriv->priority = TRUE;
175     GST_OBJECT_UNLOCK (element);
176   }
177 
178   return pad;
179 }
180 
181 static gboolean
gst_rtp_dtmf_mux_src_event(GstRTPMux * rtp_mux,GstEvent * event)182 gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux, GstEvent * event)
183 {
184   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
185     const GstStructure *s = gst_event_get_structure (event);
186 
187     if (s && gst_structure_has_name (s, "dtmf-event")) {
188       GST_OBJECT_LOCK (rtp_mux);
189       if (GST_CLOCK_TIME_IS_VALID (rtp_mux->last_stop)) {
190         event = (GstEvent *)
191             gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (event));
192         s = gst_event_get_structure (event);
193         gst_structure_set ((GstStructure *) s,
194             "last-stop", G_TYPE_UINT64, rtp_mux->last_stop, NULL);
195       }
196       GST_OBJECT_UNLOCK (rtp_mux);
197     }
198   }
199 
200   return GST_RTP_MUX_CLASS (gst_rtp_dtmf_mux_parent_class)->src_event (rtp_mux,
201       event);
202 }
203 
204 
205 static GstStateChangeReturn
gst_rtp_dtmf_mux_change_state(GstElement * element,GstStateChange transition)206 gst_rtp_dtmf_mux_change_state (GstElement * element, GstStateChange transition)
207 {
208   GstStateChangeReturn ret;
209   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
210 
211   switch (transition) {
212     case GST_STATE_CHANGE_READY_TO_PAUSED:
213     {
214       GST_OBJECT_LOCK (mux);
215       mux->last_priority_end = GST_CLOCK_TIME_NONE;
216       GST_OBJECT_UNLOCK (mux);
217       break;
218     }
219     default:
220       break;
221   }
222 
223   ret =
224       GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->change_state (element,
225       transition);
226 
227   return ret;
228 }
229