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