documentation: fix a number of typos
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION:element-rtpdtmfdepay
24  * @title: rtpdtmfdepay
25  * @see_also: rtpdtmfsrc, rtpdtmfmux
26  *
27  * This element takes RTP DTMF packets and produces sound. It also emits a
28  * message on the #GstBus.
29  *
30  * The message is called "dtmf-event" and has the following fields:
31  *
32  * * `type` (G_TYPE_INT, 0-1): Which of the two methods
33  *   specified in RFC 2833 to use. The value should be 0 for tones and 1 for
34  *   named events. Tones are specified by their frequencies and events are specified
35  *   by their number. This element currently only recognizes events.
36  *   Do not confuse with "method" which specified the output.
37  *
38  * * `number` (G_TYPE_INT, 0-16): The event number.
39  *
40  * * `volume` (G_TYPE_INT, 0-36): This field describes the power level of the tone, expressed in dBm0
41  *   after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
42  *   valid DTMF is from 0 to -36 dBm0.
43  *
44  * * `method` (G_TYPE_INT, 1): This field will always been 1 (ie RTP event) from this element.
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include "gstrtpdtmfdepay.h"
52
53 #include <string.h>
54 #include <math.h>
55
56 #include <gst/audio/audio.h>
57 #include <gst/rtp/gstrtpbuffer.h>
58
59 #define DEFAULT_PACKET_INTERVAL  50     /* ms */
60 #define MIN_PACKET_INTERVAL      10     /* ms */
61 #define MAX_PACKET_INTERVAL      50     /* ms */
62 #define SAMPLE_RATE              8000
63 #define SAMPLE_SIZE              16
64 #define CHANNELS                 1
65 #define MIN_DUTY_CYCLE           (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
66
67 #define MIN_UNIT_TIME            0
68 #define MAX_UNIT_TIME            1000
69 #define DEFAULT_UNIT_TIME        0
70
71 #define DEFAULT_MAX_DURATION     0
72
73 typedef struct st_dtmf_key
74 {
75   float low_frequency;
76   float high_frequency;
77 } DTMF_KEY;
78
79 static const DTMF_KEY DTMF_KEYS[] = {
80   {941, 1336},
81   {697, 1209},
82   {697, 1336},
83   {697, 1477},
84   {770, 1209},
85   {770, 1336},
86   {770, 1477},
87   {852, 1209},
88   {852, 1336},
89   {852, 1477},
90   {941, 1209},
91   {941, 1477},
92   {697, 1633},
93   {770, 1633},
94   {852, 1633},
95   {941, 1633},
96 };
97
98 #define MAX_DTMF_EVENTS 16
99
100 enum
101 {
102   DTMF_KEY_EVENT_1 = 1,
103   DTMF_KEY_EVENT_2 = 2,
104   DTMF_KEY_EVENT_3 = 3,
105   DTMF_KEY_EVENT_4 = 4,
106   DTMF_KEY_EVENT_5 = 5,
107   DTMF_KEY_EVENT_6 = 6,
108   DTMF_KEY_EVENT_7 = 7,
109   DTMF_KEY_EVENT_8 = 8,
110   DTMF_KEY_EVENT_9 = 9,
111   DTMF_KEY_EVENT_0 = 0,
112   DTMF_KEY_EVENT_STAR = 10,
113   DTMF_KEY_EVENT_POUND = 11,
114   DTMF_KEY_EVENT_A = 12,
115   DTMF_KEY_EVENT_B = 13,
116   DTMF_KEY_EVENT_C = 14,
117   DTMF_KEY_EVENT_D = 15,
118 };
119
120 GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
121 #define GST_CAT_DEFAULT gst_rtp_dtmf_depay_debug
122
123 enum
124 {
125   /* FILL ME */
126   LAST_SIGNAL
127 };
128
129 enum
130 {
131   PROP_0,
132   PROP_UNIT_TIME,
133   PROP_MAX_DURATION
134 };
135
136 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
137 GST_STATIC_PAD_TEMPLATE ("src",
138     GST_PAD_SRC,
139     GST_PAD_ALWAYS,
140     GST_STATIC_CAPS ("audio/x-raw, "
141         "format = (string) \"" GST_AUDIO_NE (S16) "\", "
142         "rate = " GST_AUDIO_RATE_RANGE ", " "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 G_DEFINE_TYPE (GstRtpDTMFDepay, gst_rtp_dtmf_depay,
157     GST_TYPE_RTP_BASE_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 (GstRTPBaseDepayload * depayload,
164     GstBuffer * buf);
165 gboolean gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter,
166     GstCaps * caps);
167
168 static void
169 gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
170 {
171   GObjectClass *gobject_class;
172   GstElementClass *gstelement_class;
173   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
174
175   gobject_class = G_OBJECT_CLASS (klass);
176   gstelement_class = GST_ELEMENT_CLASS (klass);
177   gstrtpbasedepayload_class = GST_RTP_BASE_DEPAYLOAD_CLASS (klass);
178
179   gst_element_class_add_static_pad_template (gstelement_class,
180       &gst_rtp_dtmf_depay_src_template);
181   gst_element_class_add_static_pad_template (gstelement_class,
182       &gst_rtp_dtmf_depay_sink_template);
183
184   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
185       "rtpdtmfdepay", 0, "rtpdtmfdepay element");
186   gst_element_class_set_static_metadata (gstelement_class,
187       "RTP DTMF packet depayloader", "Codec/Depayloader/Network",
188       "Generates DTMF Sound from telephone-event RTP packets",
189       "Youness Alaoui <youness.alaoui@collabora.co.uk>");
190
191   gobject_class->set_property =
192       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
193   gobject_class->get_property =
194       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_get_property);
195
196   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
197       g_param_spec_uint ("unit-time", "Duration unittime",
198           "The smallest unit (ms) the duration must be a multiple of (0 disables it)",
199           MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME,
200           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201
202   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
203       g_param_spec_uint ("max-duration", "Maximum duration",
204           "The maxumimum duration (ms) of the outgoing soundpacket. "
205           "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
206           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207
208   gstrtpbasedepayload_class->process =
209       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
210   gstrtpbasedepayload_class->set_caps =
211       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
212
213 }
214
215 static void
216 gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay)
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     case PROP_MAX_DURATION:
234       rtpdtmfdepay->max_duration = g_value_get_uint (value);
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238       break;
239   }
240 }
241
242 static void
243 gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
244     GValue * value, GParamSpec * pspec)
245 {
246   GstRtpDTMFDepay *rtpdtmfdepay;
247
248   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (object);
249
250   switch (prop_id) {
251     case PROP_UNIT_TIME:
252       g_value_set_uint (value, rtpdtmfdepay->unit_time);
253       break;
254     case PROP_MAX_DURATION:
255       g_value_set_uint (value, rtpdtmfdepay->max_duration);
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262
263 gboolean
264 gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
265 {
266   GstCaps *filtercaps, *srccaps;
267   GstStructure *structure = gst_caps_get_structure (caps, 0);
268   gint clock_rate = 8000;       /* default */
269
270   gst_structure_get_int (structure, "clock-rate", &clock_rate);
271   filter->clock_rate = clock_rate;
272
273   filtercaps =
274       gst_pad_get_pad_template_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter));
275
276   filtercaps = gst_caps_make_writable (filtercaps);
277   gst_caps_set_simple (filtercaps, "rate", G_TYPE_INT, clock_rate, NULL);
278
279   srccaps = gst_pad_peer_query_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter),
280       filtercaps);
281   gst_caps_unref (filtercaps);
282
283   gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
284   gst_caps_unref (srccaps);
285
286   return TRUE;
287 }
288
289 static GstBuffer *
290 gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
291     GstRTPDTMFPayload payload)
292 {
293   GstBuffer *buf;
294   GstMapInfo map;
295   gint16 *p;
296   gint tone_size;
297   double i = 0;
298   double amplitude, f1, f2;
299   double volume_factor;
300   DTMF_KEY key = DTMF_KEYS[payload.event];
301   guint32 clock_rate;
302   GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtpdtmfdepay);
303   gint volume;
304   static GstAllocationParams params = { 0, 1, 0, 0, };
305
306   clock_rate = depayload->clock_rate;
307
308   /* Create a buffer for the tone */
309   tone_size = (payload.duration * SAMPLE_SIZE * CHANNELS) / 8;
310   buf = gst_buffer_new_allocate (NULL, tone_size, &params);
311   GST_BUFFER_DURATION (buf) = payload.duration * GST_SECOND / clock_rate;
312   volume = payload.volume;
313
314   gst_buffer_map (buf, &map, GST_MAP_WRITE);
315   p = (gint16 *) map.data;
316
317   volume_factor = pow (10, (-volume) / 20);
318
319   /*
320    * For each sample point we calculate 'x' as the
321    * the amplitude value.
322    */
323   for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
324     /*
325      * We add the fundamental frequencies together.
326      */
327     f1 = sin (2 * M_PI * key.low_frequency * (rtpdtmfdepay->sample /
328             clock_rate));
329     f2 = sin (2 * M_PI * key.high_frequency * (rtpdtmfdepay->sample /
330             clock_rate));
331
332     amplitude = (f1 + f2) / 2;
333
334     /* Adjust the volume */
335     amplitude *= volume_factor;
336
337     /* Make the [-1:1] interval into a [-32767:32767] interval */
338     amplitude *= 32767;
339
340     /* Store it in the data buffer */
341     *(p++) = (gint16) amplitude;
342
343     (rtpdtmfdepay->sample)++;
344   }
345
346   gst_buffer_unmap (buf, &map);
347
348   return buf;
349 }
350
351
352 static GstBuffer *
353 gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
354 {
355
356   GstRtpDTMFDepay *rtpdtmfdepay = NULL;
357   GstBuffer *outbuf = NULL;
358   gint payload_len;
359   guint8 *payload = NULL;
360   guint32 timestamp;
361   GstRTPDTMFPayload dtmf_payload;
362   gboolean marker;
363   GstStructure *structure = NULL;
364   GstMessage *dtmf_message = NULL;
365   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
366
367   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
368
369   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
370
371   payload_len = gst_rtp_buffer_get_payload_len (&rtpbuffer);
372   payload = gst_rtp_buffer_get_payload (&rtpbuffer);
373
374   if (payload_len != sizeof (GstRTPDTMFPayload))
375     goto bad_packet;
376
377   memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
378
379   if (dtmf_payload.event > MAX_EVENT)
380     goto bad_packet;
381
382   marker = gst_rtp_buffer_get_marker (&rtpbuffer);
383
384   timestamp = gst_rtp_buffer_get_timestamp (&rtpbuffer);
385
386   dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
387
388   /* clip to whole units of unit_time */
389   if (rtpdtmfdepay->unit_time) {
390     guint unit_time_clock =
391         (rtpdtmfdepay->unit_time * depayload->clock_rate) / 1000;
392     if (dtmf_payload.duration % unit_time_clock) {
393       /* Make sure we don't overflow the duration */
394       if (dtmf_payload.duration < G_MAXUINT16 - unit_time_clock)
395         dtmf_payload.duration += unit_time_clock -
396             (dtmf_payload.duration % unit_time_clock);
397       else
398         dtmf_payload.duration -= dtmf_payload.duration % unit_time_clock;
399     }
400   }
401
402   /* clip to max duration */
403   if (rtpdtmfdepay->max_duration) {
404     guint max_duration_clock =
405         (rtpdtmfdepay->max_duration * depayload->clock_rate) / 1000;
406
407     if (max_duration_clock < G_MAXUINT16 &&
408         dtmf_payload.duration > max_duration_clock)
409       dtmf_payload.duration = max_duration_clock;
410   }
411
412   GST_DEBUG_OBJECT (depayload, "Received new RTP DTMF packet : "
413       "marker=%d - timestamp=%u - event=%d - duration=%d",
414       marker, timestamp, dtmf_payload.event, dtmf_payload.duration);
415
416   GST_DEBUG_OBJECT (depayload,
417       "Previous information : timestamp=%u - duration=%d",
418       rtpdtmfdepay->previous_ts, rtpdtmfdepay->previous_duration);
419
420   /* First packet */
421   if (marker || rtpdtmfdepay->previous_ts != timestamp) {
422     rtpdtmfdepay->sample = 0;
423     rtpdtmfdepay->previous_ts = timestamp;
424     rtpdtmfdepay->previous_duration = dtmf_payload.duration;
425     rtpdtmfdepay->first_gst_ts = GST_BUFFER_PTS (buf);
426
427     structure = gst_structure_new ("dtmf-event",
428         "number", G_TYPE_INT, dtmf_payload.event,
429         "volume", G_TYPE_INT, dtmf_payload.volume,
430         "type", G_TYPE_INT, 1, "method", G_TYPE_INT, 1, NULL);
431     if (structure) {
432       dtmf_message =
433           gst_message_new_element (GST_OBJECT (depayload), structure);
434       if (dtmf_message) {
435         if (!gst_element_post_message (GST_ELEMENT (depayload), dtmf_message)) {
436           GST_ERROR_OBJECT (depayload,
437               "Unable to send dtmf-event message to bus");
438         }
439       } else {
440         GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event message");
441       }
442     } else {
443       GST_ERROR_OBJECT (depayload, "Unable to create dtmf-event structure");
444     }
445   } else {
446     guint16 duration = dtmf_payload.duration;
447     dtmf_payload.duration -= rtpdtmfdepay->previous_duration;
448     /* If late buffer, ignore */
449     if (duration > rtpdtmfdepay->previous_duration)
450       rtpdtmfdepay->previous_duration = duration;
451   }
452
453   GST_DEBUG_OBJECT (depayload, "new previous duration : %d - new duration : %d"
454       " - diff  : %d - clock rate : %d - timestamp : %" G_GUINT64_FORMAT,
455       rtpdtmfdepay->previous_duration, dtmf_payload.duration,
456       (rtpdtmfdepay->previous_duration - dtmf_payload.duration),
457       depayload->clock_rate, GST_BUFFER_TIMESTAMP (buf));
458
459   /* If late or duplicate packet (like the redundant end packet). Ignore */
460   if (dtmf_payload.duration > 0) {
461     outbuf = gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload);
462
463
464     GST_BUFFER_PTS (outbuf) = rtpdtmfdepay->first_gst_ts +
465         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
466         GST_SECOND / depayload->clock_rate;
467     GST_BUFFER_OFFSET (outbuf) =
468         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
469         GST_SECOND / depayload->clock_rate;
470     GST_BUFFER_OFFSET_END (outbuf) = rtpdtmfdepay->previous_duration *
471         GST_SECOND / depayload->clock_rate;
472
473     GST_DEBUG_OBJECT (depayload,
474         "timestamp : %" G_GUINT64_FORMAT " - time %" GST_TIME_FORMAT,
475         GST_BUFFER_TIMESTAMP (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
476
477   }
478
479   gst_rtp_buffer_unmap (&rtpbuffer);
480
481   return outbuf;
482
483 bad_packet:
484   GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
485       ("Packet did not validate"), (NULL));
486
487   if (rtpbuffer.buffer != NULL)
488     gst_rtp_buffer_unmap (&rtpbuffer);
489
490   return NULL;
491 }
492
493 gboolean
494 gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
495 {
496   return gst_element_register (plugin, "rtpdtmfdepay",
497       GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);
498 }