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.
30 #include <gst/rtp/gstrtpbuffer.h>
31 #include "gstrtpdtmfdepay.h"
34 # define M_PI 3.14159265358979323846 /* pi */
38 #define DEFAULT_PACKET_INTERVAL 50 /* ms */
39 #define MIN_PACKET_INTERVAL 10 /* ms */
40 #define MAX_PACKET_INTERVAL 50 /* ms */
41 #define SAMPLE_RATE 8000
42 #define SAMPLE_SIZE 16
48 #define MIN_INTER_DIGIT_INTERVAL 100
49 #define MIN_PULSE_DURATION 250
50 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
53 typedef struct st_dtmf_key {
60 static const DTMF_KEY DTMF_KEYS[] = {
61 {"DTMF_KEY_EVENT_0", 0, 941, 1336},
62 {"DTMF_KEY_EVENT_1", 1, 697, 1209},
63 {"DTMF_KEY_EVENT_2", 2, 697, 1336},
64 {"DTMF_KEY_EVENT_3", 3, 697, 1477},
65 {"DTMF_KEY_EVENT_4", 4, 770, 1209},
66 {"DTMF_KEY_EVENT_5", 5, 770, 1336},
67 {"DTMF_KEY_EVENT_6", 6, 770, 1477},
68 {"DTMF_KEY_EVENT_7", 7, 852, 1209},
69 {"DTMF_KEY_EVENT_8", 8, 852, 1336},
70 {"DTMF_KEY_EVENT_9", 9, 852, 1477},
71 {"DTMF_KEY_EVENT_S", 10, 941, 1209},
72 {"DTMF_KEY_EVENT_P", 11, 941, 1477},
73 {"DTMF_KEY_EVENT_A", 12, 697, 1633},
74 {"DTMF_KEY_EVENT_B", 13, 770, 1633},
75 {"DTMF_KEY_EVENT_C", 14, 852, 1633},
76 {"DTMF_KEY_EVENT_D", 15, 941, 1633},
79 #define MAX_DTMF_EVENTS 16
92 DTMF_KEY_EVENT_STAR = 10,
93 DTMF_KEY_EVENT_POUND = 11,
94 DTMF_KEY_EVENT_A = 12,
95 DTMF_KEY_EVENT_B = 13,
96 DTMF_KEY_EVENT_C = 14,
97 DTMF_KEY_EVENT_D = 15,
100 /* elementfactory information */
101 static const GstElementDetails gst_rtp_dtmfdepay_details =
102 GST_ELEMENT_DETAILS ("RTP DTMF packet depayloader",
103 "Codec/Depayloader/Network",
104 "Generates DTMF Sound from telephone-event RTP packets",
105 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
107 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
108 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
121 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
122 GST_STATIC_PAD_TEMPLATE ("src",
125 GST_STATIC_CAPS ("audio/x-raw-int, "
128 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
129 "signed = (boolean) true, "
130 "rate = (int) [0, MAX], "
131 "channels = (int) 1")
134 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
135 GST_STATIC_PAD_TEMPLATE ("sink",
138 GST_STATIC_CAPS ("application/x-rtp, "
139 "media = (string) \"audio\", "
140 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
141 "clock-rate = (int) [ 0, MAX ], "
142 "encoding-name = (string) \"TELEPHONE-EVENT\"")
145 GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
146 GST_TYPE_BASE_RTP_DEPAYLOAD);
149 static GstBuffer *gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload,
151 gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
155 gst_rtp_dtmf_depay_base_init (gpointer klass)
157 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
159 gst_element_class_add_pad_template (element_class,
160 gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
161 gst_element_class_add_pad_template (element_class,
162 gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
165 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
166 "rtpdtmfdepay", 0, "rtpdtmfdepay element");
167 gst_element_class_set_details (element_class, &gst_rtp_dtmfdepay_details);
171 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
173 GObjectClass *gobject_class;
174 GstElementClass *gstelement_class;
175 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
177 gobject_class = (GObjectClass *) klass;
178 gstelement_class = (GstElementClass *) klass;
179 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
181 parent_class = g_type_class_peek_parent (klass);
183 gstbasertpdepayload_class->process = gst_rtp_dtmf_depay_process;
184 gstbasertpdepayload_class->set_caps = gst_rtp_dtmf_depay_setcaps;
189 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
190 GstRtpDTMFDepayClass * klass)
197 gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
200 GstStructure *structure = gst_caps_get_structure (caps, 0);
201 gint clock_rate = 8000; /* default */
203 gst_structure_get_int (structure, "clock-rate", &clock_rate);
204 filter->clock_rate = clock_rate;
206 srccaps = gst_caps_new_simple ("audio/x-raw-int",
207 "width", G_TYPE_INT, 16,
208 "depth", G_TYPE_INT, 16,
209 "endianness", G_TYPE_INT, G_BYTE_ORDER,
210 "signed", G_TYPE_BOOLEAN, TRUE,
211 "channels", G_TYPE_INT, 1,
212 "rate", G_TYPE_INT, clock_rate, NULL);
213 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
214 gst_caps_unref (srccaps);
220 gst_dtmf_src_generate_tone(GstRtpDTMFDepay *rtpdtmfdepay,
221 GstRTPDTMFPayload payload, GstBuffer * buffer)
226 double amplitude, f1, f2;
227 double volume_factor;
228 DTMF_KEY key = DTMF_KEYS[payload.event];
229 guint32 clock_rate = 8000 /* default */;
230 GstBaseRTPDepayload * depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
232 clock_rate = depayload->clock_rate;
234 /* Create a buffer for the tone */
235 tone_size = (payload.duration*SAMPLE_SIZE*CHANNELS)/8;
236 GST_BUFFER_SIZE (buffer) = tone_size;
237 GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
238 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
239 GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
241 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
243 volume_factor = pow (10, (-payload.volume) / 20);
246 * For each sample point we calculate 'x' as the
247 * the amplitude value.
249 for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
251 * We add the fundamental frequencies together.
253 f1 = sin(2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample / clock_rate));
254 f2 = sin(2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample / clock_rate));
256 amplitude = (f1 + f2) / 2;
258 /* Adjust the volume */
259 amplitude *= volume_factor;
261 /* Make the [-1:1] interval into a [-32767:32767] interval */
264 /* Store it in the data buffer */
265 *(p++) = (gint16) amplitude;
267 (rtpdtmfdepay->sample)++;
273 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
276 GstRtpDTMFDepay *rtpdtmfdepay = NULL;
277 GstBuffer *outbuf = NULL;
279 guint8 *payload = NULL;
281 GstRTPDTMFPayload dtmf_payload;
283 GstStructure *structure = NULL;
284 GstMessage *dtmf_message = NULL;
286 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
288 if (!gst_rtp_buffer_validate (buf))
291 payload_len = gst_rtp_buffer_get_payload_len (buf);
292 payload = gst_rtp_buffer_get_payload (buf);
294 if (payload_len != sizeof(GstRTPDTMFPayload) )
297 memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
299 if (dtmf_payload.event > MAX_EVENT)
303 marker = gst_rtp_buffer_get_marker (buf);
305 timestamp = gst_rtp_buffer_get_timestamp (buf);
307 dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
309 GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
310 "marker=%d - timestamp=%u - event=%d - duration=%d",
311 marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
313 GST_DEBUG_OBJECT (depayload, "Previous information : timestamp=%u - duration=%d",
314 rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
317 if (marker || rtpdtmfdepay->previous_ts != timestamp) {
318 rtpdtmfdepay->sample = 0;
319 rtpdtmfdepay->previous_ts = timestamp;
320 rtpdtmfdepay->previous_duration = dtmf_payload.duration;
321 rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
323 structure = gst_structure_new ("dtmf-event",
324 "number", G_TYPE_INT, dtmf_payload.event,
325 "volume", G_TYPE_INT, dtmf_payload.volume,
326 "type", G_TYPE_INT, 1,
327 "method", G_TYPE_INT, 1,
330 dtmf_message = gst_message_new_element (GST_OBJECT (depayload), structure);
332 if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
333 GST_ERROR_OBJECT (depayload, "Unable to send dtmf-event message to bus");
336 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
339 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
342 guint16 duration = dtmf_payload.duration;
343 dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
344 /* If late buffer, ignore */
345 if (duration > rtpdtmfdepay->previous_duration)
346 rtpdtmfdepay->previous_duration = duration;
349 GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
350 " - diff : %d - clock rate : %d - timestamp : %llu",
351 rtpdtmfdepay->previous_duration, dtmf_payload.duration,
352 (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
353 depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
355 /* If late or duplicate packet (like the redundant end packet). Ignore */
356 if (dtmf_payload.duration > 0) {
357 outbuf = gst_buffer_new ();
358 gst_dtmf_src_generate_tone(rtpdtmfdepay, dtmf_payload, outbuf);
361 GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
362 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
363 GST_SECOND / depayload->clock_rate;
364 GST_BUFFER_OFFSET (outbuf) =
365 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
366 GST_SECOND / depayload->clock_rate;
367 GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
368 GST_SECOND / depayload->clock_rate;
370 GST_DEBUG_OBJECT (depayload, "timestamp : %llu - time %" GST_TIME_FORMAT,
371 GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
379 GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
380 ("Packet did not validate"), (NULL));
385 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
387 return gst_element_register (plugin, "rtpdtmfdepay",
388 GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);