1 /* GstRtpDtmfDepay
2 *
3 * Copyright (C) 2008 Collabora Limited
4 * Copyright (C) 2008 Nokia Corporation
5 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
6 *
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.
11 *
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.
16 *
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 /**
23 * SECTION:element-rtpdtmfdepay
24 * @title: rtpdtmfdepay
25 * @see_also: rtpdtmfsrc, rtpdtmfmux
26 *
27 * This element takes RTP DTMF packets and produces sound. It also emits a
28 * message on the #GstBus.
29 *
30 * The message is called "dtmf-event" and has the following fields:
31 *
32 * * `type` (G_TYPE_INT, 0-1): Which of the two methods
33 * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
34 * named events. Tones are specified by their frequencies and events are specified
35 * by their number. This element currently only recognizes events.
36 * Do not confuse with "method" which specified the output.
37 *
38 * * `number` (G_TYPE_INT, 0-16): The event number.
39 *
40 * * `volume` (G_TYPE_INT, 0-36): This field describes the power level of the tone, expressed in dBm0
41 * after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
42 * valid DTMF is from 0 to -36 dBm0.
43 *
44 * * `method` (G_TYPE_INT, 1): This field will always been 1 (ie RTP event) from this element.
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include "gstrtpdtmfdepay.h"
52
53 #include <string.h>
54 #include <math.h>
55
56 #include <gst/audio/audio.h>
57 #include <gst/base/gstbitreader.h>
58 #include <gst/rtp/gstrtpbuffer.h>
59
60 #define DEFAULT_PACKET_INTERVAL 50 /* ms */
61 #define MIN_PACKET_INTERVAL 10 /* ms */
62 #define MAX_PACKET_INTERVAL 50 /* ms */
63 #define SAMPLE_RATE 8000
64 #define SAMPLE_SIZE 16
65 #define CHANNELS 1
66 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
67
68 #define MIN_UNIT_TIME 0
69 #define MAX_UNIT_TIME 1000
70 #define DEFAULT_UNIT_TIME 0
71
72 #define DEFAULT_MAX_DURATION 0
73
74 typedef struct st_dtmf_key
75 {
76 float low_frequency;
77 float high_frequency;
78 } DTMF_KEY;
79
80 static const DTMF_KEY DTMF_KEYS[] = {
81 {941, 1336},
82 {697, 1209},
83 {697, 1336},
84 {697, 1477},
85 {770, 1209},
86 {770, 1336},
87 {770, 1477},
88 {852, 1209},
89 {852, 1336},
90 {852, 1477},
91 {941, 1209},
92 {941, 1477},
93 {697, 1633},
94 {770, 1633},
95 {852, 1633},
96 {941, 1633},
97 };
98
99 #define MAX_DTMF_EVENTS 16
100
101 enum
102 {
103 DTMF_KEY_EVENT_1 = 1,
104 DTMF_KEY_EVENT_2 = 2,
105 DTMF_KEY_EVENT_3 = 3,
106 DTMF_KEY_EVENT_4 = 4,
107 DTMF_KEY_EVENT_5 = 5,
108 DTMF_KEY_EVENT_6 = 6,
109 DTMF_KEY_EVENT_7 = 7,
110 DTMF_KEY_EVENT_8 = 8,
111 DTMF_KEY_EVENT_9 = 9,
112 DTMF_KEY_EVENT_0 = 0,
113 DTMF_KEY_EVENT_STAR = 10,
114 DTMF_KEY_EVENT_POUND = 11,
115 DTMF_KEY_EVENT_A = 12,
116 DTMF_KEY_EVENT_B = 13,
117 DTMF_KEY_EVENT_C = 14,
118 DTMF_KEY_EVENT_D = 15,
119 };
120
121 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
122 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
123
124 enum
125 {
126 /* FILL ME */
127 LAST_SIGNAL
128 };
129
130 enum
131 {
132 PROP_0,
133 PROP_UNIT_TIME,
134 PROP_MAX_DURATION
135 };
136
137 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
138 GST_STATIC_PAD_TEMPLATE ("src",
139 GST_PAD_SRC,
140 GST_PAD_ALWAYS,
141 GST_STATIC_CAPS ("audio/x-raw, "
142 "format = (string) \"" GST_AUDIO_NE (S16) "\", "
143 "rate = " GST_AUDIO_RATE_RANGE ", " "channels = (int) 1")
144 );
145
146 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
147 GST_STATIC_PAD_TEMPLATE ("sink",
148 GST_PAD_SINK,
149 GST_PAD_ALWAYS,
150 GST_STATIC_CAPS ("application/x-rtp, "
151 "media = (string) \"audio\", "
152 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
153 "clock-rate = (int) [ 0, MAX ], "
154 "encoding-name = (string) \"TELEPHONE-EVENT\"")
155 );
156
157 G_DEFINE_TYPE (GstRtpDTMFDepay, gst_rtp_dtmf_depay,
158 GST_TYPE_RTP_BASE_DEPAYLOAD);
159 GST_ELEMENT_REGISTER_DEFINE (rtpdtmfdepay, "rtpdtmfdepay", GST_RANK_MARGINAL,
160 GST_TYPE_RTP_DTMF_DEPAY);
161
162 static void gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
163 const GValue * value, GParamSpec * pspec);
164 static void gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
165 GValue * value, GParamSpec * pspec);
166 static GstBuffer *gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload,
167 GstBuffer * buf);
168 gboolean gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter,
169 GstCaps * caps);
170
171 static void
gst_rtp_dtmf_depay_class_init(GstRtpDTMFDepayClass * klass)172 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
173 {
174 GObjectClass *gobject_class;
175 GstElementClass *gstelement_class;
176 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
177
178 gobject_class = G_OBJECT_CLASS (klass);
179 gstelement_class = GST_ELEMENT_CLASS (klass);
180 gstrtpbasedepayload_class = GST_RTP_BASE_DEPAYLOAD_CLASS (klass);
181
182 gst_element_class_add_static_pad_template (gstelement_class,
183 &gst_rtp_dtmf_depay_src_template);
184 gst_element_class_add_static_pad_template (gstelement_class,
185 &gst_rtp_dtmf_depay_sink_template);
186
187 GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
188 "rtpdtmfdepay", 0, "rtpdtmfdepay element");
189 gst_element_class_set_static_metadata (gstelement_class,
190 "RTP DTMF packet depayloader", "Codec/Depayloader/Network",
191 "Generates DTMF Sound from telephone-event RTP packets",
192 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
193
194 gobject_class->set_property =
195 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
196 gobject_class->get_property =
197 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_get_property);
198
199 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
200 g_param_spec_uint ("unit-time", "Duration unittime",
201 "The smallest unit (ms) the duration must be a multiple of (0 disables it)",
202 MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME,
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
206 g_param_spec_uint ("max-duration", "Maximum duration",
207 "The maxumimum duration (ms) of the outgoing soundpacket. "
208 "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210
211 gstrtpbasedepayload_class->process =
212 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
213 gstrtpbasedepayload_class->set_caps =
214 GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
215
216 }
217
218 static void
gst_rtp_dtmf_depay_init(GstRtpDTMFDepay * rtpdtmfdepay)219 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay)
220 {
221 rtpdtmfdepay->unit_time = DEFAULT_UNIT_TIME;
222 }
223
224 static void
gst_rtp_dtmf_depay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)225 gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
226 const GValue * value, GParamSpec * pspec)
227 {
228 GstRtpDTMFDepay *rtpdtmfdepay;
229
230 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
231
232 switch (prop_id) {
233 case PROP_UNIT_TIME:
234 rtpdtmfdepay->unit_time = g_value_get_uint (value);
235 break;
236 case PROP_MAX_DURATION:
237 rtpdtmfdepay->max_duration = g_value_get_uint (value);
238 break;
239 default:
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 break;
242 }
243 }
244
245 static void
gst_rtp_dtmf_depay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)246 gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
247 GValue * value, GParamSpec * pspec)
248 {
249 GstRtpDTMFDepay *rtpdtmfdepay;
250
251 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
252
253 switch (prop_id) {
254 case PROP_UNIT_TIME:
255 g_value_set_uint (value, rtpdtmfdepay->unit_time);
256 break;
257 case PROP_MAX_DURATION:
258 g_value_set_uint (value, rtpdtmfdepay->max_duration);
259 break;
260 default:
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 break;
263 }
264 }
265
266 gboolean
gst_rtp_dtmf_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)267 gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
268 {
269 GstCaps *filtercaps, *srccaps;
270 GstStructure *structure = gst_caps_get_structure (caps, 0);
271 gint clock_rate = 8000; /* default */
272
273 gst_structure_get_int (structure, "clock-rate", &clock_rate);
274 filter->clock_rate = clock_rate;
275
276 filtercaps =
277 gst_pad_get_pad_template_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter));
278
279 filtercaps = gst_caps_make_writable (filtercaps);
280 gst_caps_set_simple (filtercaps, "rate", G_TYPE_INT, clock_rate, NULL);
281
282 srccaps = gst_pad_peer_query_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter),
283 filtercaps);
284 gst_caps_unref (filtercaps);
285
286 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
287 gst_caps_unref (srccaps);
288
289 return TRUE;
290 }
291
292 static GstBuffer *
gst_dtmf_src_generate_tone(GstRtpDTMFDepay * rtpdtmfdepay,GstRTPDTMFPayload payload)293 gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
294 GstRTPDTMFPayload payload)
295 {
296 GstBuffer *buf;
297 GstMapInfo map;
298 gint16 *p;
299 gint tone_size;
300 double i = 0;
301 double amplitude, f1, f2;
302 double volume_factor;
303 DTMF_KEY key = DTMF_KEYS[payload.event];
304 guint32 clock_rate;
305 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtpdtmfdepay);
306 gint volume;
307 static GstAllocationParams params = { 0, 1, 0, 0, };
308
309 clock_rate = depayload->clock_rate;
310
311 /* Create a buffer for the tone */
312 tone_size = (payload.duration * SAMPLE_SIZE * CHANNELS) / 8;
313 buf = gst_buffer_new_allocate (NULL, tone_size, ¶ms);
314 GST_BUFFER_DURATION (buf) = payload.duration * GST_SECOND / clock_rate;
315 volume = payload.volume;
316
317 gst_buffer_map (buf, &map, GST_MAP_WRITE);
318 p = (gint16 *) map.data;
319
320 volume_factor = pow (10, (-volume) / 20);
321
322 /*
323 * For each sample point we calculate 'x' as the
324 * the amplitude value.
325 */
326 for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
327 /*
328 * We add the fundamental frequencies together.
329 */
330 f1 = sin (2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample /
331 clock_rate));
332 f2 = sin (2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample /
333 clock_rate));
334
335 amplitude = (f1 + f2) / 2;
336
337 /* Adjust the volume */
338 amplitude *= volume_factor;
339
340 /* Make the [-1:1] interval into a [-32767:32767] interval */
341 amplitude *= 32767;
342
343 /* Store it in the data buffer */
344 *(p++) = (gint16) amplitude;
345
346 (rtpdtmfdepay->sample)++;
347 }
348
349 gst_buffer_unmap (buf, &map);
350
351 return buf;
352 }
353
354
355 static GstBuffer *
gst_rtp_dtmf_depay_process(GstRTPBaseDepayload * depayload,GstBuffer * buf)356 gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
357 {
358
359 GstRtpDTMFDepay *rtpdtmfdepay = NULL;
360 GstBuffer *outbuf = NULL;
361 guint payload_len;
362 guint8 *payload = NULL;
363 guint32 timestamp;
364 GstRTPDTMFPayload dtmf_payload;
365 gboolean marker;
366 GstStructure *structure = NULL;
367 GstMessage *dtmf_message = NULL;
368 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
369 GstBitReader bitreader;
370
371 rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
372
373 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
374
375 payload_len = gst_rtp_buffer_get_payload_len (&rtpbuffer);
376 payload = gst_rtp_buffer_get_payload (&rtpbuffer);
377
378 if (payload_len != 4)
379 goto bad_packet;
380
381 gst_bit_reader_init (&bitreader, payload, payload_len);
382 gst_bit_reader_get_bits_uint8 (&bitreader, &dtmf_payload.event, 8);
383 gst_bit_reader_skip (&bitreader, 2);
384 gst_bit_reader_get_bits_uint8 (&bitreader, &dtmf_payload.volume, 6);
385 gst_bit_reader_get_bits_uint16 (&bitreader, &dtmf_payload.duration, 16);
386
387 if (dtmf_payload.event > MAX_EVENT)
388 goto bad_packet;
389
390 marker = gst_rtp_buffer_get_marker (&rtpbuffer);
391
392 timestamp = gst_rtp_buffer_get_timestamp (&rtpbuffer);
393
394 /* clip to whole units of unit_time */
395 if (rtpdtmfdepay->unit_time) {
396 guint unit_time_clock =
397 (rtpdtmfdepay->unit_time * depayload->clock_rate) / 1000;
398 if (dtmf_payload.duration % unit_time_clock) {
399 /* Make sure we don't overflow the duration */
400 if (dtmf_payload.duration < G_MAXUINT16 - unit_time_clock)
401 dtmf_payload.duration += unit_time_clock -
402 (dtmf_payload.duration % unit_time_clock);
403 else
404 dtmf_payload.duration -= dtmf_payload.duration % unit_time_clock;
405 }
406 }
407
408 /* clip to max duration */
409 if (rtpdtmfdepay->max_duration) {
410 guint max_duration_clock =
411 (rtpdtmfdepay->max_duration * depayload->clock_rate) / 1000;
412
413 if (max_duration_clock < G_MAXUINT16 &&
414 dtmf_payload.duration > max_duration_clock)
415 dtmf_payload.duration = max_duration_clock;
416 }
417
418 GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
419 "marker=%d - timestamp=%u - event=%d - duration=%d",
420 marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
421
422 GST_DEBUG_OBJECT (depayload,
423 "Previous information : timestamp=%u - duration=%d",
424 rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
425
426 /* First packet */
427 if (marker || rtpdtmfdepay->previous_ts != timestamp) {
428 rtpdtmfdepay->sample = 0;
429 rtpdtmfdepay->previous_ts = timestamp;
430 rtpdtmfdepay->previous_duration = dtmf_payload.duration;
431 rtpdtmfdepay->first_gst_ts = GST_BUFFER_PTS (buf);
432
433 structure = gst_structure_new ("dtmf-event",
434 "number", G_TYPE_INT, dtmf_payload.event,
435 "volume", G_TYPE_INT, dtmf_payload.volume,
436 "type", G_TYPE_INT, 1, "method", G_TYPE_INT, 1, NULL);
437 if (structure) {
438 dtmf_message =
439 gst_message_new_element (GST_OBJECT (depayload), structure);
440 if (dtmf_message) {
441 if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
442 GST_ERROR_OBJECT (depayload,
443 "Unable to send dtmf-event message to bus");
444 }
445 } else {
446 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
447 }
448 } else {
449 GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
450 }
451 } else {
452 guint16 duration = dtmf_payload.duration;
453 dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
454 /* If late buffer, ignore */
455 if (duration > rtpdtmfdepay->previous_duration)
456 rtpdtmfdepay->previous_duration = duration;
457 }
458
459 GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
460 " - diff : %d - clock rate : %d - timestamp : %" G_GUINT64_FORMAT,
461 rtpdtmfdepay->previous_duration, dtmf_payload.duration,
462 (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
463 depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
464
465 /* If late or duplicate packet (like the redundant end packet). Ignore */
466 if (dtmf_payload.duration > 0) {
467 outbuf = gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload);
468
469
470 GST_BUFFER_PTS (outbuf) = rtpdtmfdepay->first_gst_ts +
471 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
472 GST_SECOND / depayload->clock_rate;
473 GST_BUFFER_OFFSET (outbuf) =
474 (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
475 GST_SECOND / depayload->clock_rate;
476 GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
477 GST_SECOND / depayload->clock_rate;
478
479 GST_DEBUG_OBJECT (depayload,
480 "timestamp : %" G_GUINT64_FORMAT " - time %" GST_TIME_FORMAT,
481 GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
482
483 }
484
485 gst_rtp_buffer_unmap (&rtpbuffer);
486
487 return outbuf;
488
489 bad_packet:
490 GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
491 ("Packet did not validate"), (NULL));
492
493 if (rtpbuffer.buffer != NULL)
494 gst_rtp_buffer_unmap (&rtpbuffer);
495
496 return NULL;
497 }
498