[MOVED FROM GST-P-FARSIGHT] Allow setting a maximum duration to a RTP DTMF event
[platform/upstream/gstreamer.git] / gst / dtmf / gstrtpdtmfdepay.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <math.h>
29
30 #include <gst/rtp/gstrtpbuffer.h>
31 #include "gstrtpdtmfdepay.h"
32
33 #ifndef M_PI
34 # define M_PI           3.14159265358979323846  /* pi */
35 #endif
36
37
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
43 #define CHANNELS                 1
44 #define MIN_EVENT                0
45 #define MAX_EVENT                16
46 #define MIN_VOLUME               0
47 #define MAX_VOLUME               36
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)
51
52 #define MIN_UNIT_TIME            0
53 #define MAX_UNIT_TIME            1000
54 #define DEFAULT_UNIT_TIME        0
55
56 #define DEFAULT_MAX_DURATION     0
57
58 typedef struct st_dtmf_key {
59         char *event_name;
60         int event_encoding;
61         float low_frequency;
62         float high_frequency;
63 } DTMF_KEY;
64
65 static const DTMF_KEY DTMF_KEYS[] = {
66         {"DTMF_KEY_EVENT_0",  0, 941, 1336},
67         {"DTMF_KEY_EVENT_1",  1, 697, 1209},
68         {"DTMF_KEY_EVENT_2",  2, 697, 1336},
69         {"DTMF_KEY_EVENT_3",  3, 697, 1477},
70         {"DTMF_KEY_EVENT_4",  4, 770, 1209},
71         {"DTMF_KEY_EVENT_5",  5, 770, 1336},
72         {"DTMF_KEY_EVENT_6",  6, 770, 1477},
73         {"DTMF_KEY_EVENT_7",  7, 852, 1209},
74         {"DTMF_KEY_EVENT_8",  8, 852, 1336},
75         {"DTMF_KEY_EVENT_9",  9, 852, 1477},
76         {"DTMF_KEY_EVENT_S", 10, 941, 1209},
77         {"DTMF_KEY_EVENT_P", 11, 941, 1477},
78         {"DTMF_KEY_EVENT_A", 12, 697, 1633},
79         {"DTMF_KEY_EVENT_B", 13, 770, 1633},
80         {"DTMF_KEY_EVENT_C", 14, 852, 1633},
81         {"DTMF_KEY_EVENT_D", 15, 941, 1633},
82 };
83
84 #define MAX_DTMF_EVENTS 16
85
86 enum {
87 DTMF_KEY_EVENT_1 = 1,
88 DTMF_KEY_EVENT_2 = 2,
89 DTMF_KEY_EVENT_3 = 3,
90 DTMF_KEY_EVENT_4 = 4,
91 DTMF_KEY_EVENT_5 = 5,
92 DTMF_KEY_EVENT_6 = 6,
93 DTMF_KEY_EVENT_7 = 7,
94 DTMF_KEY_EVENT_8 = 8,
95 DTMF_KEY_EVENT_9 = 9,
96 DTMF_KEY_EVENT_0 = 0,
97 DTMF_KEY_EVENT_STAR = 10,
98 DTMF_KEY_EVENT_POUND = 11,
99 DTMF_KEY_EVENT_A = 12,
100 DTMF_KEY_EVENT_B = 13,
101 DTMF_KEY_EVENT_C = 14,
102 DTMF_KEY_EVENT_D = 15,
103 };
104
105 /* elementfactory information */
106 static const GstElementDetails gst_rtp_dtmfdepay_details =
107 GST_ELEMENT_DETAILS ("RTP DTMF packet depayloader",
108     "Codec/Depayloader/Network",
109     "Generates DTMF Sound from telephone-event RTP packets",
110     "Youness Alaoui <youness.alaoui@collabora.co.uk>");
111
112 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
113 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
114
115 enum
116 {
117
118
119   /* FILL ME */
120   LAST_SIGNAL
121 };
122
123 enum
124 {
125   PROP_0,
126   PROP_UNIT_TIME,
127   PROP_MAX_DURATION
128 };
129
130 enum
131 {
132   ARG_0
133 };
134
135 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
136 GST_STATIC_PAD_TEMPLATE ("src",
137     GST_PAD_SRC,
138     GST_PAD_ALWAYS,
139     GST_STATIC_CAPS ("audio/x-raw-int, "
140         "width = (int) 16, "
141         "depth = (int) 16, "
142         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
143         "signed = (boolean) true, "
144         "rate = (int) [0, MAX], "
145         "channels = (int) 1")
146     );
147
148 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
149 GST_STATIC_PAD_TEMPLATE ("sink",
150     GST_PAD_SINK,
151     GST_PAD_ALWAYS,
152     GST_STATIC_CAPS ("application/x-rtp, "
153         "media = (string) \"audio\", "
154         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
155         "clock-rate = (int) [ 0, MAX ], "
156         "encoding-name = (string) \"TELEPHONE-EVENT\"")
157     );
158
159 GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
160     GST_TYPE_BASE_RTP_DEPAYLOAD);
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 (GstBaseRTPDepayload * depayload,
167     GstBuffer * buf);
168 gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
169     GstCaps * caps);
170
171 static void
172 gst_rtp_dtmf_depay_base_init (gpointer klass)
173 {
174   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
175
176   gst_element_class_add_pad_template (element_class,
177       gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
178   gst_element_class_add_pad_template (element_class,
179       gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
180
181
182   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
183           "rtpdtmfdepay", 0, "rtpdtmfdepay element");
184   gst_element_class_set_details (element_class, &gst_rtp_dtmfdepay_details);
185 }
186
187 static void
188 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
189 {
190   GObjectClass *gobject_class;
191   GstElementClass *gstelement_class;
192   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
193
194   gobject_class = (GObjectClass *) klass;
195   gstelement_class = (GstElementClass *) klass;
196   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
197
198   parent_class = g_type_class_peek_parent (klass);
199
200   gobject_class->set_property =
201       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
202   gobject_class->get_property =
203       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_get_property);
204
205   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
206     g_param_spec_uint ("unit-time", "Duration unittime",
207         "The smallest unit (ms) the duration must be a multiple of (0 disables it)", MIN_UNIT_TIME,
208         MAX_UNIT_TIME, DEFAULT_UNIT_TIME, G_PARAM_READWRITE));
209
210   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
211     g_param_spec_uint ("max-duration", "Maximum duration",
212         "The maxumimum duration (ms) of the outgoing soundpacket. "
213         "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
214         G_PARAM_READWRITE));
215
216   gstbasertpdepayload_class->process =
217     GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
218   gstbasertpdepayload_class->set_caps =
219     GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
220
221 }
222
223 static void
224 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
225     GstRtpDTMFDepayClass * klass)
226 {
227   rtpdtmfdepay->unit_time = DEFAULT_UNIT_TIME;
228 }
229
230 static void
231 gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
232     const GValue * value, GParamSpec * pspec)
233 {
234   GstRtpDTMFDepay * rtpdtmfdepay;
235
236   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
237
238   switch (prop_id) {
239     case PROP_UNIT_TIME:
240       rtpdtmfdepay->unit_time = g_value_get_uint (value);
241       break;
242     case PROP_MAX_DURATION:
243       rtpdtmfdepay->max_duration = g_value_get_uint (value);
244       break;
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248   }
249 }
250
251 static void
252 gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
253     GValue * value, GParamSpec * pspec)
254 {
255   GstRtpDTMFDepay * rtpdtmfdepay;
256
257   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
258
259   switch (prop_id) {
260     case PROP_UNIT_TIME:
261       g_value_set_uint (value, rtpdtmfdepay->unit_time);
262       break;
263     case PROP_MAX_DURATION:
264       g_value_set_uint (value, rtpdtmfdepay->max_duration);
265       break;
266     default:
267       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268       break;
269   }
270 }
271
272 gboolean
273 gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
274 {
275   GstCaps *srccaps;
276   GstStructure *structure = gst_caps_get_structure (caps, 0);
277   gint clock_rate = 8000;      /* default */
278
279   gst_structure_get_int (structure, "clock-rate", &clock_rate);
280   filter->clock_rate = clock_rate;
281
282   srccaps = gst_caps_new_simple ("audio/x-raw-int",
283       "width", G_TYPE_INT, 16,
284       "depth", G_TYPE_INT, 16,
285       "endianness", G_TYPE_INT, G_BYTE_ORDER,
286       "signed", G_TYPE_BOOLEAN, TRUE,
287       "channels", G_TYPE_INT, 1,
288       "rate", G_TYPE_INT, clock_rate, NULL);
289   gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
290   gst_caps_unref (srccaps);
291
292   return TRUE;
293 }
294
295 static void
296 gst_dtmf_src_generate_tone(GstRtpDTMFDepay *rtpdtmfdepay,
297     GstRTPDTMFPayload payload, GstBuffer * buffer)
298 {
299   gint16 *p;
300   gint tone_size;
301   double i = 0;
302   double amplitude, f1, f2;
303   double volume_factor;
304   DTMF_KEY key = DTMF_KEYS[payload.event];
305   guint32 clock_rate = 8000 /* default */;
306   GstBaseRTPDepayload * depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
307   gint volume;
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   GST_BUFFER_SIZE (buffer) = tone_size;
314   GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
315   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
316   GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
317   volume = payload.volume;
318
319   p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
320
321   volume_factor = pow (10, (-volume) / 20);
322
323   /*
324    * For each sample point we calculate 'x' as the
325    * the amplitude value.
326    */
327   for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
328     /*
329      * We add the fundamental frequencies together.
330      */
331     f1 = sin(2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample / clock_rate));
332     f2 = sin(2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample / clock_rate));
333
334     amplitude = (f1 + f2) / 2;
335
336     /* Adjust the volume */
337     amplitude *= volume_factor;
338
339     /* Make the [-1:1] interval into a [-32767:32767] interval */
340     amplitude *= 32767;
341
342     /* Store it in the data buffer */
343     *(p++) = (gint16) amplitude;
344
345     (rtpdtmfdepay->sample)++;
346   }
347 }
348
349
350 static GstBuffer *
351 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
352 {
353
354   GstRtpDTMFDepay *rtpdtmfdepay = NULL;
355   GstBuffer *outbuf = NULL;
356   gint payload_len;
357   guint8 *payload = NULL;
358   guint32 timestamp;
359   GstRTPDTMFPayload dtmf_payload;
360   gboolean marker;
361   GstStructure *structure = NULL;
362   GstMessage *dtmf_message = NULL;
363
364   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
365
366   if (!gst_rtp_buffer_validate (buf))
367     goto bad_packet;
368
369   payload_len = gst_rtp_buffer_get_payload_len (buf);
370   payload = gst_rtp_buffer_get_payload (buf);
371
372   if (payload_len != sizeof(GstRTPDTMFPayload) )
373     goto bad_packet;
374
375   memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
376
377   if (dtmf_payload.event > MAX_EVENT)
378     goto bad_packet;
379
380
381   marker = gst_rtp_buffer_get_marker (buf);
382
383   timestamp = gst_rtp_buffer_get_timestamp (buf);
384
385   dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
386
387   /* clip to whole units of unit_time */
388   if (rtpdtmfdepay->unit_time)
389   {
390     guint unit_time_clock =
391         (rtpdtmfdepay->unit_time * depayload->clock_rate) / 1000;
392     if (dtmf_payload.duration % unit_time_clock)
393     {
394       /* Make sure we don't overflow the duration */
395       if (dtmf_payload.duration < G_MAXUINT16 - unit_time_clock)
396         dtmf_payload.duration += unit_time_clock -
397             (dtmf_payload.duration % unit_time_clock);
398       else
399         dtmf_payload.duration -= dtmf_payload.duration % unit_time_clock;
400     }
401   }
402
403   /* clip to max duration */
404   if (rtpdtmfdepay->max_duration)
405   {
406     guint max_duration_clock =
407         (rtpdtmfdepay->max_duration * depayload->clock_rate) / 1000;
408
409     if (max_duration_clock < G_MAXUINT16 &&
410         dtmf_payload.duration > max_duration_clock)
411       dtmf_payload.duration = max_duration_clock;
412   }
413
414   GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
415       "marker=%d - timestamp=%u - event=%d - duration=%d",
416       marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
417
418   GST_DEBUG_OBJECT (depayload, "Previous information : timestamp=%u - duration=%d",
419       rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
420
421   /* First packet */
422   if (marker || rtpdtmfdepay->previous_ts != timestamp) {
423     rtpdtmfdepay->sample = 0;
424     rtpdtmfdepay->previous_ts = timestamp;
425     rtpdtmfdepay->previous_duration = dtmf_payload.duration;
426     rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
427
428     structure = gst_structure_new ("dtmf-event",
429         "number", G_TYPE_INT, dtmf_payload.event,
430         "volume", G_TYPE_INT, dtmf_payload.volume,
431         "type", G_TYPE_INT, 1,
432         "method", G_TYPE_INT, 1,
433         NULL);
434     if (structure) {
435       dtmf_message = gst_message_new_element (GST_OBJECT (depayload), structure);
436       if (dtmf_message) {
437         if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
438           GST_ERROR_OBJECT (depayload, "Unable to send dtmf-event message to bus");
439         }
440       } else {
441         GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
442       }
443     } else {
444       GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
445     }
446   } else {
447     guint16 duration = dtmf_payload.duration;
448     dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
449     /* If late buffer, ignore */
450     if (duration > rtpdtmfdepay->previous_duration)
451       rtpdtmfdepay->previous_duration = duration;
452   }
453
454   GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
455       " - diff  : %d - clock rate : %d - timestamp : %llu",
456       rtpdtmfdepay->previous_duration, dtmf_payload.duration,
457       (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
458       depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
459
460   /* If late or duplicate packet (like the redundant end packet). Ignore */
461   if (dtmf_payload.duration > 0) {
462     outbuf = gst_buffer_new ();
463     gst_dtmf_src_generate_tone(rtpdtmfdepay, dtmf_payload, outbuf);
464
465
466     GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
467         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
468         GST_SECOND / depayload->clock_rate;
469     GST_BUFFER_OFFSET (outbuf) =
470         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
471           GST_SECOND / depayload->clock_rate;
472     GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
473           GST_SECOND / depayload->clock_rate;
474
475     GST_DEBUG_OBJECT (depayload, "timestamp : %llu - time %" GST_TIME_FORMAT,
476         GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
477
478   }
479
480   return outbuf;
481
482
483 bad_packet:
484   GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
485       ("Packet did not validate"), (NULL));
486   return NULL;
487 }
488
489 gboolean
490 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
491 {
492   return gst_element_register (plugin, "rtpdtmfdepay",
493       GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);
494 }
495