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