1 /*
2 * GStreamer - DTMF Detection
3 *
4 * Copyright 2009 Nokia Corporation
5 * Copyright 2009 Collabora Ltd,
6 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
7 *
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.
12 *
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.
17 *
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.
22 *
23 */
24
25 /**
26 * SECTION:element-dtmfdetect
27 * @title: dtmfdetect
28 * @short_description: Detects DTMF tones
29 *
30 * This element will detect DTMF tones and emit messages.
31 *
32 * The message is called `dtmf-event` and has the following fields:
33 *
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 * specified 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.
41 *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include "gstdtmfdetect.h"
49
50 #include <string.h>
51
52 #include <gst/audio/audio.h>
53
54 GST_DEBUG_CATEGORY (dtmf_detect_debug);
55 #define GST_CAT_DEFAULT (dtmf_detect_debug)
56
57 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_PAD_SINK,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("audio/x-raw, "
61 "format = (string) \"" GST_AUDIO_NE (S16) "\", "
62 "rate = (int) 8000, " "channels = (int) 1")
63 );
64
65
66 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
67 GST_PAD_SRC,
68 GST_PAD_ALWAYS,
69 GST_STATIC_CAPS ("audio/x-raw, "
70 "format = (string) \"" GST_AUDIO_NE (S16) "\", "
71 "rate = (int) 8000, " "channels = (int) 1")
72 );
73
74 /* signals and args */
75 enum
76 {
77 /* FILL ME */
78 LAST_SIGNAL
79 };
80
81 enum
82 {
83 PROP_0,
84 };
85
86 static void gst_dtmf_detect_finalize (GObject * object);
87
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,
91 GstBuffer * buf);
92 static gboolean gst_dtmf_detect_sink_event (GstBaseTransform * trans,
93 GstEvent * event);
94
95 G_DEFINE_TYPE (GstDtmfDetect, gst_dtmf_detect, GST_TYPE_BASE_TRANSFORM);
96 GST_ELEMENT_REGISTER_DEFINE (dtmfdetect, "dtmfdetect",
97 GST_RANK_MARGINAL, GST_TYPE_DTMF_DETECT);
98
99 static void
gst_dtmf_detect_class_init(GstDtmfDetectClass * klass)100 gst_dtmf_detect_class_init (GstDtmfDetectClass * klass)
101 {
102 GObjectClass *gobject_class;
103 GstElementClass *gstelement_class;
104 GstBaseTransformClass *gstbasetransform_class;
105
106 gobject_class = G_OBJECT_CLASS (klass);
107 gstelement_class = GST_ELEMENT_CLASS (klass);
108 gstbasetransform_class = (GstBaseTransformClass *) klass;
109
110 GST_DEBUG_CATEGORY_INIT (dtmf_detect_debug, "dtmfdetect", 0, "dtmfdetect");
111
112 gobject_class->finalize = gst_dtmf_detect_finalize;
113
114 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
115 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
116
117 gst_element_class_set_static_metadata (gstelement_class,
118 "DTMF detector element", "Filter/Analyzer/Audio",
119 "This element detects DTMF tones",
120 "Olivier Crete <olivier.crete@collabora.com>");
121
122 gstbasetransform_class->set_caps =
123 GST_DEBUG_FUNCPTR (gst_dtmf_detect_set_caps);
124 gstbasetransform_class->transform_ip =
125 GST_DEBUG_FUNCPTR (gst_dtmf_detect_transform_ip);
126 gstbasetransform_class->sink_event =
127 GST_DEBUG_FUNCPTR (gst_dtmf_detect_sink_event);
128 }
129
130 static void
gst_dtmf_detect_init(GstDtmfDetect * dtmfdetect)131 gst_dtmf_detect_init (GstDtmfDetect * dtmfdetect)
132 {
133 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
134 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
135 }
136
137 static void
gst_dtmf_detect_finalize(GObject * object)138 gst_dtmf_detect_finalize (GObject * object)
139 {
140 GstDtmfDetect *self = GST_DTMF_DETECT (object);
141
142 if (self->dtmf_state)
143 dtmf_rx_free (self->dtmf_state);
144
145 G_OBJECT_CLASS (gst_dtmf_detect_parent_class)->finalize (object);
146 }
147
148 static void
gst_dtmf_detect_state_reset(GstDtmfDetect * self)149 gst_dtmf_detect_state_reset (GstDtmfDetect * self)
150 {
151 if (self->dtmf_state)
152 dtmf_rx_free (self->dtmf_state);
153 self->dtmf_state = dtmf_rx_init (NULL, NULL, NULL);
154 }
155
156 static gboolean
gst_dtmf_detect_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)157 gst_dtmf_detect_set_caps (GstBaseTransform * trans, GstCaps * incaps,
158 GstCaps * outcaps)
159 {
160 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
161
162 gst_dtmf_detect_state_reset (self);
163
164 return TRUE;
165 }
166
167
168 static GstFlowReturn
gst_dtmf_detect_transform_ip(GstBaseTransform * trans,GstBuffer * buf)169 gst_dtmf_detect_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
170 {
171 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
172 gint dtmf_count;
173 gchar dtmfbuf[MAX_DTMF_DIGITS] = "";
174 gint i;
175 GstMapInfo map;
176
177 if (GST_BUFFER_IS_DISCONT (buf))
178 gst_dtmf_detect_state_reset (self);
179 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
180 return GST_FLOW_OK;
181
182 gst_buffer_map (buf, &map, GST_MAP_READ);
183
184 dtmf_rx (self->dtmf_state, (gint16 *) map.data, map.size / 2);
185
186 dtmf_count = dtmf_rx_get (self->dtmf_state, dtmfbuf, MAX_DTMF_DIGITS);
187
188 if (dtmf_count)
189 GST_DEBUG_OBJECT (self, "Got %d DTMF events: %s", dtmf_count, dtmfbuf);
190 else
191 GST_LOG_OBJECT (self, "Got no DTMF events");
192
193 gst_buffer_unmap (buf, &map);
194
195 for (i = 0; i < dtmf_count; i++) {
196 GstMessage *dtmf_message = NULL;
197 GstStructure *structure;
198 gint dtmf_payload_event;
199
200 GST_DEBUG_OBJECT (self, "Got DTMF event %c", dtmfbuf[i]);
201
202 switch (dtmfbuf[i]) {
203 case '0':
204 dtmf_payload_event = 0;
205 break;
206 case '1':
207 dtmf_payload_event = 1;
208 break;
209 case '2':
210 dtmf_payload_event = 2;
211 break;
212 case '3':
213 dtmf_payload_event = 3;
214 break;
215 case '4':
216 dtmf_payload_event = 4;
217 break;
218 case '5':
219 dtmf_payload_event = 5;
220 break;
221 case '6':
222 dtmf_payload_event = 6;
223 break;
224 case '7':
225 dtmf_payload_event = 7;
226 break;
227 case '8':
228 dtmf_payload_event = 8;
229 break;
230 case '9':
231 dtmf_payload_event = 9;
232 break;
233 case '*':
234 dtmf_payload_event = 10;
235 break;
236 case '#':
237 dtmf_payload_event = 11;
238 break;
239 case 'A':
240 dtmf_payload_event = 12;
241 break;
242 case 'B':
243 dtmf_payload_event = 13;
244 break;
245 case 'C':
246 dtmf_payload_event = 14;
247 break;
248 case 'D':
249 dtmf_payload_event = 15;
250 break;
251 default:
252 continue;
253 }
254
255 structure = gst_structure_new ("dtmf-event",
256 "type", G_TYPE_INT, 1,
257 "number", G_TYPE_INT, dtmf_payload_event,
258 "method", G_TYPE_INT, 2, NULL);
259 dtmf_message = gst_message_new_element (GST_OBJECT (self), structure);
260 gst_element_post_message (GST_ELEMENT (self), dtmf_message);
261 }
262
263 return GST_FLOW_OK;
264 }
265
266
267 static gboolean
gst_dtmf_detect_sink_event(GstBaseTransform * trans,GstEvent * event)268 gst_dtmf_detect_sink_event (GstBaseTransform * trans, GstEvent * event)
269 {
270 GstDtmfDetect *self = GST_DTMF_DETECT (trans);
271
272 switch (GST_EVENT_TYPE (event)) {
273 case GST_EVENT_FLUSH_STOP:
274 gst_dtmf_detect_state_reset (self);
275 break;
276 default:
277 break;
278 }
279
280 return GST_BASE_TRANSFORM_CLASS (gst_dtmf_detect_parent_class)->sink_event
281 (trans, event);
282 }
283