[MOVED FROM GST-P-FARSIGHT] Fix byte ordering issues with dtmfsrc and rtpdtmfdepay...
[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
232   clock_rate = depayload->clock_rate;
233
234   /* Create a buffer for the tone */
235   tone_size = (payload.duration*SAMPLE_SIZE*CHANNELS)/8;
236   GST_BUFFER_SIZE (buffer) = tone_size;
237   GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
238   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
239   GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
240
241   p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
242
243   volume_factor = pow (10, (-payload.volume) / 20);
244
245   /*
246    * For each sample point we calculate 'x' as the
247    * the amplitude value.
248    */
249   for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
250     /*
251      * We add the fundamental frequencies together.
252      */
253     f1 = sin(2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample / clock_rate));
254     f2 = sin(2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample / clock_rate));
255
256     amplitude = (f1 + f2) / 2;
257
258     /* Adjust the volume */
259     amplitude *= volume_factor;
260
261     /* Make the [-1:1] interval into a [-32767:32767] interval */
262     amplitude *= 32767;
263
264     /* Store it in the data buffer */
265     *(p++) = (gint16) amplitude;
266
267     (rtpdtmfdepay->sample)++;
268   }
269 }
270
271
272 static GstBuffer *
273 gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
274 {
275
276   GstRtpDTMFDepay *rtpdtmfdepay = NULL;
277   GstBuffer *outbuf = NULL;
278   gint payload_len;
279   guint8 *payload = NULL;
280   guint32 timestamp;
281   GstRTPDTMFPayload dtmf_payload;
282   gboolean marker;
283   GstStructure *structure = NULL;
284   GstMessage *dtmf_message = NULL;
285
286   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
287
288   if (!gst_rtp_buffer_validate (buf))
289     goto bad_packet;
290
291   payload_len = gst_rtp_buffer_get_payload_len (buf);
292   payload = gst_rtp_buffer_get_payload (buf);
293
294   if (payload_len != sizeof(GstRTPDTMFPayload) )
295     goto bad_packet;
296
297   memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
298
299   if (dtmf_payload.event > MAX_EVENT)
300     goto bad_packet;
301
302
303   marker = gst_rtp_buffer_get_marker (buf);
304
305   timestamp = gst_rtp_buffer_get_timestamp (buf);
306
307   dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
308
309   GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
310       "marker=%d - timestamp=%u - event=%d - duration=%d",
311       marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
312
313   GST_DEBUG_OBJECT (depayload, "Previous information : timestamp=%u - duration=%d",
314       rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
315
316   /* First packet */
317   if (marker || rtpdtmfdepay->previous_ts != timestamp) {
318     rtpdtmfdepay->sample = 0;
319     rtpdtmfdepay->previous_ts = timestamp;
320     rtpdtmfdepay->previous_duration = dtmf_payload.duration;
321     rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
322
323     structure = gst_structure_new ("dtmf-event",
324         "number", G_TYPE_INT, dtmf_payload.event,
325         "volume", G_TYPE_INT, dtmf_payload.volume,
326         "type", G_TYPE_INT, 1,
327         "method", G_TYPE_INT, 1,
328         NULL);
329     if (structure) {
330       dtmf_message = gst_message_new_element (GST_OBJECT (depayload), structure);
331       if (dtmf_message) {
332         if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
333           GST_ERROR_OBJECT (depayload, "Unable to send dtmf-event message to bus");
334         }
335       } else {
336         GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
337       }
338     } else {
339       GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
340     }
341   } else {
342     guint16 duration = dtmf_payload.duration;
343     dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
344     /* If late buffer, ignore */
345     if (duration > rtpdtmfdepay->previous_duration)
346       rtpdtmfdepay->previous_duration = duration;
347   }
348
349   GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
350       " - diff  : %d - clock rate : %d - timestamp : %llu",
351       rtpdtmfdepay->previous_duration, dtmf_payload.duration,
352       (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
353       depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
354
355   /* If late or duplicate packet (like the redundant end packet). Ignore */
356   if (dtmf_payload.duration > 0) {
357     outbuf = gst_buffer_new ();
358     gst_dtmf_src_generate_tone(rtpdtmfdepay, dtmf_payload, outbuf);
359
360
361     GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
362         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
363         GST_SECOND / depayload->clock_rate;
364     GST_BUFFER_OFFSET (outbuf) =
365         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
366           GST_SECOND / depayload->clock_rate;
367     GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
368           GST_SECOND / depayload->clock_rate;
369
370     GST_DEBUG_OBJECT (depayload, "timestamp : %llu - time %" GST_TIME_FORMAT,
371         GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
372
373   }
374
375   return outbuf;
376
377
378 bad_packet:
379   GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
380       ("Packet did not validate"), (NULL));
381   return NULL;
382 }
383
384 gboolean
385 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
386 {
387   return gst_element_register (plugin, "rtpdtmfdepay",
388       GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);
389 }
390