2 * GStreamer - DTMF Detection
4 * Copyright 2009 Nokia Corporation
5 * Copyright 2009 Collabora Ltd,
6 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 * SECTION:element-dtmfdetect
28 * @short_description: Detects DTMF tones
30 * This element will detect DTMF tones and emit messages.
32 * The message is called `dtmf-event` and has the following fields:
34 * * gint `type` (0-1): The application uses this field to specify which of the two methods
35 * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
36 * named events. Tones are specified by their frequencies and events are
37 * specfied by their number. This element can only take events as input.
38 * Do not confuse with "method" which specified the output.
39 * * gint `number` (0-16): The event number.
40 * * gint `method` (2): This field will always been 2 (ie sound) from this element.
48 #include "gstdtmfdetect.h"
52 #include <gst/audio/audio.h>
54 GST_DEBUG_CATEGORY (dtmf_detect_debug);
55 #define GST_CAT_DEFAULT (dtmf_detect_debug)
57 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("audio/x-raw, "
61 "format = (string) \"" GST_AUDIO_NE (S16) "\", "
62 "rate = (int) 8000, " "channels = (int) 1")
66 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS ("audio/x-raw, "
70 "format = (string) \"" GST_AUDIO_NE (S16) "\", "
71 "rate = (int) 8000, " "channels = (int) 1")
74 /* signals and args */
86 static void gst_dtmf_detect_finalize (GObject * object);
88 static gboolean gst_dtmf_detect_set_caps (GstBaseTransform * trans,
89 GstCaps * incaps, GstCaps * outcaps);
90 static GstFlowReturn gst_dtmf_detect_transform_ip (GstBaseTransform * trans,
92 static gboolean gst_dtmf_detect_sink_event (GstBaseTransform * trans,
95 G_DEFINE_TYPE (GstDtmfDetect, gst_dtmf_detect, GST_TYPE_BASE_TRANSFORM);
98 gst_dtmf_detect_class_init (GstDtmfDetectClass * klass)
100 GObjectClass *gobject_class;
101 GstElementClass *gstelement_class;
102 GstBaseTransformClass *gstbasetransform_class;
104 gobject_class = G_OBJECT_CLASS (klass);
105 gstelement_class = GST_ELEMENT_CLASS (klass);
106 gstbasetransform_class = (GstBaseTransformClass *) klass;
108 GST_DEBUG_CATEGORY_INIT (dtmf_detect_debug, "dtmfdetect", 0, "dtmfdetect");
110 gobject_class->finalize = gst_dtmf_detect_finalize;
112 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
113 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
115 gst_element_class_set_static_metadata (gstelement_class,
116 "DTMF detector element", "Filter/Analyzer/Audio",
117 "This element detects DTMF tones",
118 "Olivier Crete <olivier.crete@collabora.com>");
120 gstbasetransform_class->set_caps =
121 GST_DEBUG_FUNCPTR (gst_dtmf_detect_set_caps);
122 gstbasetransform_class->transform_ip =
123 GST_DEBUG_FUNCPTR (gst_dtmf_detect_transform_ip);
124 gstbasetransform_class->sink_event =
125 GST_DEBUG_FUNCPTR (gst_dtmf_detect_sink_event);
129 gst_dtmf_detect_init (GstDtmfDetect * dtmfdetect)
131 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
132 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
136 gst_dtmf_detect_finalize (GObject * object)
138 GstDtmfDetect *self = GST_DTMF_DETECT (object);
140 if (self->dtmf_state)
141 dtmf_rx_free (self->dtmf_state);
143 G_OBJECT_CLASS (gst_dtmf_detect_parent_class)->finalize (object);
147 gst_dtmf_detect_state_reset (GstDtmfDetect * self)
149 if (self->dtmf_state)
150 dtmf_rx_free (self->dtmf_state);
151 self->dtmf_state = dtmf_rx_init (NULL, NULL, NULL);
155 gst_dtmf_detect_set_caps (GstBaseTransform * trans, GstCaps * incaps,
158 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
160 gst_dtmf_detect_state_reset (self);
167 gst_dtmf_detect_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
169 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
171 gchar dtmfbuf[MAX_DTMF_DIGITS] = "";
175 if (GST_BUFFER_IS_DISCONT (buf))
176 gst_dtmf_detect_state_reset (self);
177 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
180 gst_buffer_map (buf, &map, GST_MAP_READ);
182 dtmf_rx (self->dtmf_state, (gint16 *) map.data, map.size / 2);
184 dtmf_count = dtmf_rx_get (self->dtmf_state, dtmfbuf, MAX_DTMF_DIGITS);
187 GST_DEBUG_OBJECT (self, "Got %d DTMF events: %s", dtmf_count, dtmfbuf);
189 GST_LOG_OBJECT (self, "Got no DTMF events");
191 gst_buffer_unmap (buf, &map);
193 for (i = 0; i < dtmf_count; i++) {
194 GstMessage *dtmf_message = NULL;
195 GstStructure *structure;
196 gint dtmf_payload_event;
198 GST_DEBUG_OBJECT (self, "Got DTMF event %c", dtmfbuf[i]);
200 switch (dtmfbuf[i]) {
202 dtmf_payload_event = 0;
205 dtmf_payload_event = 1;
208 dtmf_payload_event = 2;
211 dtmf_payload_event = 3;
214 dtmf_payload_event = 4;
217 dtmf_payload_event = 5;
220 dtmf_payload_event = 6;
223 dtmf_payload_event = 7;
226 dtmf_payload_event = 8;
229 dtmf_payload_event = 9;
232 dtmf_payload_event = 10;
235 dtmf_payload_event = 11;
238 dtmf_payload_event = 12;
241 dtmf_payload_event = 13;
244 dtmf_payload_event = 14;
247 dtmf_payload_event = 15;
253 structure = gst_structure_new ("dtmf-event",
254 "type", G_TYPE_INT, 1,
255 "number", G_TYPE_INT, dtmf_payload_event,
256 "method", G_TYPE_INT, 2, NULL);
257 dtmf_message = gst_message_new_element (GST_OBJECT (self), structure);
258 gst_element_post_message (GST_ELEMENT (self), dtmf_message);
266 gst_dtmf_detect_sink_event (GstBaseTransform * trans, GstEvent * event)
268 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
270 switch (GST_EVENT_TYPE (event)) {
271 case GST_EVENT_FLUSH_STOP:
272 gst_dtmf_detect_state_reset (self);
278 return GST_BASE_TRANSFORM_CLASS (gst_dtmf_detect_parent_class)->sink_event
284 gst_dtmf_detect_plugin_init (GstPlugin * plugin)
286 return gst_element_register (plugin, "dtmfdetect",
287 GST_RANK_MARGINAL, GST_TYPE_DTMF_DETECT);