3 * Copyright (C) 2008 Collabora Limited
4 * Copyright (C) 2008 Nokia Corporation
5 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * SECTION:element-rtpdtmfdepay
24 * @see_also: rtpdtmfsrc, rtpdtmfmux
26 * This element takes RTP DTMF packets and produces sound. It also emits a
27 * message on the #GstBus.
29 * The message is called "dtmf-event" and has the following fields
32 * <colspec colname='Name' />
33 * <colspec colname='Type' />
34 * <colspec colname='Possible values' />
35 * <colspec colname='Purpose' />
39 * <entry>GType</entry>
40 * <entry>Possible values</entry>
41 * <entry>Purpose</entry>
47 * <entry>G_TYPE_INT</entry>
49 * <entry>Which of the two methods
50 * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
51 * named events. Tones are specified by their frequencies and events are specied
52 * by their number. This element currently only recognizes events.
53 * Do not confuse with "method" which specified the output.
57 * <entry>number</entry>
58 * <entry>G_TYPE_INT</entry>
60 * <entry>The event number.</entry>
63 * <entry>volume</entry>
64 * <entry>G_TYPE_INT</entry>
66 * <entry>This field describes the power level of the tone, expressed in dBm0
67 * after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
68 * valid DTMF is from 0 to -36 dBm0.
72 * <entry>method</entry>
73 * <entry>G_TYPE_INT</entry>
75 * <entry>This field will always been 1 (ie RTP event) from this element.
90 #include <gst/rtp/gstrtpbuffer.h>
91 #include "gstrtpdtmfdepay.h"
94 # define M_PI 3.14159265358979323846 /* pi */
98 #define DEFAULT_PACKET_INTERVAL 50 /* ms */
99 #define MIN_PACKET_INTERVAL 10 /* ms */
100 #define MAX_PACKET_INTERVAL 50 /* ms */
101 #define SAMPLE_RATE 8000
102 #define SAMPLE_SIZE 16
107 #define MAX_VOLUME 36
108 #define MIN_INTER_DIGIT_INTERVAL 100
109 #define MIN_PULSE_DURATION 250
110 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
112 #define MIN_UNIT_TIME 0
113 #define MAX_UNIT_TIME 1000
114 #define DEFAULT_UNIT_TIME 0
116 #define DEFAULT_MAX_DURATION 0
118 typedef struct st_dtmf_key
120 const char *event_name;
123 float high_frequency;
126 static const DTMF_KEY DTMF_KEYS[] = {
127 {"DTMF_KEY_EVENT_0", 0, 941, 1336},
128 {"DTMF_KEY_EVENT_1", 1, 697, 1209},
129 {"DTMF_KEY_EVENT_2", 2, 697, 1336},
130 {"DTMF_KEY_EVENT_3", 3, 697, 1477},
131 {"DTMF_KEY_EVENT_4", 4, 770, 1209},
132 {"DTMF_KEY_EVENT_5", 5, 770, 1336},
133 {"DTMF_KEY_EVENT_6", 6, 770, 1477},
134 {"DTMF_KEY_EVENT_7", 7, 852, 1209},
135 {"DTMF_KEY_EVENT_8", 8, 852, 1336},
136 {"DTMF_KEY_EVENT_9", 9, 852, 1477},
137 {"DTMF_KEY_EVENT_S", 10, 941, 1209},
138 {"DTMF_KEY_EVENT_P", 11, 941, 1477},
139 {"DTMF_KEY_EVENT_A", 12, 697, 1633},
140 {"DTMF_KEY_EVENT_B", 13, 770, 1633},
141 {"DTMF_KEY_EVENT_C", 14, 852, 1633},
142 {"DTMF_KEY_EVENT_D", 15, 941, 1633},
145 #define MAX_DTMF_EVENTS 16
149 DTMF_KEY_EVENT_1 = 1,
150 DTMF_KEY_EVENT_2 = 2,
151 DTMF_KEY_EVENT_3 = 3,
152 DTMF_KEY_EVENT_4 = 4,
153 DTMF_KEY_EVENT_5 = 5,
154 DTMF_KEY_EVENT_6 = 6,
155 DTMF_KEY_EVENT_7 = 7,
156 DTMF_KEY_EVENT_8 = 8,
157 DTMF_KEY_EVENT_9 = 9,
158 DTMF_KEY_EVENT_0 = 0,
159 DTMF_KEY_EVENT_STAR = 10,
160 DTMF_KEY_EVENT_POUND = 11,
161 DTMF_KEY_EVENT_A = 12,
162 DTMF_KEY_EVENT_B = 13,
163 DTMF_KEY_EVENT_C = 14,
164 DTMF_KEY_EVENT_D = 15,
167 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
168 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
190 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
191 GST_STATIC_PAD_TEMPLATE ("src",
194 GST_STATIC_CAPS ("audio/x-raw-int, "
197 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
198 "signed = (boolean) true, "
199 "rate = (int) [0, MAX], " "channels = (int) 1")
202 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
203 GST_STATIC_PAD_TEMPLATE ("sink",
206 GST_STATIC_CAPS ("application/x-rtp, "
207 "media = (string) \"audio\", "
208 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
209 "clock-rate = (int) [ 0, MAX ], "
210 "encoding-name = (string) \"TELEPHONE-EVENT\"")
213 GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
214 GST_TYPE_BASE_RTP_DEPAYLOAD);
216 static void gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
217 const GValue * value, GParamSpec * pspec);
218 static void gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec);
220 static GstBuffer *gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload,
222 gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
226 gst_rtp_dtmf_depay_base_init (gpointer klass)
228 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
230 gst_element_class_add_pad_template (element_class,
231 gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
232 gst_element_class_add_pad_template (element_class,
233 gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
236 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
237 "rtpdtmfdepay", 0, "rtpdtmfdepay element");
238 gst_element_class_set_details_simple (element_class,
239 "RTP DTMF packet depayloader", "Codec/Depayloader/Network",
240 "Generates DTMF Sound from telephone-event RTP packets",
241 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
245 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
247 GObjectClass *gobject_class;
248 GstElementClass *gstelement_class;
249 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
251 gobject_class = (GObjectClass *) klass;
252 gstelement_class = (GstElementClass *) klass;
253 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
255 parent_class = g_type_class_peek_parent (klass);
257 gobject_class->set_property =
258 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
259 gobject_class->get_property =
260 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_get_property);
262 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
263 g_param_spec_uint ("unit-time", "Duration unittime",
264 "The smallest unit (ms) the duration must be a multiple of (0 disables it)",
265 MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME,
266 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
269 g_param_spec_uint ("max-duration", "Maximum duration",
270 "The maxumimum duration (ms) of the outgoing soundpacket. "
271 "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
272 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 gstbasertpdepayload_class->process =
275 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
276 gstbasertpdepayload_class->set_caps =
277 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
282 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
283 GstRtpDTMFDepayClass * klass)
285 rtpdtmfdepay->unit_time = DEFAULT_UNIT_TIME;
289 gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec)
292 GstRtpDTMFDepay *rtpdtmfdepay;
294 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
298 rtpdtmfdepay->unit_time = g_value_get_uint (value);
300 case PROP_MAX_DURATION:
301 rtpdtmfdepay->max_duration = g_value_get_uint (value);
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
311 GValue * value, GParamSpec * pspec)
313 GstRtpDTMFDepay *rtpdtmfdepay;
315 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
319 g_value_set_uint (value, rtpdtmfdepay->unit_time);
321 case PROP_MAX_DURATION:
322 g_value_set_uint (value, rtpdtmfdepay->max_duration);
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
334 GstStructure *structure = gst_caps_get_structure (caps, 0);
335 gint clock_rate = 8000; /* default */
337 gst_structure_get_int (structure, "clock-rate", &clock_rate);
338 filter->clock_rate = clock_rate;
340 srccaps = gst_caps_new_simple ("audio/x-raw-int",
341 "width", G_TYPE_INT, 16,
342 "depth", G_TYPE_INT, 16,
343 "endianness", G_TYPE_INT, G_BYTE_ORDER,
344 "signed", G_TYPE_BOOLEAN, TRUE,
345 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
346 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
347 gst_caps_unref (srccaps);
353 gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
354 GstRTPDTMFPayload payload, GstBuffer * buffer)
359 double amplitude, f1, f2;
360 double volume_factor;
361 DTMF_KEY key = DTMF_KEYS[payload.event];
362 guint32 clock_rate = 8000 /* default */ ;
363 GstBaseRTPDepayload *depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
366 clock_rate = depayload->clock_rate;
368 /* Create a buffer for the tone */
369 tone_size = (payload.duration * SAMPLE_SIZE * CHANNELS) / 8;
370 GST_BUFFER_SIZE (buffer) = tone_size;
371 GST_BUFFER_MALLOCDATA (buffer) = g_malloc (tone_size);
372 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
373 GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
374 volume = payload.volume;
376 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
378 volume_factor = pow (10, (-volume) / 20);
381 * For each sample point we calculate 'x' as the
382 * the amplitude value.
384 for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
386 * We add the fundamental frequencies together.
388 f1 = sin (2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample /
390 f2 = sin (2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample /
393 amplitude = (f1 + f2) / 2;
395 /* Adjust the volume */
396 amplitude *= volume_factor;
398 /* Make the [-1:1] interval into a [-32767:32767] interval */
401 /* Store it in the data buffer */
402 *(p++) = (gint16) amplitude;
404 (rtpdtmfdepay->sample)++;
410 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
413 GstRtpDTMFDepay *rtpdtmfdepay = NULL;
414 GstBuffer *outbuf = NULL;
416 guint8 *payload = NULL;
418 GstRTPDTMFPayload dtmf_payload;
420 GstStructure *structure = NULL;
421 GstMessage *dtmf_message = NULL;
423 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
425 if (!gst_rtp_buffer_validate (buf))
428 payload_len = gst_rtp_buffer_get_payload_len (buf);
429 payload = gst_rtp_buffer_get_payload (buf);
431 if (payload_len != sizeof (GstRTPDTMFPayload))
434 memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
436 if (dtmf_payload.event > MAX_EVENT)
440 marker = gst_rtp_buffer_get_marker (buf);
442 timestamp = gst_rtp_buffer_get_timestamp (buf);
444 dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
446 /* clip to whole units of unit_time */
447 if (rtpdtmfdepay->unit_time) {
448 guint unit_time_clock =
449 (rtpdtmfdepay->unit_time * depayload->clock_rate) / 1000;
450 if (dtmf_payload.duration % unit_time_clock) {
451 /* Make sure we don't overflow the duration */
452 if (dtmf_payload.duration < G_MAXUINT16 - unit_time_clock)
453 dtmf_payload.duration += unit_time_clock -
454 (dtmf_payload.duration % unit_time_clock);
456 dtmf_payload.duration -= dtmf_payload.duration % unit_time_clock;
460 /* clip to max duration */
461 if (rtpdtmfdepay->max_duration) {
462 guint max_duration_clock =
463 (rtpdtmfdepay->max_duration * depayload->clock_rate) / 1000;
465 if (max_duration_clock < G_MAXUINT16 &&
466 dtmf_payload.duration > max_duration_clock)
467 dtmf_payload.duration = max_duration_clock;
470 GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
471 "marker=%d - timestamp=%u - event=%d - duration=%d",
472 marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
474 GST_DEBUG_OBJECT (depayload,
475 "Previous information : timestamp=%u - duration=%d",
476 rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
479 if (marker || rtpdtmfdepay->previous_ts != timestamp) {
480 rtpdtmfdepay->sample = 0;
481 rtpdtmfdepay->previous_ts = timestamp;
482 rtpdtmfdepay->previous_duration = dtmf_payload.duration;
483 rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
485 structure = gst_structure_new ("dtmf-event",
486 "number", G_TYPE_INT, dtmf_payload.event,
487 "volume", G_TYPE_INT, dtmf_payload.volume,
488 "type", G_TYPE_INT, 1, "method", G_TYPE_INT, 1, NULL);
491 gst_message_new_element (GST_OBJECT (depayload), structure);
493 if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
494 GST_ERROR_OBJECT (depayload,
495 "Unable to send dtmf-event message to bus");
498 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
501 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
504 guint16 duration = dtmf_payload.duration;
505 dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
506 /* If late buffer, ignore */
507 if (duration > rtpdtmfdepay->previous_duration)
508 rtpdtmfdepay->previous_duration = duration;
511 GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
512 " - diff : %d - clock rate : %d - timestamp : %" G_GUINT64_FORMAT,
513 rtpdtmfdepay->previous_duration, dtmf_payload.duration,
514 (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
515 depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
517 /* If late or duplicate packet (like the redundant end packet). Ignore */
518 if (dtmf_payload.duration > 0) {
519 outbuf = gst_buffer_new ();
520 gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload, outbuf);
523 GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
524 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
525 GST_SECOND / depayload->clock_rate;
526 GST_BUFFER_OFFSET (outbuf) =
527 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
528 GST_SECOND / depayload->clock_rate;
529 GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
530 GST_SECOND / depayload->clock_rate;
532 GST_DEBUG_OBJECT (depayload,
533 "timestamp : %" G_GUINT64_FORMAT " - time %" GST_TIME_FORMAT,
534 GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
542 GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
543 ("Packet did not validate"), (NULL));
548 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
550 return gst_element_register (plugin, "rtpdtmfdepay",
551 GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);