3 * Copyright (C) <2008> Collabora.
4 * Copyright (C) <2008> Nokia.
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.
30 #include <gst/rtp/gstrtpbuffer.h>
31 #include "gstrtpdtmfdepay.h"
34 # define M_PI 3.14159265358979323846 /* pi */
38 #define GST_TONE_DTMF_TYPE_EVENT 0
39 #define DEFAULT_PACKET_INTERVAL 50 /* ms */
40 #define MIN_PACKET_INTERVAL 10 /* ms */
41 #define MAX_PACKET_INTERVAL 50 /* ms */
42 #define SAMPLE_RATE 8000
43 #define SAMPLE_SIZE 16
49 #define MIN_INTER_DIGIT_INTERVAL 100
50 #define MIN_PULSE_DURATION 250
51 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
54 typedef struct st_dtmf_key {
61 static const DTMF_KEY DTMF_KEYS[] = {
62 {"DTMF_KEY_EVENT_0", 0, 941, 1336},
63 {"DTMF_KEY_EVENT_1", 1, 697, 1209},
64 {"DTMF_KEY_EVENT_2", 2, 697, 1336},
65 {"DTMF_KEY_EVENT_3", 3, 697, 1477},
66 {"DTMF_KEY_EVENT_4", 4, 770, 1209},
67 {"DTMF_KEY_EVENT_5", 5, 770, 1336},
68 {"DTMF_KEY_EVENT_6", 6, 770, 1477},
69 {"DTMF_KEY_EVENT_7", 7, 852, 1209},
70 {"DTMF_KEY_EVENT_8", 8, 852, 1336},
71 {"DTMF_KEY_EVENT_9", 9, 852, 1477},
72 {"DTMF_KEY_EVENT_S", 10, 941, 1209},
73 {"DTMF_KEY_EVENT_P", 11, 941, 1477},
74 {"DTMF_KEY_EVENT_A", 12, 697, 1633},
75 {"DTMF_KEY_EVENT_B", 13, 770, 1633},
76 {"DTMF_KEY_EVENT_C", 14, 852, 1633},
77 {"DTMF_KEY_EVENT_D", 15, 941, 1633},
80 #define MAX_DTMF_EVENTS 16
93 DTMF_KEY_EVENT_STAR = 10,
94 DTMF_KEY_EVENT_POUND = 11,
95 DTMF_KEY_EVENT_A = 12,
96 DTMF_KEY_EVENT_B = 13,
97 DTMF_KEY_EVENT_C = 14,
98 DTMF_KEY_EVENT_D = 15,
101 /* elementfactory information */
102 static const GstElementDetails gst_rtp_dtmfdepay_details =
103 GST_ELEMENT_DETAILS ("RTP DTMF packet depayloader",
104 "Codec/Depayloader/Network",
105 "Generates DTMF Sound from telephone-event RTP packets",
106 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
108 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
109 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
122 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
126 GST_STATIC_CAPS ("audio/x-raw-int, "
129 "endianness = (int) 1234, "
130 "signed = (boolean) true, "
131 "rate = (int) [0, MAX], "
132 "channels = (int) 1")
135 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
136 GST_STATIC_PAD_TEMPLATE ("sink",
139 GST_STATIC_CAPS ("application/x-rtp, "
140 "media = (string) \"audio\", "
141 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
142 "clock-rate = (int) [ 0, MAX ], "
143 "encoding-name = (string) \"TELEPHONE-EVENT\"")
146 GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
147 GST_TYPE_BASE_RTP_DEPAYLOAD);
150 static GstBuffer *gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload,
152 gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
156 gst_rtp_dtmf_depay_set_gst_timestamp (GstBaseRTPDepayload * filter,
157 guint32 rtptime, GstBuffer * buf);
161 gst_rtp_dtmf_depay_base_init (gpointer klass)
163 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165 gst_element_class_add_pad_template (element_class,
166 gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
167 gst_element_class_add_pad_template (element_class,
168 gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
171 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
172 "rtpdtmfdepay", 0, "rtpdtmfdepay element");
173 gst_element_class_set_details (element_class, &gst_rtp_dtmfdepay_details);
177 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
179 GObjectClass *gobject_class;
180 GstElementClass *gstelement_class;
181 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
183 gobject_class = (GObjectClass *) klass;
184 gstelement_class = (GstElementClass *) klass;
185 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
187 parent_class = g_type_class_peek_parent (klass);
189 gstbasertpdepayload_class->process = gst_rtp_dtmf_depay_process;
190 gstbasertpdepayload_class->set_caps = gst_rtp_dtmf_depay_setcaps;
191 // gstbasertpdepayload_class->set_gst_timestamp = gst_rtp_dtmf_depay_set_gst_timestamp;
196 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
197 GstRtpDTMFDepayClass * klass)
204 gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
207 GstStructure *structure = gst_caps_get_structure (caps, 0);
208 gint clock_rate = 8000; /* default */
210 gst_structure_get_int (structure, "clock-rate", &clock_rate);
211 filter->clock_rate = clock_rate;
213 srccaps = gst_caps_new_simple ("audio/x-raw-int",
214 "width", G_TYPE_INT, 16,
215 "depth", G_TYPE_INT, 16,
216 "endianness", G_TYPE_INT, 1234,
217 "signed", G_TYPE_BOOLEAN, TRUE,
218 "channels", G_TYPE_INT, 1,
219 "rate", G_TYPE_INT, clock_rate, NULL);
220 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
221 gst_caps_unref (srccaps);
227 gst_rtp_dtmf_depay_set_gst_timestamp (GstBaseRTPDepayload * filter,
228 guint32 rtptime, GstBuffer * buf)
230 GstClockTime timestamp, duration;
233 timestamp = GST_BUFFER_TIMESTAMP (buf);
234 duration = GST_BUFFER_DURATION (buf);
236 /* if this is the first buffer send a NEWSEGMENT */
237 if (filter->need_newsegment) {
239 GstClockTime stop, position;
246 gst_event_new_new_segment_full (FALSE, 1.0,
247 1.0, GST_FORMAT_TIME, 0, stop, position);
249 gst_pad_push_event (filter->srcpad, event);
251 filter->need_newsegment = FALSE;
252 GST_DEBUG_OBJECT (filter, "Pushed newsegment event on this first buffer");
258 gst_dtmf_src_generate_silence(GstBuffer * buffer, float duration)
262 /* Create a buffer with data set to 0 */
263 buf_size = ((duration/1000)*SAMPLE_RATE*SAMPLE_SIZE*CHANNELS)/8;
264 GST_BUFFER_SIZE (buffer) = buf_size;
265 GST_BUFFER_MALLOCDATA (buffer) = g_malloc0(buf_size);
266 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
272 gst_dtmf_src_generate_tone(GstRtpDTMFDepay *rtpdtmfdepay,
273 GstRTPDTMFPayload payload, GstBuffer * buffer)
278 double amplitude, f1, f2;
279 double volume_factor;
280 DTMF_KEY key = DTMF_KEYS[payload.event];
281 guint32 clock_rate = 8000 /* default */;
282 GstBaseRTPDepayload * depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
284 clock_rate = depayload->clock_rate;
286 /* Create a buffer for the tone */
287 tone_size = (payload.duration*SAMPLE_SIZE*CHANNELS)/8;
288 GST_BUFFER_SIZE (buffer) = tone_size;
289 GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
290 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
291 GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
293 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
295 volume_factor = pow (10, (-payload.volume) / 20);
298 * For each sample point we calculate 'x' as the
299 * the amplitude value.
301 for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
303 * We add the fundamental frequencies together.
305 f1 = sin(2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample / clock_rate));
306 f2 = sin(2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample / clock_rate));
308 amplitude = (f1 + f2) / 2;
310 /* Adjust the volume */
311 amplitude *= volume_factor;
313 /* Make the [-1:1] interval into a [-32767:32767] interval */
316 /* Store it in the data buffer */
317 *(p++) = (gint16) amplitude;
319 (rtpdtmfdepay->sample)++;
325 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
328 GstRtpDTMFDepay *rtpdtmfdepay = NULL;
329 GstBuffer *outbuf = NULL;
331 guint8 *payload = NULL;
333 GstRTPDTMFPayload dtmf_payload;
335 GstStructure *structure = NULL;
336 GstMessage *dtmf_message = NULL;
338 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
340 if (!gst_rtp_buffer_validate (buf))
343 payload_len = gst_rtp_buffer_get_payload_len (buf);
344 payload = gst_rtp_buffer_get_payload (buf);
346 if (payload_len != sizeof(GstRTPDTMFPayload) )
349 memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
351 if (dtmf_payload.event > MAX_EVENT)
355 marker = gst_rtp_buffer_get_marker (buf);
357 timestamp = gst_rtp_buffer_get_timestamp (buf);
359 dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
361 GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
362 "marker=%d - timestamp=%u - event=%d - duration=%d",
363 marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
365 GST_DEBUG_OBJECT (depayload, "Previous information : timestamp=%u - duration=%d",
366 rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
369 if (marker || rtpdtmfdepay->previous_ts != timestamp) {
370 rtpdtmfdepay->sample = 0;
371 rtpdtmfdepay->previous_ts = timestamp;
372 rtpdtmfdepay->previous_duration = dtmf_payload.duration;
373 rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
375 structure = gst_structure_new ("dtmf-event",
376 "number", G_TYPE_INT, dtmf_payload.event,
377 "volume", G_TYPE_INT, dtmf_payload.volume,
378 "type", G_TYPE_INT, 1,
379 "method", G_TYPE_INT, 1,
382 dtmf_message = gst_message_new_element (GST_OBJECT (depayload), structure);
384 if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
385 GST_DEBUG_OBJECT (depayload, "Unable to send dtmf-event message to bus");
388 GST_DEBUG_OBJECT (depayload, "Unable to create dtmf-event message");
391 GST_DEBUG_OBJECT (depayload, "Unable to create dtmf-event structure");
394 guint16 duration = dtmf_payload.duration;
395 dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
396 /* If late buffer, ignore */
397 if (duration > rtpdtmfdepay->previous_duration)
398 rtpdtmfdepay->previous_duration = duration;
401 GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
402 " - diff : %d - clock rate : %d - timestamp : %llu",
403 rtpdtmfdepay->previous_duration, dtmf_payload.duration,
404 (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
405 depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
407 /* If late or duplicate packet (like the redundant end packet). Ignore */
408 if (dtmf_payload.duration > 0) {
409 outbuf = gst_buffer_new ();
410 gst_dtmf_src_generate_tone(rtpdtmfdepay, dtmf_payload, outbuf);
413 GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
414 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
415 GST_SECOND / depayload->clock_rate;
416 GST_BUFFER_OFFSET (outbuf) =
417 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
418 GST_SECOND / depayload->clock_rate;
419 GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
420 GST_SECOND / depayload->clock_rate;
422 GST_DEBUG_OBJECT (depayload, "timestamp : %llu - time %" GST_TIME_FORMAT,
423 GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
431 GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
432 ("Packet did not validate"), (NULL));
437 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
439 return gst_element_register (plugin, "rtpdtmfdepay",
440 GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);