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