rtpmanager: Update codes based on 1.18.4
[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-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 (GstRTPDTMFMux, gst_rtp_dtmf_mux, GST_TYPE_RTP_MUX);
72
73 static void
74 gst_rtp_dtmf_mux_init (GstRTPDTMFMux * mux)
75 {
76 }
77
78
79 static void
80 gst_rtp_dtmf_mux_class_init (GstRTPDTMFMuxClass * klass)
81 {
82   GstElementClass *gstelement_class;
83   GstRTPMuxClass *gstrtpmux_class;
84
85   gstelement_class = (GstElementClass *) klass;
86   gstrtpmux_class = (GstRTPMuxClass *) klass;
87
88   gst_element_class_add_static_pad_template (gstelement_class,
89       &priority_sink_factory);
90
91   gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
92       "Codec/Muxer",
93       "mixes RTP DTMF streams into other RTP streams",
94       "Zeeshan Ali <first.last@nokia.com>");
95
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;
102 }
103
104 static gboolean
105 gst_rtp_dtmf_mux_accept_buffer_locked (GstRTPMux * rtp_mux,
106     GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
107 {
108   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (rtp_mux);
109   GstClockTime running_ts;
110
111   running_ts = GST_BUFFER_PTS (rtpbuffer->buffer);
112
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));
117
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);
124         else
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));
130       } else {
131         GST_WARNING_OBJECT (mux, "Buffer %p has an invalid duration,"
132             " not blocking other pad", rtpbuffer->buffer);
133       }
134     } else {
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));
140         return FALSE;
141       }
142     }
143   } else {
144     GST_LOG_OBJECT (mux, "Buffer %p has an invalid timestamp,"
145         " letting through", rtpbuffer->buffer);
146   }
147
148   return TRUE;
149 }
150
151
152 static GstPad *
153 gst_rtp_dtmf_mux_request_new_pad (GstElement * element, GstPadTemplate * templ,
154     const gchar * name, const GstCaps * caps)
155 {
156   GstPad *pad;
157
158   pad =
159       GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->request_new_pad
160       (element, templ, name, caps);
161
162   if (pad) {
163     GstRTPMuxPadPrivate *padpriv;
164
165     GST_OBJECT_LOCK (element);
166     padpriv = gst_pad_get_element_private (pad);
167
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);
172   }
173
174   return pad;
175 }
176
177 static gboolean
178 gst_rtp_dtmf_mux_src_event (GstRTPMux * rtp_mux, GstEvent * event)
179 {
180   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
181     const GstStructure *s = gst_event_get_structure (event);
182
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)) {
186         event = (GstEvent *)
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);
191       }
192       GST_OBJECT_UNLOCK (rtp_mux);
193     }
194   }
195
196   return GST_RTP_MUX_CLASS (gst_rtp_dtmf_mux_parent_class)->src_event (rtp_mux,
197       event);
198 }
199
200
201 static GstStateChangeReturn
202 gst_rtp_dtmf_mux_change_state (GstElement * element, GstStateChange transition)
203 {
204   GstStateChangeReturn ret;
205   GstRTPDTMFMux *mux = GST_RTP_DTMF_MUX (element);
206
207   switch (transition) {
208     case GST_STATE_CHANGE_READY_TO_PAUSED:
209     {
210       GST_OBJECT_LOCK (mux);
211       mux->last_priority_end = GST_CLOCK_TIME_NONE;
212       GST_OBJECT_UNLOCK (mux);
213       break;
214     }
215     default:
216       break;
217   }
218
219   ret =
220       GST_ELEMENT_CLASS (gst_rtp_dtmf_mux_parent_class)->change_state (element,
221       transition);
222
223   return ret;
224 }
225
226 gboolean
227 gst_rtp_dtmf_mux_plugin_init (GstPlugin * plugin)
228 {
229   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_mux_debug, "rtpdtmfmux", 0,
230       "rtp dtmf muxer");
231
232   return gst_element_register (plugin, "rtpdtmfmux", GST_RANK_NONE,
233       GST_TYPE_RTP_DTMF_MUX);
234 }