Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.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  * @see_also: rtpdtmsrc, rtpdtmfmuxx
31  *
32  * The DTMFSrc element generates DTMF (ITU-T Q.23 Specification) tone packets on request
33  * from application. The application communicates the beginning and end of a
34  * DTMF event using custom upstream gstreamer events. To report a DTMF event, an
35  * application must send an event of type GST_EVENT_CUSTOM_UPSTREAM, having a
36  * structure of name "dtmf-event" with fields set according to the following
37  * table:
38  *
39  * <informaltable>
40  * <tgroup cols='4'>
41  * <colspec colname='Name' />
42  * <colspec colname='Type' />
43  * <colspec colname='Possible values' />
44  * <colspec colname='Purpose' />
45  * <thead>
46  * <row>
47  * <entry>Name</entry>
48  * <entry>GType</entry>
49  * <entry>Possible values</entry>
50  * <entry>Purpose</entry>
51  * </row>
52  * </thead>
53  * <tbody>
54  * <row>
55  * <entry>type</entry>
56  * <entry>G_TYPE_INT</entry>
57  * <entry>0-1</entry>
58  * <entry>The application uses this field to specify which of the two methods
59  * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
60  * named events. Tones are specified by their frequencies and events are specied
61  * by their number. This element can only take events as input. Do not confuse
62  * with "method" which specified the output.
63  * </entry>
64  * </row>
65  * <row>
66  * <entry>number</entry>
67  * <entry>G_TYPE_INT</entry>
68  * <entry>0-15</entry>
69  * <entry>The event number.</entry>
70  * </row>
71  * <row>
72  * <entry>volume</entry>
73  * <entry>G_TYPE_INT</entry>
74  * <entry>0-36</entry>
75  * <entry>This field describes the power level of the tone, expressed in dBm0
76  * after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
77  * valid DTMF is from 0 to -36 dBm0. Can be omitted if start is set to FALSE.
78  * </entry>
79  * </row>
80  * <row>
81  * <entry>start</entry>
82  * <entry>G_TYPE_BOOLEAN</entry>
83  * <entry>True or False</entry>
84  * <entry>Whether the event is starting or ending.</entry>
85  * </row>
86  * <row>
87  * <entry>method</entry>
88  * <entry>G_TYPE_INT</entry>
89  * <entry>2</entry>
90  * <entry>The method used for sending event, this element will react if this
91  * field is absent or 2.
92  * </entry>
93  * </row>
94  * </tbody>
95  * </tgroup>
96  * </informaltable>
97  *
98  * For example, the following code informs the pipeline (and in turn, the
99  * DTMFSrc element inside the pipeline) about the start of a DTMF named
100  * event '1' of volume -25 dBm0:
101  *
102  * <programlisting>
103  * structure = gst_structure_new ("dtmf-event",
104  *                    "type", G_TYPE_INT, 1,
105  *                    "number", G_TYPE_INT, 1,
106  *                    "volume", G_TYPE_INT, 25,
107  *                    "start", G_TYPE_BOOLEAN, TRUE, NULL);
108  *
109  * event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
110  * gst_element_send_event (pipeline, event);
111  * </programlisting>
112  *
113  * When a DTMF tone actually starts or stop, a "dtmf-event-processed"
114  * element #GstMessage with the same fields as the "dtmf-event"
115  * #GstEvent that was used to request the event. Also, if any event
116  * has not been processed when the element goes from the PAUSED to the
117  * READY state, then a "dtmf-event-dropped" message is posted on the
118  * #GstBus in the order that they were received.
119  */
120
121 #ifdef HAVE_CONFIG_H
122 #include "config.h"
123 #endif
124
125 #include <stdlib.h>
126 #include <string.h>
127 #include <math.h>
128
129 #include <glib.h>
130
131 #include "gstdtmfcommon.h"
132
133 #include "gstdtmfsrc.h"
134
135 #define GST_TONE_DTMF_TYPE_EVENT 1
136 #define DEFAULT_PACKET_INTERVAL  50     /* ms */
137 #define MIN_PACKET_INTERVAL      10     /* ms */
138 #define MAX_PACKET_INTERVAL      50     /* ms */
139 #define DEFAULT_SAMPLE_RATE      8000
140 #define SAMPLE_SIZE              16
141 #define CHANNELS                 1
142 #define MIN_DUTY_CYCLE           (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
143
144
145 typedef struct st_dtmf_key
146 {
147   const char *event_name;
148   int event_encoding;
149   float low_frequency;
150   float high_frequency;
151 } DTMF_KEY;
152
153 static const DTMF_KEY DTMF_KEYS[] = {
154   {"DTMF_KEY_EVENT_0", 0, 941, 1336},
155   {"DTMF_KEY_EVENT_1", 1, 697, 1209},
156   {"DTMF_KEY_EVENT_2", 2, 697, 1336},
157   {"DTMF_KEY_EVENT_3", 3, 697, 1477},
158   {"DTMF_KEY_EVENT_4", 4, 770, 1209},
159   {"DTMF_KEY_EVENT_5", 5, 770, 1336},
160   {"DTMF_KEY_EVENT_6", 6, 770, 1477},
161   {"DTMF_KEY_EVENT_7", 7, 852, 1209},
162   {"DTMF_KEY_EVENT_8", 8, 852, 1336},
163   {"DTMF_KEY_EVENT_9", 9, 852, 1477},
164   {"DTMF_KEY_EVENT_S", 10, 941, 1209},
165   {"DTMF_KEY_EVENT_P", 11, 941, 1477},
166   {"DTMF_KEY_EVENT_A", 12, 697, 1633},
167   {"DTMF_KEY_EVENT_B", 13, 770, 1633},
168   {"DTMF_KEY_EVENT_C", 14, 852, 1633},
169   {"DTMF_KEY_EVENT_D", 15, 941, 1633},
170 };
171
172 #define MAX_DTMF_EVENTS 16
173
174 enum
175 {
176   DTMF_KEY_EVENT_1 = 1,
177   DTMF_KEY_EVENT_2 = 2,
178   DTMF_KEY_EVENT_3 = 3,
179   DTMF_KEY_EVENT_4 = 4,
180   DTMF_KEY_EVENT_5 = 5,
181   DTMF_KEY_EVENT_6 = 6,
182   DTMF_KEY_EVENT_7 = 7,
183   DTMF_KEY_EVENT_8 = 8,
184   DTMF_KEY_EVENT_9 = 9,
185   DTMF_KEY_EVENT_0 = 0,
186   DTMF_KEY_EVENT_STAR = 10,
187   DTMF_KEY_EVENT_POUND = 11,
188   DTMF_KEY_EVENT_A = 12,
189   DTMF_KEY_EVENT_B = 13,
190   DTMF_KEY_EVENT_C = 14,
191   DTMF_KEY_EVENT_D = 15,
192 };
193
194 GST_DEBUG_CATEGORY_STATIC (gst_dtmf_src_debug);
195 #define GST_CAT_DEFAULT gst_dtmf_src_debug
196
197 enum
198 {
199   PROP_0,
200   PROP_INTERVAL,
201 };
202
203 static GstStaticPadTemplate gst_dtmf_src_template =
204 GST_STATIC_PAD_TEMPLATE ("src",
205     GST_PAD_SRC,
206     GST_PAD_ALWAYS,
207     GST_STATIC_CAPS ("audio/x-raw-int, "
208         "width = (int) 16, "
209         "depth = (int) 16, "
210         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
211         "signed = (bool) true, rate = (int) [1, MAX], channels = (int) 1")
212     );
213
214 GST_BOILERPLATE (GstDTMFSrc, gst_dtmf_src, GstBaseSrc, GST_TYPE_BASE_SRC);
215
216 static void gst_dtmf_src_finalize (GObject * object);
217
218 static void gst_dtmf_src_set_property (GObject * object, guint prop_id,
219     const GValue * value, GParamSpec * pspec);
220 static void gst_dtmf_src_get_property (GObject * object, guint prop_id,
221     GValue * value, GParamSpec * pspec);
222 static gboolean gst_dtmf_src_handle_event (GstBaseSrc * src, GstEvent * event);
223 static gboolean gst_dtmf_src_send_event (GstElement * src, GstEvent * event);
224 static GstStateChangeReturn gst_dtmf_src_change_state (GstElement * element,
225     GstStateChange transition);
226 static GstFlowReturn gst_dtmf_src_create (GstBaseSrc * basesrc,
227     guint64 offset, guint length, GstBuffer ** buffer);
228 static void gst_dtmf_src_add_start_event (GstDTMFSrc * dtmfsrc,
229     gint event_number, gint event_volume);
230 static void gst_dtmf_src_add_stop_event (GstDTMFSrc * dtmfsrc);
231
232 static gboolean gst_dtmf_src_unlock (GstBaseSrc * src);
233
234 static gboolean gst_dtmf_src_unlock_stop (GstBaseSrc * src);
235 static gboolean gst_dtmf_src_negotiate (GstBaseSrc * basesrc);
236
237 static void
238 gst_dtmf_src_base_init (gpointer g_class)
239 {
240   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
241
242   GST_DEBUG_CATEGORY_INIT (gst_dtmf_src_debug, "dtmfsrc", 0, "dtmfsrc element");
243
244   gst_element_class_add_static_pad_template (element_class,
245       &gst_dtmf_src_template);
246
247   gst_element_class_set_details_simple (element_class, "DTMF tone generator",
248       "Source/Audio",
249       "Generates DTMF tones",
250       "Youness Alaoui <youness.alaoui@collabora.co.uk>");
251 }
252
253 static void
254 gst_dtmf_src_class_init (GstDTMFSrcClass * klass)
255 {
256   GObjectClass *gobject_class;
257   GstBaseSrcClass *gstbasesrc_class;
258   GstElementClass *gstelement_class;
259
260   gobject_class = G_OBJECT_CLASS (klass);
261   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
262   gstelement_class = GST_ELEMENT_CLASS (klass);
263
264
265   gobject_class->finalize = gst_dtmf_src_finalize;
266   gobject_class->set_property = gst_dtmf_src_set_property;
267   gobject_class->get_property = gst_dtmf_src_get_property;
268
269   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INTERVAL,
270       g_param_spec_uint ("interval", "Interval between tone packets",
271           "Interval in ms between two tone packets", MIN_PACKET_INTERVAL,
272           MAX_PACKET_INTERVAL, DEFAULT_PACKET_INTERVAL,
273           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274
275   gstelement_class->change_state =
276       GST_DEBUG_FUNCPTR (gst_dtmf_src_change_state);
277   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_dtmf_src_send_event);
278   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_dtmf_src_unlock);
279   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_dtmf_src_unlock_stop);
280
281   gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dtmf_src_handle_event);
282   gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_dtmf_src_create);
283   gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_dtmf_src_negotiate);
284 }
285
286 static void
287 event_free (GstDTMFSrcEvent * event)
288 {
289   if (event)
290     g_slice_free (GstDTMFSrcEvent, event);
291 }
292
293 static void
294 gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, GstDTMFSrcClass * g_class)
295 {
296   /* we operate in time */
297   gst_base_src_set_format (GST_BASE_SRC (dtmfsrc), GST_FORMAT_TIME);
298   gst_base_src_set_live (GST_BASE_SRC (dtmfsrc), TRUE);
299
300   dtmfsrc->interval = DEFAULT_PACKET_INTERVAL;
301
302   dtmfsrc->event_queue = g_async_queue_new_full ((GDestroyNotify) event_free);
303   dtmfsrc->last_event = NULL;
304
305   dtmfsrc->sample_rate = DEFAULT_SAMPLE_RATE;
306
307   GST_DEBUG_OBJECT (dtmfsrc, "init done");
308 }
309
310 static void
311 gst_dtmf_src_finalize (GObject * object)
312 {
313   GstDTMFSrc *dtmfsrc;
314
315   dtmfsrc = GST_DTMF_SRC (object);
316
317   if (dtmfsrc->event_queue) {
318     g_async_queue_unref (dtmfsrc->event_queue);
319     dtmfsrc->event_queue = NULL;
320   }
321
322   G_OBJECT_CLASS (parent_class)->finalize (object);
323 }
324
325 static gboolean
326 gst_dtmf_src_handle_dtmf_event (GstDTMFSrc * dtmfsrc,
327     const GstStructure * event_structure)
328 {
329   gint event_type;
330   gboolean start;
331   gint method;
332   GstClockTime last_stop;
333   gint event_number;
334   gint event_volume;
335   gboolean correct_order;
336
337   if (!gst_structure_get_int (event_structure, "type", &event_type) ||
338       !gst_structure_get_boolean (event_structure, "start", &start) ||
339       (start == TRUE && event_type != GST_TONE_DTMF_TYPE_EVENT))
340     goto failure;
341
342   if (gst_structure_get_int (event_structure, "method", &method)) {
343     if (method != 2) {
344       goto failure;
345     }
346   }
347
348   if (start)
349     if (!gst_structure_get_int (event_structure, "number", &event_number) ||
350         !gst_structure_get_int (event_structure, "volume", &event_volume))
351       goto failure;
352
353
354   GST_OBJECT_LOCK (dtmfsrc);
355   if (gst_structure_get_clock_time (event_structure, "last-stop", &last_stop))
356     dtmfsrc->last_stop = last_stop;
357   else
358     dtmfsrc->last_stop = GST_CLOCK_TIME_NONE;
359   correct_order = (start != dtmfsrc->last_event_was_start);
360   dtmfsrc->last_event_was_start = start;
361   GST_OBJECT_UNLOCK (dtmfsrc);
362
363   if (!correct_order)
364     goto failure;
365
366   if (start) {
367     GST_DEBUG_OBJECT (dtmfsrc, "Received start event %d with volume %d",
368         event_number, event_volume);
369     gst_dtmf_src_add_start_event (dtmfsrc, event_number, event_volume);
370   }
371
372   else {
373     GST_DEBUG_OBJECT (dtmfsrc, "Received stop event");
374     gst_dtmf_src_add_stop_event (dtmfsrc);
375   }
376
377   return TRUE;
378 failure:
379   return FALSE;
380 }
381
382 static gboolean
383 gst_dtmf_src_handle_custom_upstream (GstDTMFSrc * dtmfsrc, 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
422 static gboolean
423 gst_dtmf_src_send_event (GstElement * element, GstEvent * event)
424 {
425
426   if (gst_dtmf_src_handle_event (GST_BASE_SRC (element), event))
427     return TRUE;
428
429   return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
430 }
431
432 static void
433 gst_dtmf_src_set_property (GObject * object, guint prop_id,
434     const GValue * value, GParamSpec * pspec)
435 {
436   GstDTMFSrc *dtmfsrc;
437
438   dtmfsrc = GST_DTMF_SRC (object);
439
440   switch (prop_id) {
441     case PROP_INTERVAL:
442       dtmfsrc->interval = g_value_get_uint (value);
443       break;
444     default:
445       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446       break;
447   }
448 }
449
450 static void
451 gst_dtmf_src_get_property (GObject * object, guint prop_id, GValue * value,
452     GParamSpec * pspec)
453 {
454   GstDTMFSrc *dtmfsrc;
455
456   dtmfsrc = GST_DTMF_SRC (object);
457
458   switch (prop_id) {
459     case PROP_INTERVAL:
460       g_value_set_uint (value, dtmfsrc->interval);
461       break;
462     default:
463       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
464       break;
465   }
466 }
467
468 static void
469 gst_dtmf_prepare_timestamps (GstDTMFSrc * dtmfsrc)
470 {
471   GstClockTime last_stop;
472   GstClockTime timestamp;
473
474   GST_OBJECT_LOCK (dtmfsrc);
475   last_stop = dtmfsrc->last_stop;
476   GST_OBJECT_UNLOCK (dtmfsrc);
477
478   if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
479     timestamp = last_stop;
480   } else {
481     GstClock *clock;
482
483     /* If there is no valid start time, lets use now as the start time */
484
485     clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
486     if (clock != NULL) {
487       timestamp = gst_clock_get_time (clock)
488           - gst_element_get_base_time (GST_ELEMENT (dtmfsrc));
489       gst_object_unref (clock);
490     } else {
491       gchar *dtmf_name = gst_element_get_name (dtmfsrc);
492       GST_ERROR_OBJECT (dtmfsrc, "No clock set for element %s", dtmf_name);
493       dtmfsrc->timestamp = GST_CLOCK_TIME_NONE;
494       g_free (dtmf_name);
495       return;
496     }
497   }
498
499   /* Make sure the timestamp always goes forward */
500   if (timestamp > dtmfsrc->timestamp)
501     dtmfsrc->timestamp = timestamp;
502 }
503
504 static void
505 gst_dtmf_src_add_start_event (GstDTMFSrc * dtmfsrc, gint event_number,
506     gint event_volume)
507 {
508
509   GstDTMFSrcEvent *event = g_slice_new0 (GstDTMFSrcEvent);
510   event->event_type = DTMF_EVENT_TYPE_START;
511   event->sample = 0;
512   event->event_number = CLAMP (event_number, MIN_EVENT, MAX_EVENT);
513   event->volume = CLAMP (event_volume, MIN_VOLUME, MAX_VOLUME);
514
515   g_async_queue_push (dtmfsrc->event_queue, event);
516 }
517
518 static void
519 gst_dtmf_src_add_stop_event (GstDTMFSrc * dtmfsrc)
520 {
521
522   GstDTMFSrcEvent *event = g_slice_new0 (GstDTMFSrcEvent);
523   event->event_type = DTMF_EVENT_TYPE_STOP;
524   event->sample = 0;
525   event->event_number = 0;
526   event->volume = 0;
527
528   g_async_queue_push (dtmfsrc->event_queue, event);
529 }
530
531 static void
532 gst_dtmf_src_generate_silence (GstBuffer * buffer, float duration,
533     gint sample_rate)
534 {
535   gint buf_size;
536
537   /* Create a buffer with data set to 0 */
538   buf_size = ((duration / 1000) * sample_rate * SAMPLE_SIZE * CHANNELS) / 8;
539   GST_BUFFER_SIZE (buffer) = buf_size;
540   GST_BUFFER_MALLOCDATA (buffer) = g_malloc0 (buf_size);
541   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
542
543 }
544
545 static void
546 gst_dtmf_src_generate_tone (GstDTMFSrcEvent * event, DTMF_KEY key,
547     float duration, GstBuffer * buffer, gint sample_rate)
548 {
549   gint16 *p;
550   gint tone_size;
551   double i = 0;
552   double amplitude, f1, f2;
553   double volume_factor;
554
555   /* Create a buffer for the tone */
556   tone_size = ((duration / 1000) * sample_rate * SAMPLE_SIZE * CHANNELS) / 8;
557   GST_BUFFER_SIZE (buffer) = tone_size;
558   GST_BUFFER_MALLOCDATA (buffer) = g_malloc (tone_size);
559   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
560
561   p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
562
563   volume_factor = pow (10, (-event->volume) / 20);
564
565   /*
566    * For each sample point we calculate 'x' as the
567    * the amplitude value.
568    */
569   for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
570     /*
571      * We add the fundamental frequencies together.
572      */
573     f1 = sin (2 * M_PI * key.low_frequency * (event->sample / sample_rate));
574     f2 = sin (2 * M_PI * key.high_frequency * (event->sample / sample_rate));
575
576     amplitude = (f1 + f2) / 2;
577
578     /* Adjust the volume */
579     amplitude *= volume_factor;
580
581     /* Make the [-1:1] interval into a [-32767:32767] interval */
582     amplitude *= 32767;
583
584     /* Store it in the data buffer */
585     *(p++) = (gint16) amplitude;
586
587     (event->sample)++;
588   }
589 }
590
591
592
593 static GstBuffer *
594 gst_dtmf_src_create_next_tone_packet (GstDTMFSrc * dtmfsrc,
595     GstDTMFSrcEvent * event)
596 {
597   GstBuffer *buf = NULL;
598   gboolean send_silence = FALSE;
599   GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
600
601   GST_LOG_OBJECT (dtmfsrc, "Creating buffer for tone %s",
602       DTMF_KEYS[event->event_number].event_name);
603
604   /* create buffer to hold the tone */
605   buf = gst_buffer_new ();
606
607   if (event->packet_count * dtmfsrc->interval < MIN_INTER_DIGIT_INTERVAL) {
608     send_silence = TRUE;
609   }
610
611   if (send_silence) {
612     GST_LOG_OBJECT (dtmfsrc, "Generating silence");
613     gst_dtmf_src_generate_silence (buf, dtmfsrc->interval,
614         dtmfsrc->sample_rate);
615   } else {
616     GST_LOG_OBJECT (dtmfsrc, "Generating tone");
617     gst_dtmf_src_generate_tone (event, DTMF_KEYS[event->event_number],
618         dtmfsrc->interval, buf, dtmfsrc->sample_rate);
619   }
620   event->packet_count++;
621
622
623   /* timestamp and duration of GstBuffer */
624   GST_BUFFER_DURATION (buf) = dtmfsrc->interval * GST_MSECOND;
625   GST_BUFFER_TIMESTAMP (buf) = dtmfsrc->timestamp;
626
627   GST_LOG_OBJECT (dtmfsrc, "Creating new buffer with event %u duration "
628       " gst: %" GST_TIME_FORMAT " at %" GST_TIME_FORMAT,
629       event->event_number, GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
630       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
631
632   dtmfsrc->timestamp += GST_BUFFER_DURATION (buf);
633
634   /* Set caps on the buffer before pushing it */
635   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
636
637   return buf;
638 }
639
640 static void
641 gst_dtmf_src_post_message (GstDTMFSrc * dtmfsrc, const gchar * message_name,
642     GstDTMFSrcEvent * event)
643 {
644   GstStructure *s = NULL;
645
646   switch (event->event_type) {
647     case DTMF_EVENT_TYPE_START:
648       s = gst_structure_new (message_name,
649           "type", G_TYPE_INT, 1,
650           "method", G_TYPE_INT, 2,
651           "start", G_TYPE_BOOLEAN, TRUE,
652           "number", G_TYPE_INT, event->event_number,
653           "volume", G_TYPE_INT, event->volume, NULL);
654       break;
655     case DTMF_EVENT_TYPE_STOP:
656       s = gst_structure_new (message_name,
657           "type", G_TYPE_INT, 1, "method", G_TYPE_INT, 2,
658           "start", G_TYPE_BOOLEAN, FALSE, NULL);
659       break;
660     case DTMF_EVENT_TYPE_PAUSE_TASK:
661       return;
662   }
663
664   if (s)
665     gst_element_post_message (GST_ELEMENT (dtmfsrc),
666         gst_message_new_element (GST_OBJECT (dtmfsrc), s));
667 }
668
669 static GstFlowReturn
670 gst_dtmf_src_create (GstBaseSrc * basesrc, guint64 offset,
671     guint length, GstBuffer ** buffer)
672 {
673   GstBuffer *buf = NULL;
674   GstDTMFSrcEvent *event;
675   GstDTMFSrc *dtmfsrc;
676   GstClock *clock;
677   GstClockID *clockid;
678   GstClockReturn clockret;
679
680   dtmfsrc = GST_DTMF_SRC (basesrc);
681
682   do {
683
684     if (dtmfsrc->last_event == NULL) {
685       GST_DEBUG_OBJECT (dtmfsrc, "popping");
686       event = g_async_queue_pop (dtmfsrc->event_queue);
687
688       GST_DEBUG_OBJECT (dtmfsrc, "popped %d", event->event_type);
689
690       switch (event->event_type) {
691         case DTMF_EVENT_TYPE_STOP:
692           GST_WARNING_OBJECT (dtmfsrc,
693               "Received a DTMF stop event when already stopped");
694           gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-dropped", event);
695           break;
696         case DTMF_EVENT_TYPE_START:
697           gst_dtmf_prepare_timestamps (dtmfsrc);
698
699           event->packet_count = 0;
700           dtmfsrc->last_event = event;
701           event = NULL;
702           gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-processed",
703               dtmfsrc->last_event);
704           break;
705         case DTMF_EVENT_TYPE_PAUSE_TASK:
706           /*
707            * We're pushing it back because it has to stay in there until
708            * the task is really paused (and the queue will then be flushed)
709            */
710           GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
711           GST_OBJECT_LOCK (dtmfsrc);
712           if (dtmfsrc->paused) {
713             g_async_queue_push (dtmfsrc->event_queue, event);
714             goto paused_locked;
715           }
716           GST_OBJECT_UNLOCK (dtmfsrc);
717           break;
718       }
719       if (event)
720         g_slice_free (GstDTMFSrcEvent, event);
721     } else if (dtmfsrc->last_event->packet_count * dtmfsrc->interval >=
722         MIN_DUTY_CYCLE) {
723       event = g_async_queue_try_pop (dtmfsrc->event_queue);
724
725       if (event != NULL) {
726
727         switch (event->event_type) {
728           case DTMF_EVENT_TYPE_START:
729             GST_WARNING_OBJECT (dtmfsrc,
730                 "Received two consecutive DTMF start events");
731             gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-dropped", event);
732             break;
733           case DTMF_EVENT_TYPE_STOP:
734             g_slice_free (GstDTMFSrcEvent, dtmfsrc->last_event);
735             dtmfsrc->last_event = NULL;
736             gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-processed", event);
737             break;
738           case DTMF_EVENT_TYPE_PAUSE_TASK:
739             /*
740              * We're pushing it back because it has to stay in there until
741              * the task is really paused (and the queue will then be flushed)
742              */
743             GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
744
745             GST_OBJECT_LOCK (dtmfsrc);
746             if (dtmfsrc->paused) {
747               g_async_queue_push (dtmfsrc->event_queue, event);
748               goto paused_locked;
749             }
750             GST_OBJECT_UNLOCK (dtmfsrc);
751
752             break;
753         }
754         g_slice_free (GstDTMFSrcEvent, event);
755       }
756     }
757   } while (dtmfsrc->last_event == NULL);
758
759   GST_LOG_OBJECT (dtmfsrc, "end event check, now wait for the proper time");
760
761   clock = gst_element_get_clock (GST_ELEMENT (basesrc));
762
763   clockid = gst_clock_new_single_shot_id (clock, dtmfsrc->timestamp +
764       gst_element_get_base_time (GST_ELEMENT (dtmfsrc)));
765   gst_object_unref (clock);
766
767   GST_OBJECT_LOCK (dtmfsrc);
768   if (!dtmfsrc->paused) {
769     dtmfsrc->clockid = clockid;
770     GST_OBJECT_UNLOCK (dtmfsrc);
771
772     clockret = gst_clock_id_wait (clockid, NULL);
773
774     GST_OBJECT_LOCK (dtmfsrc);
775     if (dtmfsrc->paused)
776       clockret = GST_CLOCK_UNSCHEDULED;
777   } else {
778     clockret = GST_CLOCK_UNSCHEDULED;
779   }
780   gst_clock_id_unref (clockid);
781   dtmfsrc->clockid = NULL;
782   GST_OBJECT_UNLOCK (dtmfsrc);
783
784   if (clockret == GST_CLOCK_UNSCHEDULED) {
785     goto paused;
786   }
787
788   buf = gst_dtmf_src_create_next_tone_packet (dtmfsrc, dtmfsrc->last_event);
789
790   GST_LOG_OBJECT (dtmfsrc, "Created buffer of size %d", GST_BUFFER_SIZE (buf));
791   *buffer = buf;
792
793   return GST_FLOW_OK;
794
795 paused_locked:
796   GST_OBJECT_UNLOCK (dtmfsrc);
797
798 paused:
799
800   if (dtmfsrc->last_event) {
801     GST_DEBUG_OBJECT (dtmfsrc, "Stopping current event");
802     /* Don't forget to release the stream lock */
803     g_slice_free (GstDTMFSrcEvent, dtmfsrc->last_event);
804     dtmfsrc->last_event = NULL;
805   }
806
807   return GST_FLOW_WRONG_STATE;
808
809 }
810
811 static gboolean
812 gst_dtmf_src_unlock (GstBaseSrc * src)
813 {
814   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
815   GstDTMFSrcEvent *event = NULL;
816
817   GST_DEBUG_OBJECT (dtmfsrc, "Called unlock");
818
819   GST_OBJECT_LOCK (dtmfsrc);
820   dtmfsrc->paused = TRUE;
821   if (dtmfsrc->clockid) {
822     gst_clock_id_unschedule (dtmfsrc->clockid);
823   }
824   GST_OBJECT_UNLOCK (dtmfsrc);
825
826   GST_DEBUG_OBJECT (dtmfsrc, "Pushing the PAUSE_TASK event on unlock request");
827   event = g_slice_new0 (GstDTMFSrcEvent);
828   event->event_type = DTMF_EVENT_TYPE_PAUSE_TASK;
829   g_async_queue_push (dtmfsrc->event_queue, event);
830
831   return TRUE;
832 }
833
834
835 static gboolean
836 gst_dtmf_src_unlock_stop (GstBaseSrc * src)
837 {
838   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
839
840   GST_DEBUG_OBJECT (dtmfsrc, "Unlock stopped");
841
842   GST_OBJECT_LOCK (dtmfsrc);
843   dtmfsrc->paused = FALSE;
844   GST_OBJECT_UNLOCK (dtmfsrc);
845
846   return TRUE;
847 }
848
849
850 static gboolean
851 gst_dtmf_src_negotiate (GstBaseSrc * basesrc)
852 {
853   GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (basesrc);
854   GstCaps *caps;
855   GstStructure *s;
856   gboolean ret;
857
858   caps = gst_pad_get_allowed_caps (GST_BASE_SRC_PAD (basesrc));
859
860   if (!caps)
861     caps =
862         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
863             (basesrc)));
864
865   if (gst_caps_is_empty (caps))
866     return FALSE;
867
868   gst_caps_truncate (caps);
869   s = gst_caps_get_structure (caps, 0);
870
871   gst_structure_fixate_field_nearest_int (s, "rate", DEFAULT_SAMPLE_RATE);
872
873   if (!gst_structure_get_int (s, "rate", &dtmfsrc->sample_rate)) {
874     GST_ERROR_OBJECT (dtmfsrc, "Could not get rate");
875     gst_caps_unref (caps);
876     return FALSE;
877   }
878
879   ret = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
880
881   gst_caps_unref (caps);
882
883   return ret;
884 }
885
886 static GstStateChangeReturn
887 gst_dtmf_src_change_state (GstElement * element, GstStateChange transition)
888 {
889   GstDTMFSrc *dtmfsrc;
890   GstStateChangeReturn result;
891   gboolean no_preroll = FALSE;
892   GstDTMFSrcEvent *event = NULL;
893
894   dtmfsrc = GST_DTMF_SRC (element);
895
896   switch (transition) {
897     case GST_STATE_CHANGE_READY_TO_PAUSED:
898       /* Flushing the event queue */
899       event = g_async_queue_try_pop (dtmfsrc->event_queue);
900
901       while (event != NULL) {
902         gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-dropped", event);
903         g_slice_free (GstDTMFSrcEvent, event);
904         event = g_async_queue_try_pop (dtmfsrc->event_queue);
905       }
906       dtmfsrc->last_event_was_start = FALSE;
907       dtmfsrc->timestamp = 0;
908       no_preroll = TRUE;
909       break;
910     default:
911       break;
912   }
913
914   if ((result =
915           GST_ELEMENT_CLASS (parent_class)->change_state (element,
916               transition)) == GST_STATE_CHANGE_FAILURE)
917     goto failure;
918
919   switch (transition) {
920     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
921       no_preroll = TRUE;
922       break;
923     case GST_STATE_CHANGE_PAUSED_TO_READY:
924       GST_DEBUG_OBJECT (dtmfsrc, "Flushing event queue");
925       /* Flushing the event queue */
926       event = g_async_queue_try_pop (dtmfsrc->event_queue);
927
928       while (event != NULL) {
929         gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-dropped", event);
930         g_slice_free (GstDTMFSrcEvent, event);
931         event = g_async_queue_try_pop (dtmfsrc->event_queue);
932       }
933       dtmfsrc->last_event_was_start = FALSE;
934
935       break;
936     default:
937       break;
938   }
939
940   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
941     result = GST_STATE_CHANGE_NO_PREROLL;
942
943   return result;
944
945   /* ERRORS */
946 failure:
947   {
948     GST_ERROR_OBJECT (dtmfsrc, "parent failed state change");
949     return result;
950   }
951 }
952
953 gboolean
954 gst_dtmf_src_plugin_init (GstPlugin * plugin)
955 {
956   return gst_element_register (plugin, "dtmfsrc",
957       GST_RANK_NONE, GST_TYPE_DTMF_SRC);
958 }