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