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"
93 #define DEFAULT_PACKET_INTERVAL 50 /* ms */
94 #define MIN_PACKET_INTERVAL 10 /* ms */
95 #define MAX_PACKET_INTERVAL 50 /* ms */
96 #define SAMPLE_RATE 8000
97 #define SAMPLE_SIZE 16
99 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
101 #define MIN_UNIT_TIME 0
102 #define MAX_UNIT_TIME 1000
103 #define DEFAULT_UNIT_TIME 0
105 #define DEFAULT_MAX_DURATION 0
107 typedef struct st_dtmf_key
109 const char *event_name;
112 float high_frequency;
115 static const DTMF_KEY DTMF_KEYS[] = {
116 {"DTMF_KEY_EVENT_0", 0, 941, 1336},
117 {"DTMF_KEY_EVENT_1", 1, 697, 1209},
118 {"DTMF_KEY_EVENT_2", 2, 697, 1336},
119 {"DTMF_KEY_EVENT_3", 3, 697, 1477},
120 {"DTMF_KEY_EVENT_4", 4, 770, 1209},
121 {"DTMF_KEY_EVENT_5", 5, 770, 1336},
122 {"DTMF_KEY_EVENT_6", 6, 770, 1477},
123 {"DTMF_KEY_EVENT_7", 7, 852, 1209},
124 {"DTMF_KEY_EVENT_8", 8, 852, 1336},
125 {"DTMF_KEY_EVENT_9", 9, 852, 1477},
126 {"DTMF_KEY_EVENT_S", 10, 941, 1209},
127 {"DTMF_KEY_EVENT_P", 11, 941, 1477},
128 {"DTMF_KEY_EVENT_A", 12, 697, 1633},
129 {"DTMF_KEY_EVENT_B", 13, 770, 1633},
130 {"DTMF_KEY_EVENT_C", 14, 852, 1633},
131 {"DTMF_KEY_EVENT_D", 15, 941, 1633},
134 #define MAX_DTMF_EVENTS 16
138 DTMF_KEY_EVENT_1 = 1,
139 DTMF_KEY_EVENT_2 = 2,
140 DTMF_KEY_EVENT_3 = 3,
141 DTMF_KEY_EVENT_4 = 4,
142 DTMF_KEY_EVENT_5 = 5,
143 DTMF_KEY_EVENT_6 = 6,
144 DTMF_KEY_EVENT_7 = 7,
145 DTMF_KEY_EVENT_8 = 8,
146 DTMF_KEY_EVENT_9 = 9,
147 DTMF_KEY_EVENT_0 = 0,
148 DTMF_KEY_EVENT_STAR = 10,
149 DTMF_KEY_EVENT_POUND = 11,
150 DTMF_KEY_EVENT_A = 12,
151 DTMF_KEY_EVENT_B = 13,
152 DTMF_KEY_EVENT_C = 14,
153 DTMF_KEY_EVENT_D = 15,
156 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
157 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
179 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
180 GST_STATIC_PAD_TEMPLATE ("src",
183 GST_STATIC_CAPS ("audio/x-raw-int, "
186 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
187 "signed = (boolean) true, "
188 "rate = (int) [0, MAX], " "channels = (int) 1")
191 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
192 GST_STATIC_PAD_TEMPLATE ("sink",
195 GST_STATIC_CAPS ("application/x-rtp, "
196 "media = (string) \"audio\", "
197 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
198 "clock-rate = (int) [ 0, MAX ], "
199 "encoding-name = (string) \"TELEPHONE-EVENT\"")
202 GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
203 GST_TYPE_BASE_RTP_DEPAYLOAD);
205 static void gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec);
207 static void gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
208 GValue * value, GParamSpec * pspec);
209 static GstBuffer *gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload,
211 gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
215 gst_rtp_dtmf_depay_base_init (gpointer klass)
217 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
219 gst_element_class_add_pad_template (element_class,
220 gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
221 gst_element_class_add_pad_template (element_class,
222 gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
225 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
226 "rtpdtmfdepay", 0, "rtpdtmfdepay element");
227 gst_element_class_set_details_simple (element_class,
228 "RTP DTMF packet depayloader", "Codec/Depayloader/Network",
229 "Generates DTMF Sound from telephone-event RTP packets",
230 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
234 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
236 GObjectClass *gobject_class;
237 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
239 gobject_class = (GObjectClass *) klass;
240 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
242 parent_class = g_type_class_peek_parent (klass);
244 gobject_class->set_property =
245 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
246 gobject_class->get_property =
247 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_get_property);
249 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
250 g_param_spec_uint ("unit-time", "Duration unittime",
251 "The smallest unit (ms) the duration must be a multiple of (0 disables it)",
252 MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME,
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
256 g_param_spec_uint ("max-duration", "Maximum duration",
257 "The maxumimum duration (ms) of the outgoing soundpacket. "
258 "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 gstbasertpdepayload_class->process =
262 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
263 gstbasertpdepayload_class->set_caps =
264 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
269 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
270 GstRtpDTMFDepayClass * klass)
272 rtpdtmfdepay->unit_time = DEFAULT_UNIT_TIME;
276 gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec)
279 GstRtpDTMFDepay *rtpdtmfdepay;
281 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
285 rtpdtmfdepay->unit_time = g_value_get_uint (value);
287 case PROP_MAX_DURATION:
288 rtpdtmfdepay->max_duration = g_value_get_uint (value);
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
298 GValue * value, GParamSpec * pspec)
300 GstRtpDTMFDepay *rtpdtmfdepay;
302 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
306 g_value_set_uint (value, rtpdtmfdepay->unit_time);
308 case PROP_MAX_DURATION:
309 g_value_set_uint (value, rtpdtmfdepay->max_duration);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
321 GstStructure *structure = gst_caps_get_structure (caps, 0);
322 gint clock_rate = 8000; /* default */
324 gst_structure_get_int (structure, "clock-rate", &clock_rate);
325 filter->clock_rate = clock_rate;
327 srccaps = gst_caps_new_simple ("audio/x-raw-int",
328 "width", G_TYPE_INT, 16,
329 "depth", G_TYPE_INT, 16,
330 "endianness", G_TYPE_INT, G_BYTE_ORDER,
331 "signed", G_TYPE_BOOLEAN, TRUE,
332 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
333 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
334 gst_caps_unref (srccaps);
340 gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
341 GstRTPDTMFPayload payload, GstBuffer * buffer)
346 double amplitude, f1, f2;
347 double volume_factor;
348 DTMF_KEY key = DTMF_KEYS[payload.event];
349 guint32 clock_rate = 8000 /* default */ ;
350 GstBaseRTPDepayload *depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
353 clock_rate = depayload->clock_rate;
355 /* Create a buffer for the tone */
356 tone_size = (payload.duration * SAMPLE_SIZE * CHANNELS) / 8;
357 GST_BUFFER_SIZE (buffer) = tone_size;
358 GST_BUFFER_MALLOCDATA (buffer) = g_malloc (tone_size);
359 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
360 GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
361 volume = payload.volume;
363 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
365 volume_factor = pow (10, (-volume) / 20);
368 * For each sample point we calculate 'x' as the
369 * the amplitude value.
371 for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
373 * We add the fundamental frequencies together.
375 f1 = sin (2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample /
377 f2 = sin (2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample /
380 amplitude = (f1 + f2) / 2;
382 /* Adjust the volume */
383 amplitude *= volume_factor;
385 /* Make the [-1:1] interval into a [-32767:32767] interval */
388 /* Store it in the data buffer */
389 *(p++) = (gint16) amplitude;
391 (rtpdtmfdepay->sample)++;
397 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
400 GstRtpDTMFDepay *rtpdtmfdepay = NULL;
401 GstBuffer *outbuf = NULL;
403 guint8 *payload = NULL;
405 GstRTPDTMFPayload dtmf_payload;
407 GstStructure *structure = NULL;
408 GstMessage *dtmf_message = NULL;
410 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
412 if (!gst_rtp_buffer_validate (buf))
415 payload_len = gst_rtp_buffer_get_payload_len (buf);
416 payload = gst_rtp_buffer_get_payload (buf);
418 if (payload_len != sizeof (GstRTPDTMFPayload))
421 memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
423 if (dtmf_payload.event > MAX_EVENT)
427 marker = gst_rtp_buffer_get_marker (buf);
429 timestamp = gst_rtp_buffer_get_timestamp (buf);
431 dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
433 /* clip to whole units of unit_time */
434 if (rtpdtmfdepay->unit_time) {
435 guint unit_time_clock =
436 (rtpdtmfdepay->unit_time * depayload->clock_rate) / 1000;
437 if (dtmf_payload.duration % unit_time_clock) {
438 /* Make sure we don't overflow the duration */
439 if (dtmf_payload.duration < G_MAXUINT16 - unit_time_clock)
440 dtmf_payload.duration += unit_time_clock -
441 (dtmf_payload.duration % unit_time_clock);
443 dtmf_payload.duration -= dtmf_payload.duration % unit_time_clock;
447 /* clip to max duration */
448 if (rtpdtmfdepay->max_duration) {
449 guint max_duration_clock =
450 (rtpdtmfdepay->max_duration * depayload->clock_rate) / 1000;
452 if (max_duration_clock < G_MAXUINT16 &&
453 dtmf_payload.duration > max_duration_clock)
454 dtmf_payload.duration = max_duration_clock;
457 GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
458 "marker=%d - timestamp=%u - event=%d - duration=%d",
459 marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
461 GST_DEBUG_OBJECT (depayload,
462 "Previous information : timestamp=%u - duration=%d",
463 rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
466 if (marker || rtpdtmfdepay->previous_ts != timestamp) {
467 rtpdtmfdepay->sample = 0;
468 rtpdtmfdepay->previous_ts = timestamp;
469 rtpdtmfdepay->previous_duration = dtmf_payload.duration;
470 rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
472 structure = gst_structure_new ("dtmf-event",
473 "number", G_TYPE_INT, dtmf_payload.event,
474 "volume", G_TYPE_INT, dtmf_payload.volume,
475 "type", G_TYPE_INT, 1, "method", G_TYPE_INT, 1, NULL);
478 gst_message_new_element (GST_OBJECT (depayload), structure);
480 if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
481 GST_ERROR_OBJECT (depayload,
482 "Unable to send dtmf-event message to bus");
485 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
488 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
491 guint16 duration = dtmf_payload.duration;
492 dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
493 /* If late buffer, ignore */
494 if (duration > rtpdtmfdepay->previous_duration)
495 rtpdtmfdepay->previous_duration = duration;
498 GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
499 " - diff : %d - clock rate : %d - timestamp : %" G_GUINT64_FORMAT,
500 rtpdtmfdepay->previous_duration, dtmf_payload.duration,
501 (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
502 depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
504 /* If late or duplicate packet (like the redundant end packet). Ignore */
505 if (dtmf_payload.duration > 0) {
506 outbuf = gst_buffer_new ();
507 gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload, outbuf);
510 GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
511 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
512 GST_SECOND / depayload->clock_rate;
513 GST_BUFFER_OFFSET (outbuf) =
514 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
515 GST_SECOND / depayload->clock_rate;
516 GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
517 GST_SECOND / depayload->clock_rate;
519 GST_DEBUG_OBJECT (depayload,
520 "timestamp : %" G_GUINT64_FORMAT " - time %" GST_TIME_FORMAT,
521 GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
529 GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
530 ("Packet did not validate"), (NULL));
535 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
537 return gst_element_register (plugin, "rtpdtmfdepay",
538 GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);