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  *   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.
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 
97 static void
gst_dtmf_detect_class_init(GstDtmfDetectClass * klass)98 gst_dtmf_detect_class_init (GstDtmfDetectClass * klass)
99 {
100   GObjectClass *gobject_class;
101   GstElementClass *gstelement_class;
102   GstBaseTransformClass *gstbasetransform_class;
103 
104   gobject_class = G_OBJECT_CLASS (klass);
105   gstelement_class = GST_ELEMENT_CLASS (klass);
106   gstbasetransform_class = (GstBaseTransformClass *) klass;
107 
108   GST_DEBUG_CATEGORY_INIT (dtmf_detect_debug, "dtmfdetect", 0, "dtmfdetect");
109 
110   gobject_class->finalize = gst_dtmf_detect_finalize;
111 
112   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
113   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
114 
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>");
119 
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);
126 }
127 
128 static void
gst_dtmf_detect_init(GstDtmfDetect * dtmfdetect)129 gst_dtmf_detect_init (GstDtmfDetect * dtmfdetect)
130 {
131   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
132   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
133 }
134 
135 static void
gst_dtmf_detect_finalize(GObject * object)136 gst_dtmf_detect_finalize (GObject * object)
137 {
138   GstDtmfDetect *self = GST_DTMF_DETECT (object);
139 
140   if (self->dtmf_state)
141     dtmf_rx_free (self->dtmf_state);
142 
143   G_OBJECT_CLASS (gst_dtmf_detect_parent_class)->finalize (object);
144 }
145 
146 static void
gst_dtmf_detect_state_reset(GstDtmfDetect * self)147 gst_dtmf_detect_state_reset (GstDtmfDetect * self)
148 {
149   if (self->dtmf_state)
150     dtmf_rx_free (self->dtmf_state);
151   self->dtmf_state = dtmf_rx_init (NULL, NULL, NULL);
152 }
153 
154 static gboolean
gst_dtmf_detect_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)155 gst_dtmf_detect_set_caps (GstBaseTransform * trans, GstCaps * incaps,
156     GstCaps * outcaps)
157 {
158   GstDtmfDetect *self = GST_DTMF_DETECT (trans);
159 
160   gst_dtmf_detect_state_reset (self);
161 
162   return TRUE;
163 }
164 
165 
166 static GstFlowReturn
gst_dtmf_detect_transform_ip(GstBaseTransform * trans,GstBuffer * buf)167 gst_dtmf_detect_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
168 {
169   GstDtmfDetect *self = GST_DTMF_DETECT (trans);
170   gint dtmf_count;
171   gchar dtmfbuf[MAX_DTMF_DIGITS] = "";
172   gint i;
173   GstMapInfo map;
174 
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))
178     return GST_FLOW_OK;
179 
180   gst_buffer_map (buf, &map, GST_MAP_READ);
181 
182   dtmf_rx (self->dtmf_state, (gint16 *) map.data, map.size / 2);
183 
184   dtmf_count = dtmf_rx_get (self->dtmf_state, dtmfbuf, MAX_DTMF_DIGITS);
185 
186   if (dtmf_count)
187     GST_DEBUG_OBJECT (self, "Got %d DTMF events: %s", dtmf_count, dtmfbuf);
188   else
189     GST_LOG_OBJECT (self, "Got no DTMF events");
190 
191   gst_buffer_unmap (buf, &map);
192 
193   for (i = 0; i < dtmf_count; i++) {
194     GstMessage *dtmf_message = NULL;
195     GstStructure *structure;
196     gint dtmf_payload_event;
197 
198     GST_DEBUG_OBJECT (self, "Got DTMF event %c", dtmfbuf[i]);
199 
200     switch (dtmfbuf[i]) {
201       case '0':
202         dtmf_payload_event = 0;
203         break;
204       case '1':
205         dtmf_payload_event = 1;
206         break;
207       case '2':
208         dtmf_payload_event = 2;
209         break;
210       case '3':
211         dtmf_payload_event = 3;
212         break;
213       case '4':
214         dtmf_payload_event = 4;
215         break;
216       case '5':
217         dtmf_payload_event = 5;
218         break;
219       case '6':
220         dtmf_payload_event = 6;
221         break;
222       case '7':
223         dtmf_payload_event = 7;
224         break;
225       case '8':
226         dtmf_payload_event = 8;
227         break;
228       case '9':
229         dtmf_payload_event = 9;
230         break;
231       case '*':
232         dtmf_payload_event = 10;
233         break;
234       case '#':
235         dtmf_payload_event = 11;
236         break;
237       case 'A':
238         dtmf_payload_event = 12;
239         break;
240       case 'B':
241         dtmf_payload_event = 13;
242         break;
243       case 'C':
244         dtmf_payload_event = 14;
245         break;
246       case 'D':
247         dtmf_payload_event = 15;
248         break;
249       default:
250         continue;
251     }
252 
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);
259   }
260 
261   return GST_FLOW_OK;
262 }
263 
264 
265 static gboolean
gst_dtmf_detect_sink_event(GstBaseTransform * trans,GstEvent * event)266 gst_dtmf_detect_sink_event (GstBaseTransform * trans, GstEvent * event)
267 {
268   GstDtmfDetect *self = GST_DTMF_DETECT (trans);
269 
270   switch (GST_EVENT_TYPE (event)) {
271     case GST_EVENT_FLUSH_STOP:
272       gst_dtmf_detect_state_reset (self);
273       break;
274     default:
275       break;
276   }
277 
278   return GST_BASE_TRANSFORM_CLASS (gst_dtmf_detect_parent_class)->sink_event
279       (trans, event);
280 }
281 
282 
283 gboolean
gst_dtmf_detect_plugin_init(GstPlugin * plugin)284 gst_dtmf_detect_plugin_init (GstPlugin * plugin)
285 {
286   return gst_element_register (plugin, "dtmfdetect",
287       GST_RANK_MARGINAL, GST_TYPE_DTMF_DETECT);
288 }
289