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