1 /* GStreamer DTMF source
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>
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.
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.
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.
29 * SECTION:element-dtmfsrc
30 * @see_also: rtpdtmsrc, rtpdtmfmuxx
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
41 * <colspec colname='Name' />
42 * <colspec colname='Type' />
43 * <colspec colname='Possible values' />
44 * <colspec colname='Purpose' />
48 * <entry>GType</entry>
49 * <entry>Possible values</entry>
50 * <entry>Purpose</entry>
56 * <entry>G_TYPE_INT</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.
66 * <entry>number</entry>
67 * <entry>G_TYPE_INT</entry>
69 * <entry>The event number.</entry>
72 * <entry>volume</entry>
73 * <entry>G_TYPE_INT</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.
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>
87 * <entry>method</entry>
88 * <entry>G_TYPE_INT</entry>
90 * <entry>The method used for sending event, this element will react if this
91 * field is absent or 2.
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:
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);
109 * event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
110 * gst_element_send_event (pipeline, event);
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.
131 #include "gstdtmfcommon.h"
133 #include "gstdtmfsrc.h"
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
142 #define MIN_DUTY_CYCLE (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
145 typedef struct st_dtmf_key
147 const char *event_name;
150 float high_frequency;
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},
172 #define MAX_DTMF_EVENTS 16
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,
194 GST_DEBUG_CATEGORY_STATIC (gst_dtmf_src_debug);
195 #define GST_CAT_DEFAULT gst_dtmf_src_debug
203 static GstStaticPadTemplate gst_dtmf_src_template =
204 GST_STATIC_PAD_TEMPLATE ("src",
207 GST_STATIC_CAPS ("audio/x-raw-int, "
210 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
211 "signed = (bool) true, rate = (int) [1, MAX], channels = (int) 1")
214 GST_BOILERPLATE (GstDTMFSrc, gst_dtmf_src, GstBaseSrc, GST_TYPE_BASE_SRC);
216 static void gst_dtmf_src_finalize (GObject * object);
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);
232 static gboolean gst_dtmf_src_unlock (GstBaseSrc * src);
234 static gboolean gst_dtmf_src_unlock_stop (GstBaseSrc * src);
235 static gboolean gst_dtmf_src_negotiate (GstBaseSrc * basesrc);
238 gst_dtmf_src_base_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
242 GST_DEBUG_CATEGORY_INIT (gst_dtmf_src_debug, "dtmfsrc", 0, "dtmfsrc element");
244 gst_element_class_add_static_pad_template (element_class,
245 &gst_dtmf_src_template);
247 gst_element_class_set_details_simple (element_class, "DTMF tone generator",
249 "Generates DTMF tones",
250 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
254 gst_dtmf_src_class_init (GstDTMFSrcClass * klass)
256 GObjectClass *gobject_class;
257 GstBaseSrcClass *gstbasesrc_class;
258 GstElementClass *gstelement_class;
260 gobject_class = G_OBJECT_CLASS (klass);
261 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
262 gstelement_class = GST_ELEMENT_CLASS (klass);
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;
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));
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);
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);
287 event_free (GstDTMFSrcEvent * event)
290 g_slice_free (GstDTMFSrcEvent, event);
294 gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, GstDTMFSrcClass * g_class)
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);
300 dtmfsrc->interval = DEFAULT_PACKET_INTERVAL;
302 dtmfsrc->event_queue = g_async_queue_new_full ((GDestroyNotify) event_free);
303 dtmfsrc->last_event = NULL;
305 dtmfsrc->sample_rate = DEFAULT_SAMPLE_RATE;
307 GST_DEBUG_OBJECT (dtmfsrc, "init done");
311 gst_dtmf_src_finalize (GObject * object)
315 dtmfsrc = GST_DTMF_SRC (object);
317 if (dtmfsrc->event_queue) {
318 g_async_queue_unref (dtmfsrc->event_queue);
319 dtmfsrc->event_queue = NULL;
322 G_OBJECT_CLASS (parent_class)->finalize (object);
326 gst_dtmf_src_handle_dtmf_event (GstDTMFSrc * dtmfsrc,
327 const GstStructure * event_structure)
332 GstClockTime last_stop;
335 gboolean correct_order;
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))
342 if (gst_structure_get_int (event_structure, "method", &method)) {
349 if (!gst_structure_get_int (event_structure, "number", &event_number) ||
350 !gst_structure_get_int (event_structure, "volume", &event_volume))
354 GST_OBJECT_LOCK (dtmfsrc);
355 if (gst_structure_get_clock_time (event_structure, "last-stop", &last_stop))
356 dtmfsrc->last_stop = last_stop;
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);
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);
373 GST_DEBUG_OBJECT (dtmfsrc, "Received stop event");
374 gst_dtmf_src_add_stop_event (dtmfsrc);
383 gst_dtmf_src_handle_custom_upstream (GstDTMFSrc * dtmfsrc, GstEvent * event)
385 gboolean result = FALSE;
386 const GstStructure *structure;
388 GstStateChangeReturn ret;
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");
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);
406 gst_dtmf_src_handle_event (GstBaseSrc * src, GstEvent * event)
409 gboolean result = FALSE;
411 dtmfsrc = GST_DTMF_SRC (src);
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);
423 gst_dtmf_src_send_event (GstElement * element, GstEvent * event)
426 if (gst_dtmf_src_handle_event (GST_BASE_SRC (element), event))
429 return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
433 gst_dtmf_src_set_property (GObject * object, guint prop_id,
434 const GValue * value, GParamSpec * pspec)
438 dtmfsrc = GST_DTMF_SRC (object);
442 dtmfsrc->interval = g_value_get_uint (value);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gst_dtmf_src_get_property (GObject * object, guint prop_id, GValue * value,
456 dtmfsrc = GST_DTMF_SRC (object);
460 g_value_set_uint (value, dtmfsrc->interval);
463 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 gst_dtmf_prepare_timestamps (GstDTMFSrc * dtmfsrc)
471 GstClockTime last_stop;
472 GstClockTime timestamp;
474 GST_OBJECT_LOCK (dtmfsrc);
475 last_stop = dtmfsrc->last_stop;
476 GST_OBJECT_UNLOCK (dtmfsrc);
478 if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
479 timestamp = last_stop;
483 /* If there is no valid start time, lets use now as the start time */
485 clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
487 timestamp = gst_clock_get_time (clock)
488 - gst_element_get_base_time (GST_ELEMENT (dtmfsrc));
489 gst_object_unref (clock);
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;
499 /* Make sure the timestamp always goes forward */
500 if (timestamp > dtmfsrc->timestamp)
501 dtmfsrc->timestamp = timestamp;
505 gst_dtmf_src_add_start_event (GstDTMFSrc * dtmfsrc, gint event_number,
509 GstDTMFSrcEvent *event = g_slice_new0 (GstDTMFSrcEvent);
510 event->event_type = DTMF_EVENT_TYPE_START;
512 event->event_number = CLAMP (event_number, MIN_EVENT, MAX_EVENT);
513 event->volume = CLAMP (event_volume, MIN_VOLUME, MAX_VOLUME);
515 g_async_queue_push (dtmfsrc->event_queue, event);
519 gst_dtmf_src_add_stop_event (GstDTMFSrc * dtmfsrc)
522 GstDTMFSrcEvent *event = g_slice_new0 (GstDTMFSrcEvent);
523 event->event_type = DTMF_EVENT_TYPE_STOP;
525 event->event_number = 0;
528 g_async_queue_push (dtmfsrc->event_queue, event);
532 gst_dtmf_src_generate_silence (GstBuffer * buffer, float duration,
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);
546 gst_dtmf_src_generate_tone (GstDTMFSrcEvent * event, DTMF_KEY key,
547 float duration, GstBuffer * buffer, gint sample_rate)
552 double amplitude, f1, f2;
553 double volume_factor;
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);
561 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
563 volume_factor = pow (10, (-event->volume) / 20);
566 * For each sample point we calculate 'x' as the
567 * the amplitude value.
569 for (i = 0; i < (tone_size / (SAMPLE_SIZE / 8)); i++) {
571 * We add the fundamental frequencies together.
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));
576 amplitude = (f1 + f2) / 2;
578 /* Adjust the volume */
579 amplitude *= volume_factor;
581 /* Make the [-1:1] interval into a [-32767:32767] interval */
584 /* Store it in the data buffer */
585 *(p++) = (gint16) amplitude;
594 gst_dtmf_src_create_next_tone_packet (GstDTMFSrc * dtmfsrc,
595 GstDTMFSrcEvent * event)
597 GstBuffer *buf = NULL;
598 gboolean send_silence = FALSE;
599 GstPad *srcpad = GST_BASE_SRC_PAD (dtmfsrc);
601 GST_LOG_OBJECT (dtmfsrc, "Creating buffer for tone %s",
602 DTMF_KEYS[event->event_number].event_name);
604 /* create buffer to hold the tone */
605 buf = gst_buffer_new ();
607 if (event->packet_count * dtmfsrc->interval < MIN_INTER_DIGIT_INTERVAL) {
612 GST_LOG_OBJECT (dtmfsrc, "Generating silence");
613 gst_dtmf_src_generate_silence (buf, dtmfsrc->interval,
614 dtmfsrc->sample_rate);
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);
620 event->packet_count++;
623 /* timestamp and duration of GstBuffer */
624 GST_BUFFER_DURATION (buf) = dtmfsrc->interval * GST_MSECOND;
625 GST_BUFFER_TIMESTAMP (buf) = dtmfsrc->timestamp;
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)));
632 dtmfsrc->timestamp += GST_BUFFER_DURATION (buf);
634 /* Set caps on the buffer before pushing it */
635 gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
641 gst_dtmf_src_post_message (GstDTMFSrc * dtmfsrc, const gchar * message_name,
642 GstDTMFSrcEvent * event)
644 GstStructure *s = NULL;
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);
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);
660 case DTMF_EVENT_TYPE_PAUSE_TASK:
665 gst_element_post_message (GST_ELEMENT (dtmfsrc),
666 gst_message_new_element (GST_OBJECT (dtmfsrc), s));
670 gst_dtmf_src_create (GstBaseSrc * basesrc, guint64 offset,
671 guint length, GstBuffer ** buffer)
673 GstBuffer *buf = NULL;
674 GstDTMFSrcEvent *event;
678 GstClockReturn clockret;
680 dtmfsrc = GST_DTMF_SRC (basesrc);
684 if (dtmfsrc->last_event == NULL) {
685 GST_DEBUG_OBJECT (dtmfsrc, "popping");
686 event = g_async_queue_pop (dtmfsrc->event_queue);
688 GST_DEBUG_OBJECT (dtmfsrc, "popped %d", event->event_type);
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);
696 case DTMF_EVENT_TYPE_START:
697 gst_dtmf_prepare_timestamps (dtmfsrc);
699 event->packet_count = 0;
700 dtmfsrc->last_event = event;
702 gst_dtmf_src_post_message (dtmfsrc, "dtmf-event-processed",
703 dtmfsrc->last_event);
705 case DTMF_EVENT_TYPE_PAUSE_TASK:
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)
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);
716 GST_OBJECT_UNLOCK (dtmfsrc);
720 g_slice_free (GstDTMFSrcEvent, event);
721 } else if (dtmfsrc->last_event->packet_count * dtmfsrc->interval >=
723 event = g_async_queue_try_pop (dtmfsrc->event_queue);
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);
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);
738 case DTMF_EVENT_TYPE_PAUSE_TASK:
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)
743 GST_DEBUG_OBJECT (dtmfsrc, "pushing pause_task...");
745 GST_OBJECT_LOCK (dtmfsrc);
746 if (dtmfsrc->paused) {
747 g_async_queue_push (dtmfsrc->event_queue, event);
750 GST_OBJECT_UNLOCK (dtmfsrc);
754 g_slice_free (GstDTMFSrcEvent, event);
757 } while (dtmfsrc->last_event == NULL);
759 GST_LOG_OBJECT (dtmfsrc, "end event check, now wait for the proper time");
761 clock = gst_element_get_clock (GST_ELEMENT (basesrc));
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);
767 GST_OBJECT_LOCK (dtmfsrc);
768 if (!dtmfsrc->paused) {
769 dtmfsrc->clockid = clockid;
770 GST_OBJECT_UNLOCK (dtmfsrc);
772 clockret = gst_clock_id_wait (clockid, NULL);
774 GST_OBJECT_LOCK (dtmfsrc);
776 clockret = GST_CLOCK_UNSCHEDULED;
778 clockret = GST_CLOCK_UNSCHEDULED;
780 gst_clock_id_unref (clockid);
781 dtmfsrc->clockid = NULL;
782 GST_OBJECT_UNLOCK (dtmfsrc);
784 if (clockret == GST_CLOCK_UNSCHEDULED) {
788 buf = gst_dtmf_src_create_next_tone_packet (dtmfsrc, dtmfsrc->last_event);
790 GST_LOG_OBJECT (dtmfsrc, "Created buffer of size %d", GST_BUFFER_SIZE (buf));
796 GST_OBJECT_UNLOCK (dtmfsrc);
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;
807 return GST_FLOW_WRONG_STATE;
812 gst_dtmf_src_unlock (GstBaseSrc * src)
814 GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
815 GstDTMFSrcEvent *event = NULL;
817 GST_DEBUG_OBJECT (dtmfsrc, "Called unlock");
819 GST_OBJECT_LOCK (dtmfsrc);
820 dtmfsrc->paused = TRUE;
821 if (dtmfsrc->clockid) {
822 gst_clock_id_unschedule (dtmfsrc->clockid);
824 GST_OBJECT_UNLOCK (dtmfsrc);
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);
836 gst_dtmf_src_unlock_stop (GstBaseSrc * src)
838 GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (src);
840 GST_DEBUG_OBJECT (dtmfsrc, "Unlock stopped");
842 GST_OBJECT_LOCK (dtmfsrc);
843 dtmfsrc->paused = FALSE;
844 GST_OBJECT_UNLOCK (dtmfsrc);
851 gst_dtmf_src_negotiate (GstBaseSrc * basesrc)
853 GstDTMFSrc *dtmfsrc = GST_DTMF_SRC (basesrc);
858 caps = gst_pad_get_allowed_caps (GST_BASE_SRC_PAD (basesrc));
862 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
865 if (gst_caps_is_empty (caps))
868 gst_caps_truncate (caps);
869 s = gst_caps_get_structure (caps, 0);
871 gst_structure_fixate_field_nearest_int (s, "rate", DEFAULT_SAMPLE_RATE);
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);
879 ret = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
881 gst_caps_unref (caps);
886 static GstStateChangeReturn
887 gst_dtmf_src_change_state (GstElement * element, GstStateChange transition)
890 GstStateChangeReturn result;
891 gboolean no_preroll = FALSE;
892 GstDTMFSrcEvent *event = NULL;
894 dtmfsrc = GST_DTMF_SRC (element);
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);
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);
906 dtmfsrc->last_event_was_start = FALSE;
907 dtmfsrc->timestamp = 0;
915 GST_ELEMENT_CLASS (parent_class)->change_state (element,
916 transition)) == GST_STATE_CHANGE_FAILURE)
919 switch (transition) {
920 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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);
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);
933 dtmfsrc->last_event_was_start = FALSE;
940 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
941 result = GST_STATE_CHANGE_NO_PREROLL;
948 GST_ERROR_OBJECT (dtmfsrc, "parent failed state change");
954 gst_dtmf_src_plugin_init (GstPlugin * plugin)
956 return gst_element_register (plugin, "dtmfsrc",
957 GST_RANK_NONE, GST_TYPE_DTMF_SRC);