[MOVED FROM GST-P-FARSIGHT] Remove get_times (Wim says its only good for really fake...
[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 tones.
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>1</entry>
95  * <entry>The method used for sending event, this element will react if this field
96  * 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, 0,
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 0
143 #define DEFAULT_PACKET_INTERVAL  50 /* ms */
144 #define MIN_PACKET_INTERVAL      10 /* ms */
145 #define MAX_PACKET_INTERVAL      50 /* ms */
146 #define 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) 1234, "
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 void gst_dtmf_src_generate_tone(GstDTMFSrcEvent *event, DTMF_KEY key,
246     float duration, GstBuffer * buffer);
247 static GstFlowReturn gst_dtmf_src_create (GstBaseSrc * basesrc,
248     guint64 offset, guint length, GstBuffer ** buffer);
249 static void gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc,
250     gint event_number, gint event_volume);
251 static void gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc);
252
253 static gboolean gst_dtmf_src_unlock (GstBaseSrc *src);
254
255
256 static void
257 gst_dtmf_src_base_init (gpointer g_class)
258 {
259   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
260
261   GST_DEBUG_CATEGORY_INIT (gst_dtmf_src_debug,
262           "dtmfsrc", 0, "dtmfsrc element");
263
264   gst_element_class_add_pad_template (element_class,
265       gst_static_pad_template_get (&gst_dtmf_src_template));
266
267   gst_element_class_set_details (element_class, &gst_dtmf_src_details);
268 }
269
270 static void
271 gst_dtmf_src_class_init (GstDTMFSrcClass * klass)
272 {
273   GObjectClass *gobject_class;
274   GstBaseSrcClass *gstbasesrc_class;
275   GstElementClass *gstelement_class;
276
277   gobject_class = G_OBJECT_CLASS (klass);
278   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
279   gstelement_class = GST_ELEMENT_CLASS (klass);
280
281
282   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_dtmf_src_finalize);
283   gobject_class->set_property =
284       GST_DEBUG_FUNCPTR (gst_dtmf_src_set_property);
285   gobject_class->get_property =
286       GST_DEBUG_FUNCPTR (gst_dtmf_src_get_property);
287
288   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INTERVAL,
289       g_param_spec_int ("interval", "Interval between tone packets",
290           "Interval in ms between two tone packets", MIN_PACKET_INTERVAL,
291           MAX_PACKET_INTERVAL, DEFAULT_PACKET_INTERVAL, G_PARAM_READWRITE));
292
293   gstelement_class->change_state =
294       GST_DEBUG_FUNCPTR (gst_dtmf_src_change_state);
295   gstbasesrc_class->unlock =
296       GST_DEBUG_FUNCPTR (gst_dtmf_src_unlock);
297
298   gstbasesrc_class->event =
299       GST_DEBUG_FUNCPTR (gst_dtmf_src_handle_event);
300   gstbasesrc_class->create =
301       GST_DEBUG_FUNCPTR (gst_dtmf_src_create);
302
303 }
304
305
306 static void
307 gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, GstDTMFSrcClass *g_class)
308 {
309   /* we operate in time */
310   gst_base_src_set_format (GST_BASE_SRC (dtmfsrc), GST_FORMAT_TIME);
311   gst_base_src_set_live (GST_BASE_SRC (dtmfsrc), TRUE);
312
313   dtmfsrc->interval = DEFAULT_PACKET_INTERVAL;
314
315   dtmfsrc->event_queue = g_async_queue_new ();
316   dtmfsrc->last_event = NULL;
317
318   dtmfsrc->clock_id = NULL;
319
320   GST_DEBUG_OBJECT (dtmfsrc, "init done");
321 }
322
323 static void
324 gst_dtmf_src_finalize (GObject * object)
325 {
326   GstDTMFSrc *dtmfsrc;
327
328   dtmfsrc = GST_DTMF_SRC (object);
329
330   if (dtmfsrc->event_queue) {
331     g_async_queue_unref (dtmfsrc->event_queue);
332     dtmfsrc->event_queue = NULL;
333   }
334
335   G_OBJECT_CLASS (parent_class)->finalize (object);
336 }
337
338 static gboolean
339 gst_dtmf_src_handle_dtmf_event (GstDTMFSrc *dtmfsrc,
340         const GstStructure * event_structure)
341 {
342   gint event_type;
343   gboolean start;
344   gint method;
345
346   if (!gst_structure_get_int (event_structure, "type", &event_type) ||
347       !gst_structure_get_boolean (event_structure, "start", &start) ||
348       (start == TRUE && event_type != GST_TONE_DTMF_TYPE_EVENT))
349     goto failure;
350
351   if (gst_structure_get_int (event_structure, "method", &method)) {
352     if (method != 2) {
353       goto failure;
354     }
355   }
356
357   if (start) {
358     gint event_number;
359     gint event_volume;
360
361     if (!gst_structure_get_int (event_structure, "number", &event_number) ||
362             !gst_structure_get_int (event_structure, "volume", &event_volume))
363       goto failure;
364
365     GST_DEBUG_OBJECT (dtmfsrc, "Received start event %d with volume %d",
366             event_number, event_volume);
367     gst_dtmf_src_add_start_event (dtmfsrc, event_number, event_volume);
368   }
369
370   else {
371     GST_DEBUG_OBJECT (dtmfsrc, "Received stop event");
372     gst_dtmf_src_add_stop_event (dtmfsrc);
373   }
374
375   return TRUE;
376 failure:
377   return FALSE;
378 }
379
380 static gboolean
381 gst_dtmf_src_handle_custom_upstream (GstDTMFSrc *dtmfsrc,
382     GstEvent * event)
383 {
384   gboolean result = FALSE;
385   const GstStructure *structure;
386   GstState state;
387   GstStateChangeReturn ret;
388
389   ret = gst_element_get_state (GST_ELEMENT (dtmfsrc), &state, NULL, 0);
390   if (ret != GST_STATE_CHANGE_SUCCESS || state != GST_STATE_PLAYING) {
391     GST_DEBUG_OBJECT (dtmfsrc, "Received event while not in PLAYING state");
392     goto ret;
393   }
394
395   GST_DEBUG_OBJECT (dtmfsrc, "Received event is of our interest");
396   structure = gst_event_get_structure (event);
397   if (structure && gst_structure_has_name (structure, "dtmf-event"))
398     result = gst_dtmf_src_handle_dtmf_event (dtmfsrc, structure);
399
400 ret:
401   return result;
402 }
403
404 static gboolean
405 gst_dtmf_src_handle_event (GstBaseSrc * src, GstEvent * event)
406 {
407   GstDTMFSrc *dtmfsrc;
408   gboolean result = FALSE;
409
410   dtmfsrc = GST_DTMF_SRC (src);
411
412   GST_DEBUG_OBJECT (dtmfsrc, "Received an event on the src pad");
413   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) {
414     result = gst_dtmf_src_handle_custom_upstream (dtmfsrc, event);
415   }
416
417   return result;
418 }
419
420 static void
421 gst_dtmf_src_set_property (GObject * object, guint prop_id,
422     const GValue * value, GParamSpec * pspec)
423 {
424   GstDTMFSrc *dtmfsrc;
425
426   dtmfsrc = GST_DTMF_SRC (object);
427
428   switch (prop_id) {
429     case PROP_INTERVAL:
430       dtmfsrc->interval = g_value_get_int (value);
431       break;
432     default:
433       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
434       break;
435   }
436 }
437
438 static void
439 gst_dtmf_src_get_property (GObject * object, guint prop_id, GValue * value,
440     GParamSpec * pspec)
441 {
442   GstDTMFSrc *dtmfsrc;
443
444   dtmfsrc = GST_DTMF_SRC (object);
445
446   switch (prop_id) {
447     case PROP_INTERVAL:
448       g_value_set_uint (value, dtmfsrc->interval);
449       break;
450     default:
451       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452       break;
453   }
454 }
455
456 static void
457 gst_dtmf_src_set_stream_lock (GstDTMFSrc *dtmfsrc, gboolean lock)
458 {
459    GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
460    GstEvent *event;
461    GstStructure *structure;
462
463    structure = gst_structure_new ("stream-lock",
464                       "lock", G_TYPE_BOOLEAN, lock, NULL);
465
466    event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
467    if (!gst_pad_push_event (srcpad, event)) {
468      GST_WARNING_OBJECT (dtmfsrc, "stream-lock event not handled");
469    }
470 }
471
472 static void
473 gst_dtmf_prepare_timestamps (GstDTMFSrc *dtmfsrc)
474 {
475   GstClock *clock;
476   GstClockTime base_time;
477
478   base_time = GST_ELEMENT_CAST (dtmfsrc)->base_time;
479
480   clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
481   if (clock != NULL) {
482     dtmfsrc->timestamp = gst_clock_get_time (clock) - base_time;
483     gst_object_unref (clock);
484   } else {
485     gchar *dtmf_name = gst_element_get_name (dtmfsrc);
486     GST_ERROR_OBJECT (dtmfsrc, "No clock set for element %s", dtmf_name);
487     dtmfsrc->timestamp = GST_CLOCK_TIME_NONE;
488     g_free (dtmf_name);
489   }
490 }
491
492 static void
493 gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc, gint event_number,
494     gint event_volume)
495 {
496
497   GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
498   event->event_type = DTMF_EVENT_TYPE_START;
499   event->sample = 0;
500   event->event_number = CLAMP (event_number, MIN_EVENT, MAX_EVENT);
501   event->volume = CLAMP (event_volume, MIN_VOLUME, MAX_VOLUME);
502
503   g_async_queue_push (dtmfsrc->event_queue, event);
504 }
505
506 static void
507 gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc)
508 {
509
510   GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
511   event->event_type = DTMF_EVENT_TYPE_STOP;
512   event->sample = 0;
513   event->event_number = 0;
514   event->volume = 0;
515
516   g_async_queue_push (dtmfsrc->event_queue, event);
517 }
518
519 static void
520 gst_dtmf_src_generate_silence(GstBuffer * buffer, float duration)
521 {
522   gint buf_size;
523
524   /* Create a buffer with data set to 0 */
525   buf_size = ((duration/1000)*SAMPLE_RATE*SAMPLE_SIZE*CHANNELS)/8;
526   GST_BUFFER_SIZE (buffer) = buf_size;
527   GST_BUFFER_MALLOCDATA (buffer) = g_malloc0(buf_size);
528   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
529
530 }
531
532 static void
533 gst_dtmf_src_generate_tone(GstDTMFSrcEvent *event, DTMF_KEY key, float duration,
534     GstBuffer * buffer)
535 {
536   gint16 *p;
537   gint tone_size;
538   double i = 0;
539   double amplitude, f1, f2;
540   double volume_factor;
541
542   /* Create a buffer for the tone */
543   tone_size = ((duration/1000)*SAMPLE_RATE*SAMPLE_SIZE*CHANNELS)/8;
544   GST_BUFFER_SIZE (buffer) = tone_size;
545   GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
546   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
547
548   p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
549
550   volume_factor = pow (10, (-event->volume) / 20);
551
552   /*
553    * For each sample point we calculate 'x' as the
554    * the amplitude value.
555    */
556   for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
557     /*
558      * We add the fundamental frequencies together.
559      */
560     f1 = sin(2 * M_PI * key.low_frequency * (event->sample / SAMPLE_RATE));
561     f2 = sin(2 * M_PI * key.high_frequency * (event->sample / SAMPLE_RATE));
562
563     amplitude = (f1 + f2) / 2;
564
565     /* Adjust the volume */
566     amplitude *= volume_factor;
567
568     /* Make the [-1:1] interval into a [-32767:32767] interval */
569     amplitude *= 32767;
570
571     /* Store it in the data buffer */
572     *(p++) = (gint16) amplitude;
573
574     (event->sample)++;
575   }
576 }
577
578
579
580 static GstBuffer *
581 gst_dtmf_src_create_next_tone_packet (GstDTMFSrc *dtmfsrc,
582     GstDTMFSrcEvent *event)
583 {
584   GstBuffer *buf = NULL;
585   gboolean send_silence = FALSE;
586   GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
587
588   GST_DEBUG_OBJECT (dtmfsrc, "Creating buffer for tone %s",
589       DTMF_KEYS[event->event_number].event_name);
590
591   /* create buffer to hold the tone */
592   buf = gst_buffer_new ();
593
594   if (event->packet_count * dtmfsrc->interval < MIN_INTER_DIGIT_INTERVAL) {
595     send_silence = TRUE;
596   }
597
598   if (send_silence) {
599     GST_DEBUG_OBJECT (dtmfsrc,  "Generating silence");
600     gst_dtmf_src_generate_silence (buf, dtmfsrc->interval);
601   } else {
602     GST_DEBUG_OBJECT (dtmfsrc,  "Generating tone");
603     gst_dtmf_src_generate_tone(event, DTMF_KEYS[event->event_number],
604         dtmfsrc->interval, buf);
605   }
606   event->packet_count++;
607
608
609   /* timestamp and duration of GstBuffer */
610   GST_BUFFER_DURATION (buf) = dtmfsrc->interval * GST_MSECOND;
611   GST_BUFFER_TIMESTAMP (buf) = dtmfsrc->timestamp;
612   dtmfsrc->timestamp += GST_BUFFER_DURATION (buf);
613
614   /* Set caps on the buffer before pushing it */
615   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
616
617   return buf;
618 }
619
620 static GstFlowReturn
621 gst_dtmf_src_create (GstBaseSrc * basesrc, guint64 offset,
622     guint length, GstBuffer ** buffer)
623 {
624   GstBuffer *buf = NULL;
625   GstFlowReturn ret;
626   GstDTMFSrcEvent *event;
627   GstDTMFSrc * dtmfsrc;
628
629   dtmfsrc = GST_DTMF_SRC (basesrc);
630
631   g_async_queue_ref (dtmfsrc->event_queue);
632
633  start:
634   if (dtmfsrc->last_event == NULL) {
635     GST_DEBUG_OBJECT (dtmfsrc, "popping");
636     event = g_async_queue_pop (dtmfsrc->event_queue);
637
638     GST_DEBUG_OBJECT (dtmfsrc, "popped %d", event->event_type);
639
640     if (event->event_type == DTMF_EVENT_TYPE_STOP) {
641       GST_WARNING_OBJECT (dtmfsrc,
642           "Received a DTMF stop event when already stopped");
643     } else if (event->event_type == DTMF_EVENT_TYPE_START) {
644       gst_dtmf_prepare_timestamps (dtmfsrc);
645
646       /* Don't forget to get exclusive access to the stream */
647       gst_dtmf_src_set_stream_lock (dtmfsrc, TRUE);
648
649       event->packet_count = 0;
650       dtmfsrc->last_event = event;
651     } else if (event->event_type == DTMF_EVENT_TYPE_PAUSE_TASK) {
652       /*
653        * We're pushing it back because it has to stay in there until
654        * the task is really paused (and the queue will then be flushed)
655        */
656       GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
657       g_async_queue_push (dtmfsrc->event_queue, event);
658       g_async_queue_unref (dtmfsrc->event_queue);
659     }
660   } else if (dtmfsrc->last_event->packet_count  * dtmfsrc->interval >=
661       MIN_DUTY_CYCLE) {
662     event = g_async_queue_try_pop (dtmfsrc->event_queue);
663
664     if (event != NULL) {
665       if (event->event_type == DTMF_EVENT_TYPE_START) {
666         GST_WARNING_OBJECT (dtmfsrc,
667             "Received two consecutive DTMF start events");
668       } else if (event->event_type == DTMF_EVENT_TYPE_STOP) {
669         gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
670         g_free (dtmfsrc->last_event);
671         dtmfsrc->last_event = NULL;
672         goto start;
673       } else if (event->event_type == DTMF_EVENT_TYPE_PAUSE_TASK) {
674         /*
675          * We're pushing it back because it has to stay in there until
676          * the task is really paused (and the queue will then be flushed)
677          */
678         GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
679         g_async_queue_push (dtmfsrc->event_queue, event);
680         g_async_queue_unref (dtmfsrc->event_queue);
681       }
682     }
683   }
684   g_async_queue_unref (dtmfsrc->event_queue);
685
686   GST_DEBUG_OBJECT (dtmfsrc, "end event check");
687
688   if (dtmfsrc->last_event) {
689     buf = gst_dtmf_src_create_next_tone_packet (dtmfsrc, dtmfsrc->last_event);
690
691     GST_DEBUG_OBJECT (dtmfsrc, "Created buffer of size %d", GST_BUFFER_SIZE (buf));
692     *buffer = buf;
693     ret = GST_FLOW_OK;
694   } else {
695     *buffer = NULL;
696     ret = GST_FLOW_WRONG_STATE;
697   }
698
699   GST_DEBUG_OBJECT (dtmfsrc, "returning");
700   return ret;
701
702 }
703
704 static gboolean
705 gst_dtmf_src_unlock (GstBaseSrc *src) {
706   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
707   GstDTMFSrcEvent *event = NULL;
708
709   GST_DEBUG_OBJECT (dtmfsrc, "Pushing the PAUSE_TASK even on PAUSED_TO_READY change");
710   event = g_malloc (sizeof(GstDTMFSrcEvent));
711   event->event_type = DTMF_EVENT_TYPE_PAUSE_TASK;
712   g_async_queue_push (dtmfsrc->event_queue, event);
713
714   return TRUE;
715 }
716
717 static GstStateChangeReturn
718 gst_dtmf_src_change_state (GstElement * element, GstStateChange transition)
719 {
720   GstDTMFSrc *dtmfsrc;
721   GstStateChangeReturn result;
722   gboolean no_preroll = FALSE;
723   GstDTMFSrcEvent *event = NULL;
724
725   dtmfsrc = GST_DTMF_SRC (element);
726
727   switch (transition) {
728     case GST_STATE_CHANGE_READY_TO_PAUSED:
729     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
730       /* Flushing the event queue */
731       event = g_async_queue_try_pop (dtmfsrc->event_queue);
732
733       while (event != NULL) {
734         g_free (event);
735         event = g_async_queue_try_pop (dtmfsrc->event_queue);
736       }
737       break;
738     default:
739       break;
740   }
741
742   if ((result =
743           GST_ELEMENT_CLASS (parent_class)->change_state (element,
744               transition)) == GST_STATE_CHANGE_FAILURE)
745     goto failure;
746
747   switch (transition) {
748     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
749       GST_DEBUG_OBJECT (dtmfsrc, "PLAYING TO PAUSED");
750
751       if (dtmfsrc->last_event) {
752         GST_DEBUG_OBJECT (dtmfsrc, "Stopping current event");
753         /* Don't forget to release the stream lock */
754         gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
755         g_free (dtmfsrc->last_event);
756         dtmfsrc->last_event = NULL;
757       }
758
759       GST_DEBUG_OBJECT (dtmfsrc, "Flushing event queue");
760       /* Flushing the event queue */
761       event = g_async_queue_try_pop (dtmfsrc->event_queue);
762
763       while (event != NULL) {
764         g_free (event);
765         event = g_async_queue_try_pop (dtmfsrc->event_queue);
766       }
767
768       /* Indicate that we don't do PRE_ROLL */
769       no_preroll = TRUE;
770       break;
771     default:
772       break;
773   }
774
775   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
776     result = GST_STATE_CHANGE_NO_PREROLL;
777
778   return result;
779
780   /* ERRORS */
781 failure:
782   {
783     GST_ERROR_OBJECT (dtmfsrc, "parent failed state change");
784     return result;
785   }
786 }
787
788 gboolean
789 gst_dtmf_src_plugin_init (GstPlugin * plugin)
790 {
791   return gst_element_register (plugin, "dtmfsrc",
792       GST_RANK_NONE, GST_TYPE_DTMF_SRC);
793 }
794