2009-11-05 03:19:58 +00:00
|
|
|
/*
|
|
|
|
* GStreamer - DTMF Detection
|
|
|
|
*
|
|
|
|
* Copyright 2009 Nokia Corporation
|
|
|
|
* Copyright 2009 Collabora Ltd,
|
|
|
|
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-11-05 03:19:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-dtmfdetect
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: dtmfdetect
|
2009-11-05 03:19:58 +00:00
|
|
|
* @short_description: Detects DTMF tones
|
|
|
|
*
|
2010-03-25 20:53:20 +00:00
|
|
|
* This element will detect DTMF tones and emit messages.
|
2009-11-05 03:19:58 +00:00
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* The message is called `dtmf-event` and has the following fields:
|
|
|
|
*
|
|
|
|
* * gint `type` (0-1): The application uses this field to specify which of the two methods
|
2010-03-25 20:53:20 +00:00
|
|
|
* specified in RFC 2833 to use. The value should be 0 for tones and 1 for
|
|
|
|
* named events. Tones are specified by their frequencies and events are
|
|
|
|
* specfied by their number. This element can only take events as input.
|
|
|
|
* Do not confuse with "method" which specified the output.
|
2017-03-08 18:01:13 +00:00
|
|
|
* * gint `number` (0-16): The event number.
|
|
|
|
* * gint `method` (2): This field will always been 2 (ie sound) from this element.
|
|
|
|
*
|
2009-11-05 03:19:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstdtmfdetect.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-01-27 11:07:43 +00:00
|
|
|
#include <gst/audio/audio.h>
|
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
GST_DEBUG_CATEGORY (dtmf_detect_debug);
|
|
|
|
#define GST_CAT_DEFAULT (dtmf_detect_debug)
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-01-27 11:07:43 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
|
|
|
"format = (string) \"" GST_AUDIO_NE (S16) "\", "
|
|
|
|
"rate = (int) 8000, " "channels = (int) 1")
|
|
|
|
);
|
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-01-27 11:07:43 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
|
|
|
"format = (string) \"" GST_AUDIO_NE (S16) "\", "
|
|
|
|
"rate = (int) 8000, " "channels = (int) 1")
|
|
|
|
);
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
/* signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
};
|
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
static void gst_dtmf_detect_finalize (GObject * object);
|
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
static gboolean gst_dtmf_detect_set_caps (GstBaseTransform * trans,
|
|
|
|
GstCaps * incaps, GstCaps * outcaps);
|
|
|
|
static GstFlowReturn gst_dtmf_detect_transform_ip (GstBaseTransform * trans,
|
|
|
|
GstBuffer * buf);
|
2012-01-25 10:21:50 +00:00
|
|
|
static gboolean gst_dtmf_detect_sink_event (GstBaseTransform * trans,
|
2009-11-05 03:19:58 +00:00
|
|
|
GstEvent * event);
|
|
|
|
|
2012-01-25 10:21:50 +00:00
|
|
|
G_DEFINE_TYPE (GstDtmfDetect, gst_dtmf_detect, GST_TYPE_BASE_TRANSFORM);
|
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
static void
|
2012-01-25 10:21:50 +00:00
|
|
|
gst_dtmf_detect_class_init (GstDtmfDetectClass * klass)
|
2009-11-05 03:19:58 +00:00
|
|
|
{
|
2013-01-10 00:59:16 +00:00
|
|
|
GObjectClass *gobject_class;
|
2012-01-25 10:21:50 +00:00
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstBaseTransformClass *gstbasetransform_class;
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
2012-01-25 10:21:50 +00:00
|
|
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
gstbasetransform_class = (GstBaseTransformClass *) klass;
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2012-01-25 10:21:50 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (dtmf_detect_debug, "dtmfdetect", 0, "dtmfdetect");
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
gobject_class->finalize = gst_dtmf_detect_finalize;
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
|
|
|
"DTMF detector element", "Filter/Analyzer/Audio",
|
2010-03-18 16:30:26 +00:00
|
|
|
"This element detects DTMF tones",
|
2012-01-25 10:21:50 +00:00
|
|
|
"Olivier Crete <olivier.crete@collabora.com>");
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
gstbasetransform_class->set_caps =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_dtmf_detect_set_caps);
|
|
|
|
gstbasetransform_class->transform_ip =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_dtmf_detect_transform_ip);
|
2012-01-25 10:21:50 +00:00
|
|
|
gstbasetransform_class->sink_event =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_dtmf_detect_sink_event);
|
2009-11-05 03:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-25 10:21:50 +00:00
|
|
|
gst_dtmf_detect_init (GstDtmfDetect * dtmfdetect)
|
2009-11-05 03:19:58 +00:00
|
|
|
{
|
|
|
|
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
|
|
|
|
gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (dtmfdetect), TRUE);
|
|
|
|
}
|
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
static void
|
|
|
|
gst_dtmf_detect_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstDtmfDetect *self = GST_DTMF_DETECT (object);
|
|
|
|
|
|
|
|
if (self->dtmf_state)
|
|
|
|
dtmf_rx_free (self->dtmf_state);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_dtmf_detect_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_dtmf_detect_state_reset (GstDtmfDetect * self)
|
|
|
|
{
|
|
|
|
if (self->dtmf_state)
|
|
|
|
dtmf_rx_free (self->dtmf_state);
|
|
|
|
self->dtmf_state = dtmf_rx_init (NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_dtmf_detect_set_caps (GstBaseTransform * trans, GstCaps * incaps,
|
|
|
|
GstCaps * outcaps)
|
|
|
|
{
|
|
|
|
GstDtmfDetect *self = GST_DTMF_DETECT (trans);
|
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
gst_dtmf_detect_state_reset (self);
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_dtmf_detect_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstDtmfDetect *self = GST_DTMF_DETECT (trans);
|
2010-03-25 20:55:32 +00:00
|
|
|
gint dtmf_count;
|
|
|
|
gchar dtmfbuf[MAX_DTMF_DIGITS] = "";
|
|
|
|
gint i;
|
2012-01-25 13:50:50 +00:00
|
|
|
GstMapInfo map;
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
if (GST_BUFFER_IS_DISCONT (buf))
|
2013-01-10 00:59:16 +00:00
|
|
|
gst_dtmf_detect_state_reset (self);
|
2010-03-25 20:58:47 +00:00
|
|
|
if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
|
|
|
|
return GST_FLOW_OK;
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2012-01-25 13:50:50 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
2012-01-25 10:21:50 +00:00
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
dtmf_rx (self->dtmf_state, (gint16 *) map.data, map.size / 2);
|
2009-11-05 03:19:58 +00:00
|
|
|
|
2013-01-10 00:59:16 +00:00
|
|
|
dtmf_count = dtmf_rx_get (self->dtmf_state, dtmfbuf, MAX_DTMF_DIGITS);
|
2009-11-05 03:19:58 +00:00
|
|
|
|
|
|
|
if (dtmf_count)
|
|
|
|
GST_DEBUG_OBJECT (self, "Got %d DTMF events: %s", dtmf_count, dtmfbuf);
|
|
|
|
else
|
|
|
|
GST_LOG_OBJECT (self, "Got no DTMF events");
|
|
|
|
|
2012-01-25 13:50:50 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2012-01-25 10:21:50 +00:00
|
|
|
|
2009-11-05 03:19:58 +00:00
|
|
|
for (i = 0; i < dtmf_count; i++) {
|
|
|
|
GstMessage *dtmf_message = NULL;
|
|
|
|
GstStructure *structure;
|
|
|
|
gint dtmf_payload_event;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Got DTMF event %c", dtmfbuf[i]);
|
|
|
|
|
|
|
|
switch (dtmfbuf[i]) {
|
|
|
|
case '0':
|
|
|
|
dtmf_payload_event = 0;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
dtmf_payload_event = 1;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
dtmf_payload_event = 2;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
dtmf_payload_event = 3;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
dtmf_payload_event = 4;
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
dtmf_payload_event = 5;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
dtmf_payload_event = 6;
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
dtmf_payload_event = 7;
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
dtmf_payload_event = 8;
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
dtmf_payload_event = 9;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
dtmf_payload_event = 10;
|
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
dtmf_payload_event = 11;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
dtmf_payload_event = 12;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
dtmf_payload_event = 13;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
dtmf_payload_event = 14;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
dtmf_payload_event = 15;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
structure = gst_structure_new ("dtmf-event",
|
|
|
|
"type", G_TYPE_INT, 1,
|
|
|
|
"number", G_TYPE_INT, dtmf_payload_event,
|
|
|
|
"method", G_TYPE_INT, 2, NULL);
|
|
|
|
dtmf_message = gst_message_new_element (GST_OBJECT (self), structure);
|
|
|
|
gst_element_post_message (GST_ELEMENT (self), dtmf_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2012-01-25 10:21:50 +00:00
|
|
|
gst_dtmf_detect_sink_event (GstBaseTransform * trans, GstEvent * event)
|
2009-11-05 03:19:58 +00:00
|
|
|
{
|
|
|
|
GstDtmfDetect *self = GST_DTMF_DETECT (trans);
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2013-01-10 00:59:16 +00:00
|
|
|
gst_dtmf_detect_state_reset (self);
|
2009-11-05 03:19:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-27 11:07:43 +00:00
|
|
|
return GST_BASE_TRANSFORM_CLASS (gst_dtmf_detect_parent_class)->sink_event
|
|
|
|
(trans, event);
|
2009-11-05 03:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_dtmf_detect_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "dtmfdetect",
|
|
|
|
GST_RANK_MARGINAL, GST_TYPE_DTMF_DETECT);
|
|
|
|
}
|