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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * SECTION:element-dtmfdetect
27 * @short_description: Detects DTMF tones
29 * This element will detect DTMF tones and emit messages.
31 * The message is called <classname>"dtmf-event"</classname> and has
32 * the following fields:
36 * gint <classname>type</classname> (0-1):
37 * The application uses this field to specify which of the two methods
38 * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
39 * named events. Tones are specified by their frequencies and events are
40 * specfied by their number. This element can only take events as input.
41 * Do not confuse with "method" which specified the output.
46 * gint <classname>number</classname> (0-16):
52 * gint <classname>method</classname> (2):
53 * This field will always been 2 (ie sound) from this element.
63 #include "gstdtmfdetect.h"
67 GST_DEBUG_CATEGORY (dtmf_detect_debug);
68 #define GST_CAT_DEFAULT (dtmf_detect_debug)
70 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS ("audio/x-raw-int, "
76 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
77 "signed = (bool) true, rate = (int) 8000, channels = (int) 1"));
79 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS ("audio/x-raw-int, "
85 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
86 "signed = (bool) true, rate = (int) 8000, channels = (int) 1"));
88 /* signals and args */
100 static gboolean gst_dtmf_detect_set_caps (GstBaseTransform * trans,
101 GstCaps * incaps, GstCaps * outcaps);
102 static GstFlowReturn gst_dtmf_detect_transform_ip (GstBaseTransform * trans,
104 static gboolean gst_dtmf_detect_event (GstBaseTransform * trans,
108 _do_init (GType type)
110 GST_DEBUG_CATEGORY_INIT (dtmf_detect_debug, "dtmfdetect", 0, "dtmfdetect");
113 GST_BOILERPLATE_FULL (GstDtmfDetect, gst_dtmf_detect, GstBaseTransform,
114 GST_TYPE_BASE_TRANSFORM, _do_init);
117 gst_dtmf_detect_base_init (gpointer klass)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121 gst_element_class_add_static_pad_template (element_class, &srctemplate);
122 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
124 gst_element_class_set_details_simple (element_class, "DTMF detector element",
125 "Filter/Analyzer/Audio",
126 "This element detects DTMF tones",
127 "Olivier Crete <olivier.crete@collabora.co.uk>");
131 gst_dtmf_detect_class_init (GstDtmfDetectClass * klass)
133 GstBaseTransformClass *gstbasetransform_class;
135 gstbasetransform_class = (GstBaseTransformClass *) klass;
137 gstbasetransform_class->set_caps =
138 GST_DEBUG_FUNCPTR (gst_dtmf_detect_set_caps);
139 gstbasetransform_class->transform_ip =
140 GST_DEBUG_FUNCPTR (gst_dtmf_detect_transform_ip);
141 gstbasetransform_class->event = GST_DEBUG_FUNCPTR (gst_dtmf_detect_event);
145 gst_dtmf_detect_init (GstDtmfDetect * dtmfdetect, GstDtmfDetectClass * klass)
147 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
148 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
152 gst_dtmf_detect_set_caps (GstBaseTransform * trans, GstCaps * incaps,
155 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
157 zap_dtmf_detect_init (&self->dtmf_state);
164 gst_dtmf_detect_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
166 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
168 gchar dtmfbuf[MAX_DTMF_DIGITS] = "";
171 if (GST_BUFFER_IS_DISCONT (buf))
172 zap_dtmf_detect_init (&self->dtmf_state);
173 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
176 zap_dtmf_detect (&self->dtmf_state, (gint16 *) GST_BUFFER_DATA (buf),
177 GST_BUFFER_SIZE (buf) / 2, FALSE);
179 dtmf_count = zap_dtmf_get (&self->dtmf_state, dtmfbuf, MAX_DTMF_DIGITS);
182 GST_DEBUG_OBJECT (self, "Got %d DTMF events: %s", dtmf_count, dtmfbuf);
184 GST_LOG_OBJECT (self, "Got no DTMF events");
186 for (i = 0; i < dtmf_count; i++) {
187 GstMessage *dtmf_message = NULL;
188 GstStructure *structure;
189 gint dtmf_payload_event;
191 GST_DEBUG_OBJECT (self, "Got DTMF event %c", dtmfbuf[i]);
193 switch (dtmfbuf[i]) {
195 dtmf_payload_event = 0;
198 dtmf_payload_event = 1;
201 dtmf_payload_event = 2;
204 dtmf_payload_event = 3;
207 dtmf_payload_event = 4;
210 dtmf_payload_event = 5;
213 dtmf_payload_event = 6;
216 dtmf_payload_event = 7;
219 dtmf_payload_event = 8;
222 dtmf_payload_event = 9;
225 dtmf_payload_event = 10;
228 dtmf_payload_event = 11;
231 dtmf_payload_event = 12;
234 dtmf_payload_event = 13;
237 dtmf_payload_event = 14;
240 dtmf_payload_event = 15;
246 structure = gst_structure_new ("dtmf-event",
247 "type", G_TYPE_INT, 1,
248 "number", G_TYPE_INT, dtmf_payload_event,
249 "method", G_TYPE_INT, 2, NULL);
250 dtmf_message = gst_message_new_element (GST_OBJECT (self), structure);
251 gst_element_post_message (GST_ELEMENT (self), dtmf_message);
259 gst_dtmf_detect_event (GstBaseTransform * trans, GstEvent * event)
261 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
263 switch (GST_EVENT_TYPE (event)) {
264 case GST_EVENT_FLUSH_STOP:
265 zap_dtmf_detect_init (&self->dtmf_state);
271 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, event,
272 (trans, event), TRUE);
277 gst_dtmf_detect_plugin_init (GstPlugin * plugin)
279 return gst_element_register (plugin, "dtmfdetect",
280 GST_RANK_MARGINAL, GST_TYPE_DTMF_DETECT);