[MOVED FROM GST-P-FARSIGHT] Put the sample rate in dtmfsrc into a variable
[platform/upstream/gstreamer.git] / gst / dtmf / gstdtmfsrc.c
1 /* GStreamer DTMF source
2  *
3  * gstdtmfsrc.c:
4  *
5  * Copyright (C) <2007> Collabora.
6  *   Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
7  * Copyright (C) <2007> Nokia Corporation.
8  *   Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
9  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
10  *               2000,2005 Wim Taymans <wim@fluendo.com>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27
28 /**
29  * SECTION:element-dtmfsrc
30  * @short_description: Generates DTMF packets
31  *
32  * <refsect2>
33  *
34  * <para>
35  * The DTMFSrc element generates DTMF (ITU-T Q.23 Specification) tone packets on request
36  * from application. The application communicates the beginning and end of a
37  * DTMF event using custom upstream gstreamer events. To report a DTMF event, an
38  * application must send an event of type GST_EVENT_CUSTOM_UPSTREAM, having a
39  * structure of name "dtmf-event" with fields set according to the following
40  * table:
41  * </para>
42  *
43  * <para>
44  * <informaltable>
45  * <tgroup cols='4'>
46  * <colspec colname='Name' />
47  * <colspec colname='Type' />
48  * <colspec colname='Possible values' />
49  * <colspec colname='Purpose' />
50  *
51  * <thead>
52  * <row>
53  * <entry>Name</entry>
54  * <entry>GType</entry>
55  * <entry>Possible values</entry>
56  * <entry>Purpose</entry>
57  * </row>
58  * </thead>
59  *
60  * <tbody>
61  * <row>
62  * <entry>type</entry>
63  * <entry>G_TYPE_INT</entry>
64  * <entry>0-1</entry>
65  * <entry>The application uses this field to specify which of the two methods
66  * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
67  * named events. This element is only capable of generating named events.
68  * </entry>
69  * </row>
70  * <row>
71  * <entry>number</entry>
72  * <entry>G_TYPE_INT</entry>
73  * <entry>0-16</entry>
74  * <entry>The event number.</entry>
75  * </row>
76  * <row>
77  * <entry>volume</entry>
78  * <entry>G_TYPE_INT</entry>
79  * <entry>0-36</entry>
80  * <entry>This field describes the power level of the tone, expressed in dBm0
81  * after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
82  * valid DTMF is from 0 to -36 dBm0. Can be omitted if start is set to FALSE.
83  * </entry>
84  * </row>
85  * <row>
86  * <entry>start</entry>
87  * <entry>G_TYPE_BOOLEAN</entry>
88  * <entry>True or False</entry>
89  * <entry>Whether the event is starting or ending.</entry>
90  * </row>
91  * <row>
92  * <entry>method</entry>
93  * <entry>G_TYPE_INT</entry>
94  * <entry>2</entry>
95  * <entry>The method used for sending event, this element will react if this
96  * field is absent or 2.
97  * </entry>
98  * </row>
99  * </tbody>
100  * </tgroup>
101  * </informaltable>
102  * </para>
103  *
104  * <para>For example, the following code informs the pipeline (and in turn, the
105  * DTMFSrc element inside the pipeline) about the start of a DTMF named
106  * event '1' of volume -25 dBm0:
107  * </para>
108  *
109  * <para>
110  * <programlisting>
111  * structure = gst_structure_new ("dtmf-event",
112  *                    "type", G_TYPE_INT, 1,
113  *                    "number", G_TYPE_INT, 1,
114  *                    "volume", G_TYPE_INT, 25,
115  *                    "start", G_TYPE_BOOLEAN, TRUE, NULL);
116  *
117  * event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
118  * gst_element_send_event (pipeline, event);
119  * </programlisting>
120  * </para>
121  *
122  * </refsect2>
123  */
124
125 #ifdef HAVE_CONFIG_H
126 #  include "config.h"
127 #endif
128
129 #include <stdlib.h>
130 #include <string.h>
131 #include <math.h>
132
133 #include <glib.h>
134
135 #ifndef M_PI
136 # define M_PI           3.14159265358979323846  /* pi */
137 #endif
138
139
140 #include "gstdtmfsrc.h"
141
142 #define GST_TONE_DTMF_TYPE_EVENT 1
143 #define DEFAULT_PACKET_INTERVAL  50 /* ms */
144 #define MIN_PACKET_INTERVAL      10 /* ms */
145 #define MAX_PACKET_INTERVAL      50 /* ms */
146 #define DEFAULT_SAMPLE_RATE      8000
147 #define SAMPLE_SIZE              16
148 #define CHANNELS                 1
149 #define MIN_EVENT                0
150 #define MAX_EVENT                16
151 #define MIN_VOLUME               0
152 #define MAX_VOLUME               36
153 #define MIN_INTER_DIGIT_INTERVAL 100
154 #define MIN_PULSE_DURATION       250
155 #define MIN_DUTY_CYCLE           (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
156
157
158 typedef struct st_dtmf_key {
159         char *event_name;
160         int event_encoding;
161         float low_frequency;
162         float high_frequency;
163 } DTMF_KEY;
164
165 static const DTMF_KEY DTMF_KEYS[] = {
166         {"DTMF_KEY_EVENT_0",  0, 941, 1336},
167         {"DTMF_KEY_EVENT_1",  1, 697, 1209},
168         {"DTMF_KEY_EVENT_2",  2, 697, 1336},
169         {"DTMF_KEY_EVENT_3",  3, 697, 1477},
170         {"DTMF_KEY_EVENT_4",  4, 770, 1209},
171         {"DTMF_KEY_EVENT_5",  5, 770, 1336},
172         {"DTMF_KEY_EVENT_6",  6, 770, 1477},
173         {"DTMF_KEY_EVENT_7",  7, 852, 1209},
174         {"DTMF_KEY_EVENT_8",  8, 852, 1336},
175         {"DTMF_KEY_EVENT_9",  9, 852, 1477},
176         {"DTMF_KEY_EVENT_S", 10, 941, 1209},
177         {"DTMF_KEY_EVENT_P", 11, 941, 1477},
178         {"DTMF_KEY_EVENT_A", 12, 697, 1633},
179         {"DTMF_KEY_EVENT_B", 13, 770, 1633},
180         {"DTMF_KEY_EVENT_C", 14, 852, 1633},
181         {"DTMF_KEY_EVENT_D", 15, 941, 1633},
182 };
183
184 #define MAX_DTMF_EVENTS 16
185
186 enum {
187 DTMF_KEY_EVENT_1 = 1,
188 DTMF_KEY_EVENT_2 = 2,
189 DTMF_KEY_EVENT_3 = 3,
190 DTMF_KEY_EVENT_4 = 4,
191 DTMF_KEY_EVENT_5 = 5,
192 DTMF_KEY_EVENT_6 = 6,
193 DTMF_KEY_EVENT_7 = 7,
194 DTMF_KEY_EVENT_8 = 8,
195 DTMF_KEY_EVENT_9 = 9,
196 DTMF_KEY_EVENT_0 = 0,
197 DTMF_KEY_EVENT_STAR = 10,
198 DTMF_KEY_EVENT_POUND = 11,
199 DTMF_KEY_EVENT_A = 12,
200 DTMF_KEY_EVENT_B = 13,
201 DTMF_KEY_EVENT_C = 14,
202 DTMF_KEY_EVENT_D = 15,
203 };
204
205 /* elementfactory information */
206 static const GstElementDetails gst_dtmf_src_details =
207 GST_ELEMENT_DETAILS ("DTMF tone generator",
208     "Source/Audio",
209     "Generates DTMF tones",
210     "Youness Alaoui <youness.alaoui@collabora.co.uk>");
211
212 GST_DEBUG_CATEGORY_STATIC (gst_dtmf_src_debug);
213 #define GST_CAT_DEFAULT gst_dtmf_src_debug
214
215 enum
216 {
217   PROP_0,
218   PROP_INTERVAL,
219 };
220
221 static GstStaticPadTemplate gst_dtmf_src_template =
222 GST_STATIC_PAD_TEMPLATE ("src",
223     GST_PAD_SRC,
224     GST_PAD_ALWAYS,
225     GST_STATIC_CAPS ("audio/x-raw-int, "
226         "width = (int) 16, "
227         "depth = (int) 16, "
228         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
229         "signed = (bool) true, "
230         "rate = (int) 8000, "
231         "channels = (int) 1")
232     );
233
234 GST_BOILERPLATE (GstDTMFSrc, gst_dtmf_src, GstBaseSrc, GST_TYPE_BASE_SRC);
235
236 static void gst_dtmf_src_finalize (GObject * object);
237
238 static void gst_dtmf_src_set_property (GObject * object, guint prop_id,
239     const GValue * value, GParamSpec * pspec);
240 static void gst_dtmf_src_get_property (GObject * object, guint prop_id,
241     GValue * value, GParamSpec * pspec);
242 static gboolean gst_dtmf_src_handle_event (GstBaseSrc *src, GstEvent * event);
243 static GstStateChangeReturn gst_dtmf_src_change_state (GstElement * element,
244     GstStateChange transition);
245 static GstFlowReturn gst_dtmf_src_create (GstBaseSrc * basesrc,
246     guint64 offset, guint length, GstBuffer ** buffer);
247 static void gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc,
248     gint event_number, gint event_volume);
249 static void gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc);
250
251 static gboolean gst_dtmf_src_unlock (GstBaseSrc *src);
252
253 static gboolean gst_dtmf_src_unlock_stop (GstBaseSrc *src);
254
255 static void
256 gst_dtmf_src_base_init (gpointer g_class)
257 {
258   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
259
260   GST_DEBUG_CATEGORY_INIT (gst_dtmf_src_debug,
261           "dtmfsrc", 0, "dtmfsrc element");
262
263   gst_element_class_add_pad_template (element_class,
264       gst_static_pad_template_get (&gst_dtmf_src_template));
265
266   gst_element_class_set_details (element_class, &gst_dtmf_src_details);
267 }
268
269 static void
270 gst_dtmf_src_class_init (GstDTMFSrcClass * klass)
271 {
272   GObjectClass *gobject_class;
273   GstBaseSrcClass *gstbasesrc_class;
274   GstElementClass *gstelement_class;
275
276   gobject_class = G_OBJECT_CLASS (klass);
277   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
278   gstelement_class = GST_ELEMENT_CLASS (klass);
279
280
281   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_dtmf_src_finalize);
282   gobject_class->set_property =
283       GST_DEBUG_FUNCPTR (gst_dtmf_src_set_property);
284   gobject_class->get_property =
285       GST_DEBUG_FUNCPTR (gst_dtmf_src_get_property);
286
287   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INTERVAL,
288       g_param_spec_uint ("interval", "Interval between tone packets",
289           "Interval in ms between two tone packets", MIN_PACKET_INTERVAL,
290           MAX_PACKET_INTERVAL, DEFAULT_PACKET_INTERVAL, G_PARAM_READWRITE));
291
292   gstelement_class->change_state =
293       GST_DEBUG_FUNCPTR (gst_dtmf_src_change_state);
294   gstbasesrc_class->unlock =
295       GST_DEBUG_FUNCPTR (gst_dtmf_src_unlock);
296   gstbasesrc_class->unlock_stop =
297       GST_DEBUG_FUNCPTR (gst_dtmf_src_unlock_stop);
298
299   gstbasesrc_class->event =
300       GST_DEBUG_FUNCPTR (gst_dtmf_src_handle_event);
301   gstbasesrc_class->create =
302       GST_DEBUG_FUNCPTR (gst_dtmf_src_create);
303
304 }
305
306
307 static void
308 gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, GstDTMFSrcClass *g_class)
309 {
310   /* we operate in time */
311   gst_base_src_set_format (GST_BASE_SRC (dtmfsrc), GST_FORMAT_TIME);
312   gst_base_src_set_live (GST_BASE_SRC (dtmfsrc), TRUE);
313
314   dtmfsrc->interval = DEFAULT_PACKET_INTERVAL;
315
316   dtmfsrc->event_queue = g_async_queue_new ();
317   dtmfsrc->last_event = NULL;
318
319   dtmfsrc->sample_rate = DEFAULT_SAMPLE_RATE;
320
321   GST_DEBUG_OBJECT (dtmfsrc, "init done");
322 }
323
324 static void
325 gst_dtmf_src_finalize (GObject * object)
326 {
327   GstDTMFSrc *dtmfsrc;
328
329   dtmfsrc = GST_DTMF_SRC (object);
330
331   if (dtmfsrc->event_queue) {
332     g_async_queue_unref (dtmfsrc->event_queue);
333     dtmfsrc->event_queue = NULL;
334   }
335
336   G_OBJECT_CLASS (parent_class)->finalize (object);
337 }
338
339 static gboolean
340 gst_dtmf_src_handle_dtmf_event (GstDTMFSrc *dtmfsrc,
341         const GstStructure * event_structure)
342 {
343   gint event_type;
344   gboolean start;
345   gint method;
346
347   if (!gst_structure_get_int (event_structure, "type", &event_type) ||
348       !gst_structure_get_boolean (event_structure, "start", &start) ||
349       (start == TRUE && event_type != GST_TONE_DTMF_TYPE_EVENT))
350     goto failure;
351
352   if (gst_structure_get_int (event_structure, "method", &method)) {
353     if (method != 2) {
354       goto failure;
355     }
356   }
357
358   if (start) {
359     gint event_number;
360     gint event_volume;
361
362     if (!gst_structure_get_int (event_structure, "number", &event_number) ||
363             !gst_structure_get_int (event_structure, "volume", &event_volume))
364       goto failure;
365
366     GST_DEBUG_OBJECT (dtmfsrc, "Received start event %d with volume %d",
367             event_number, event_volume);
368     gst_dtmf_src_add_start_event (dtmfsrc, event_number, event_volume);
369   }
370
371   else {
372     GST_DEBUG_OBJECT (dtmfsrc, "Received stop event");
373     gst_dtmf_src_add_stop_event (dtmfsrc);
374   }
375
376   return TRUE;
377 failure:
378   return FALSE;
379 }
380
381 static gboolean
382 gst_dtmf_src_handle_custom_upstream (GstDTMFSrc *dtmfsrc,
383     GstEvent * event)
384 {
385   gboolean result = FALSE;
386   const GstStructure *structure;
387   GstState state;
388   GstStateChangeReturn ret;
389
390   ret = gst_element_get_state (GST_ELEMENT (dtmfsrc), &state, NULL, 0);
391   if (ret != GST_STATE_CHANGE_SUCCESS || state != GST_STATE_PLAYING) {
392     GST_DEBUG_OBJECT (dtmfsrc, "Received event while not in PLAYING state");
393     goto ret;
394   }
395
396   GST_DEBUG_OBJECT (dtmfsrc, "Received event is of our interest");
397   structure = gst_event_get_structure (event);
398   if (structure && gst_structure_has_name (structure, "dtmf-event"))
399     result = gst_dtmf_src_handle_dtmf_event (dtmfsrc, structure);
400
401 ret:
402   return result;
403 }
404
405 static gboolean
406 gst_dtmf_src_handle_event (GstBaseSrc * src, GstEvent * event)
407 {
408   GstDTMFSrc *dtmfsrc;
409   gboolean result = FALSE;
410
411   dtmfsrc = GST_DTMF_SRC (src);
412
413   GST_DEBUG_OBJECT (dtmfsrc, "Received an event on the src pad");
414   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
415     result = gst_dtmf_src_handle_custom_upstream (dtmfsrc, event);
416   }
417
418   return result;
419 }
420
421 static void
422 gst_dtmf_src_set_property (GObject * object, guint prop_id,
423     const GValue * value, GParamSpec * pspec)
424 {
425   GstDTMFSrc *dtmfsrc;
426
427   dtmfsrc = GST_DTMF_SRC (object);
428
429   switch (prop_id) {
430     case PROP_INTERVAL:
431       dtmfsrc->interval = g_value_get_uint (value);
432       break;
433     default:
434       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435       break;
436   }
437 }
438
439 static void
440 gst_dtmf_src_get_property (GObject * object, guint prop_id, GValue * value,
441     GParamSpec * pspec)
442 {
443   GstDTMFSrc *dtmfsrc;
444
445   dtmfsrc = GST_DTMF_SRC (object);
446
447   switch (prop_id) {
448     case PROP_INTERVAL:
449       g_value_set_uint (value, dtmfsrc->interval);
450       break;
451     default:
452       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453       break;
454   }
455 }
456
457 static void
458 gst_dtmf_src_set_stream_lock (GstDTMFSrc *dtmfsrc, gboolean lock)
459 {
460    GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
461    GstEvent *event;
462    GstStructure *structure;
463
464    structure = gst_structure_new ("stream-lock",
465                       "lock", G_TYPE_BOOLEAN, lock, NULL);
466
467    event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
468    if (!gst_pad_push_event (srcpad, event)) {
469      GST_WARNING_OBJECT (dtmfsrc, "stream-lock event not handled");
470    }
471 }
472
473 static void
474 gst_dtmf_prepare_timestamps (GstDTMFSrc *dtmfsrc)
475 {
476   GstClock *clock;
477   GstClockTime base_time;
478
479   base_time = gst_element_get_base_time (GST_ELEMENT (dtmfsrc));
480
481   clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
482   if (clock != NULL) {
483 #ifdef MAEMO_BROKEN
484     dtmfsrc->timestamp = gst_clock_get_time (clock);
485 #else
486     dtmfsrc->timestamp = gst_clock_get_time (clock) - base_time;
487 #endif
488     gst_object_unref (clock);
489   } else {
490     gchar *dtmf_name = gst_element_get_name (dtmfsrc);
491     GST_ERROR_OBJECT (dtmfsrc, "No clock set for element %s", dtmf_name);
492     dtmfsrc->timestamp = GST_CLOCK_TIME_NONE;
493     g_free (dtmf_name);
494   }
495 }
496
497 static void
498 gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc, gint event_number,
499     gint event_volume)
500 {
501
502   GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
503   event->event_type = DTMF_EVENT_TYPE_START;
504   event->sample = 0;
505   event->event_number = CLAMP (event_number, MIN_EVENT, MAX_EVENT);
506   event->volume = CLAMP (event_volume, MIN_VOLUME, MAX_VOLUME);
507
508   g_async_queue_push (dtmfsrc->event_queue, event);
509 }
510
511 static void
512 gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc)
513 {
514
515   GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
516   event->event_type = DTMF_EVENT_TYPE_STOP;
517   event->sample = 0;
518   event->event_number = 0;
519   event->volume = 0;
520
521   g_async_queue_push (dtmfsrc->event_queue, event);
522 }
523
524 static void
525 gst_dtmf_src_generate_silence(GstBuffer * buffer, float duration,
526     gint sample_rate)
527 {
528   gint buf_size;
529
530   /* Create a buffer with data set to 0 */
531   buf_size = ((duration/1000)*sample_rate*SAMPLE_SIZE*CHANNELS)/8;
532   GST_BUFFER_SIZE (buffer) = buf_size;
533   GST_BUFFER_MALLOCDATA (buffer) = g_malloc0(buf_size);
534   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
535
536 }
537
538 static void
539 gst_dtmf_src_generate_tone(GstDTMFSrcEvent *event, DTMF_KEY key, float duration,
540     GstBuffer * buffer, gint sample_rate)
541 {
542   gint16 *p;
543   gint tone_size;
544   double i = 0;
545   double amplitude, f1, f2;
546   double volume_factor;
547
548   /* Create a buffer for the tone */
549   tone_size = ((duration/1000)*sample_rate*SAMPLE_SIZE*CHANNELS)/8;
550   GST_BUFFER_SIZE (buffer) = tone_size;
551   GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
552   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
553
554   p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
555
556   volume_factor = pow (10, (-event->volume) / 20);
557
558   /*
559    * For each sample point we calculate 'x' as the
560    * the amplitude value.
561    */
562   for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
563     /*
564      * We add the fundamental frequencies together.
565      */
566     f1 = sin(2 * M_PI * key.low_frequency * (event->sample / sample_rate));
567     f2 = sin(2 * M_PI * key.high_frequency * (event->sample / sample_rate));
568
569     amplitude = (f1 + f2) / 2;
570
571     /* Adjust the volume */
572     amplitude *= volume_factor;
573
574     /* Make the [-1:1] interval into a [-32767:32767] interval */
575     amplitude *= 32767;
576
577     /* Store it in the data buffer */
578     *(p++) = (gint16) amplitude;
579
580     (event->sample)++;
581   }
582 }
583
584
585
586 static GstBuffer *
587 gst_dtmf_src_create_next_tone_packet (GstDTMFSrc *dtmfsrc,
588     GstDTMFSrcEvent *event)
589 {
590   GstBuffer *buf = NULL;
591   gboolean send_silence = FALSE;
592   GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
593
594   GST_DEBUG_OBJECT (dtmfsrc, "Creating buffer for tone %s",
595       DTMF_KEYS[event->event_number].event_name);
596
597   /* create buffer to hold the tone */
598   buf = gst_buffer_new ();
599
600   if (event->packet_count * dtmfsrc->interval < MIN_INTER_DIGIT_INTERVAL) {
601     send_silence = TRUE;
602   }
603
604   if (send_silence) {
605     GST_DEBUG_OBJECT (dtmfsrc,  "Generating silence");
606     gst_dtmf_src_generate_silence (buf, dtmfsrc->interval,
607         dtmfsrc->sample_rate);
608   } else {
609     GST_DEBUG_OBJECT (dtmfsrc,  "Generating tone");
610     gst_dtmf_src_generate_tone(event, DTMF_KEYS[event->event_number],
611         dtmfsrc->interval, buf, dtmfsrc->sample_rate);
612   }
613   event->packet_count++;
614
615
616   /* timestamp and duration of GstBuffer */
617   GST_BUFFER_DURATION (buf) = dtmfsrc->interval * GST_MSECOND;
618   GST_BUFFER_TIMESTAMP (buf) = dtmfsrc->timestamp;
619   dtmfsrc->timestamp += GST_BUFFER_DURATION (buf);
620
621   /* Set caps on the buffer before pushing it */
622   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
623
624   return buf;
625 }
626
627 static GstFlowReturn
628 gst_dtmf_src_create (GstBaseSrc * basesrc, guint64 offset,
629     guint length, GstBuffer ** buffer)
630 {
631   GstBuffer *buf = NULL;
632   GstDTMFSrcEvent *event;
633   GstDTMFSrc * dtmfsrc;
634   GstClock *clock;
635   GstClockID *clockid;
636   GstClockReturn clockret;
637
638   dtmfsrc = GST_DTMF_SRC (basesrc);
639
640   do {
641
642     if (dtmfsrc->last_event == NULL) {
643       GST_DEBUG_OBJECT (dtmfsrc, "popping");
644       event = g_async_queue_pop (dtmfsrc->event_queue);
645
646       GST_DEBUG_OBJECT (dtmfsrc, "popped %d", event->event_type);
647
648       switch (event->event_type) {
649         case DTMF_EVENT_TYPE_STOP:
650           GST_WARNING_OBJECT (dtmfsrc,
651               "Received a DTMF stop event when already stopped");
652           break;
653         case DTMF_EVENT_TYPE_START:
654           gst_dtmf_prepare_timestamps (dtmfsrc);
655
656           /* Don't forget to get exclusive access to the stream */
657           gst_dtmf_src_set_stream_lock (dtmfsrc, TRUE);
658
659           event->packet_count = 0;
660           dtmfsrc->last_event = event;
661           event = NULL;
662           break;
663         case DTMF_EVENT_TYPE_PAUSE_TASK:
664           /*
665            * We're pushing it back because it has to stay in there until
666            * the task is really paused (and the queue will then be flushed)
667            */
668           GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
669           GST_OBJECT_LOCK (dtmfsrc);
670           if (dtmfsrc->paused) {
671             g_async_queue_push (dtmfsrc->event_queue, event);
672             goto paused_locked;
673           }
674           GST_OBJECT_UNLOCK (dtmfsrc);
675           break;
676       }
677       if (event)
678         g_free (event);
679     } else if (dtmfsrc->last_event->packet_count  * dtmfsrc->interval >=
680         MIN_DUTY_CYCLE) {
681       event = g_async_queue_try_pop (dtmfsrc->event_queue);
682
683       if (event != NULL) {
684
685         switch (event->event_type) {
686           case DTMF_EVENT_TYPE_START:
687             GST_WARNING_OBJECT (dtmfsrc,
688                 "Received two consecutive DTMF start events");
689             break;
690           case DTMF_EVENT_TYPE_STOP:
691             gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
692
693             g_free (dtmfsrc->last_event);
694             dtmfsrc->last_event = NULL;
695             break;
696           case DTMF_EVENT_TYPE_PAUSE_TASK:
697             /*
698              * We're pushing it back because it has to stay in there until
699              * the task is really paused (and the queue will then be flushed)
700              */
701             GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
702
703             GST_OBJECT_LOCK (dtmfsrc);
704             if (dtmfsrc->paused) {
705               g_async_queue_push (dtmfsrc->event_queue, event);
706               goto paused_locked;
707             }
708             GST_OBJECT_UNLOCK (dtmfsrc);
709
710             break;
711         }
712         g_free (event);
713       }
714     }
715   } while (dtmfsrc->last_event == NULL);
716
717   GST_DEBUG_OBJECT (dtmfsrc, "end event check, now wait for the proper time");
718
719   clock = gst_element_get_clock (GST_ELEMENT (basesrc));
720
721 #ifdef MAEMO_BROKEN
722   clockid = gst_clock_new_single_shot_id (clock, dtmfsrc->timestamp);
723 #else
724   clockid = gst_clock_new_single_shot_id (clock, dtmfsrc->timestamp +
725       gst_element_get_base_time (GST_ELEMENT (dtmfsrc)));
726 #endif
727   gst_object_unref (clock);
728
729   GST_OBJECT_LOCK (dtmfsrc);
730   if (!dtmfsrc->paused) {
731     dtmfsrc->clockid = clockid;
732     GST_OBJECT_UNLOCK (dtmfsrc);
733
734     clockret = gst_clock_id_wait (clockid, NULL);
735
736     GST_OBJECT_LOCK (dtmfsrc);
737     if (dtmfsrc->paused)
738       clockret = GST_CLOCK_UNSCHEDULED;
739   } else  {
740     clockret = GST_CLOCK_UNSCHEDULED;
741   }
742   gst_clock_id_unref (clockid);
743   dtmfsrc->clockid = NULL;
744   GST_OBJECT_UNLOCK (dtmfsrc);
745
746   if (clockret == GST_CLOCK_UNSCHEDULED) {
747     goto paused;
748   }
749
750   buf = gst_dtmf_src_create_next_tone_packet (dtmfsrc, dtmfsrc->last_event);
751
752   GST_DEBUG_OBJECT (dtmfsrc, "Created buffer of size %d", GST_BUFFER_SIZE (buf));
753   *buffer = buf;
754
755   GST_DEBUG_OBJECT (dtmfsrc, "returning a buffer");
756   return GST_FLOW_OK;
757
758  paused_locked:
759   GST_OBJECT_UNLOCK (dtmfsrc);
760
761  paused:
762
763   if (dtmfsrc->last_event) {
764     GST_DEBUG_OBJECT (dtmfsrc, "Stopping current event");
765     /* Don't forget to release the stream lock */
766     gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
767     g_free (dtmfsrc->last_event);
768     dtmfsrc->last_event = NULL;
769   }
770
771   return GST_FLOW_WRONG_STATE;
772
773 }
774
775 static gboolean
776 gst_dtmf_src_unlock (GstBaseSrc *src) {
777   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
778   GstDTMFSrcEvent *event = NULL;
779
780   GST_DEBUG_OBJECT (dtmfsrc, "Called unlock");
781
782   GST_OBJECT_LOCK (dtmfsrc);
783   dtmfsrc->paused = TRUE;
784   if (dtmfsrc->clockid) {
785     gst_clock_id_unschedule (dtmfsrc->clockid);
786   }
787   GST_OBJECT_UNLOCK (dtmfsrc);
788
789   GST_DEBUG_OBJECT (dtmfsrc, "Pushing the PAUSE_TASK event on unlock request");
790   event = g_malloc (sizeof(GstDTMFSrcEvent));
791   event->event_type = DTMF_EVENT_TYPE_PAUSE_TASK;
792   g_async_queue_push (dtmfsrc->event_queue, event);
793
794   return TRUE;
795 }
796
797
798 static gboolean
799 gst_dtmf_src_unlock_stop (GstBaseSrc *src) {
800   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
801
802   GST_DEBUG_OBJECT (dtmfsrc, "Unlock stopped");
803
804   GST_OBJECT_LOCK (dtmfsrc);
805   dtmfsrc->paused = FALSE;
806   GST_OBJECT_UNLOCK (dtmfsrc);
807
808   return TRUE;
809 }
810
811
812 static GstStateChangeReturn
813 gst_dtmf_src_change_state (GstElement * element, GstStateChange transition)
814 {
815   GstDTMFSrc *dtmfsrc;
816   GstStateChangeReturn result;
817   gboolean no_preroll = FALSE;
818   GstDTMFSrcEvent *event = NULL;
819
820   dtmfsrc = GST_DTMF_SRC (element);
821
822   switch (transition) {
823     case GST_STATE_CHANGE_READY_TO_PAUSED:
824       /* Flushing the event queue */
825       event = g_async_queue_try_pop (dtmfsrc->event_queue);
826
827       while (event != NULL) {
828         g_free (event);
829         event = g_async_queue_try_pop (dtmfsrc->event_queue);
830       }
831       no_preroll = TRUE;
832       break;
833     default:
834       break;
835   }
836
837   if ((result =
838           GST_ELEMENT_CLASS (parent_class)->change_state (element,
839               transition)) == GST_STATE_CHANGE_FAILURE)
840     goto failure;
841
842   switch (transition) {
843     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
844       no_preroll = TRUE;
845       break;
846     case GST_STATE_CHANGE_PAUSED_TO_READY:
847       GST_DEBUG_OBJECT (dtmfsrc, "Flushing event queue");
848       /* Flushing the event queue */
849       event = g_async_queue_try_pop (dtmfsrc->event_queue);
850
851       while (event != NULL) {
852         g_free (event);
853         event = g_async_queue_try_pop (dtmfsrc->event_queue);
854       }
855
856       break;
857     default:
858       break;
859   }
860
861   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
862     result = GST_STATE_CHANGE_NO_PREROLL;
863
864   return result;
865
866   /* ERRORS */
867 failure:
868   {
869     GST_ERROR_OBJECT (dtmfsrc, "parent failed state change");
870     return result;
871   }
872 }
873
874 gboolean
875 gst_dtmf_src_plugin_init (GstPlugin * plugin)
876 {
877   return gst_element_register (plugin, "dtmfsrc",
878       GST_RANK_NONE, GST_TYPE_DTMF_SRC);
879 }
880