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