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