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 * @short_description: Generates DTMF packets
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
46 * <colspec colname='Name' />
47 * <colspec colname='Type' />
48 * <colspec colname='Possible values' />
49 * <colspec colname='Purpose' />
54 * <entry>GType</entry>
55 * <entry>Possible values</entry>
56 * <entry>Purpose</entry>
63 * <entry>G_TYPE_INT</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.
71 * <entry>number</entry>
72 * <entry>G_TYPE_INT</entry>
74 * <entry>The event number.</entry>
77 * <entry>volume</entry>
78 * <entry>G_TYPE_INT</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.
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>
92 * <entry>method</entry>
93 * <entry>G_TYPE_INT</entry>
95 * <entry>The method used for sending event, this element will react if this field
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:
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);
117 * event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
118 * gst_element_send_event (pipeline, event);
136 # define M_PI 3.14159265358979323846 /* pi */
140 #include "gstdtmfsrc.h"
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
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)
158 typedef struct st_dtmf_key {
162 float high_frequency;
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},
184 #define MAX_DTMF_EVENTS 16
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,
205 /* elementfactory information */
206 static const GstElementDetails gst_dtmf_src_details =
207 GST_ELEMENT_DETAILS ("DTMF tone generator",
209 "Generates DTMF tones",
210 "Youness Alaoui <youness.alaoui@collabora.co.uk>");
212 GST_DEBUG_CATEGORY_STATIC (gst_dtmf_src_debug);
213 #define GST_CAT_DEFAULT gst_dtmf_src_debug
221 static GstStaticPadTemplate gst_dtmf_src_template =
222 GST_STATIC_PAD_TEMPLATE ("src",
225 GST_STATIC_CAPS ("audio/x-raw-int, "
228 "endianness = (int) 1234, "
229 "signed = (bool) true, "
230 "rate = (int) 8000, "
231 "channels = (int) 1")
234 static GstElementClass *parent_class = NULL;
236 static void gst_dtmf_src_base_init (gpointer g_class);
237 static void gst_dtmf_src_class_init (GstDTMFSrcClass * klass);
238 static void gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, gpointer g_class);
239 static void gst_dtmf_src_finalize (GObject * object);
242 gst_dtmf_src_get_type (void)
244 static GType base_src_type = 0;
246 if (G_UNLIKELY (base_src_type == 0)) {
247 static const GTypeInfo base_src_info = {
248 sizeof (GstDTMFSrcClass),
249 (GBaseInitFunc) gst_dtmf_src_base_init,
251 (GClassInitFunc) gst_dtmf_src_class_init,
256 (GInstanceInitFunc) gst_dtmf_src_init,
259 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
260 "GstDTMFSrc", &base_src_info, 0);
262 return base_src_type;
265 static void gst_dtmf_src_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec);
267 static void gst_dtmf_src_get_property (GObject * object, guint prop_id,
268 GValue * value, GParamSpec * pspec);
269 static gboolean gst_dtmf_src_handle_event (GstPad * pad, GstEvent * event);
270 static GstStateChangeReturn gst_dtmf_src_change_state (GstElement * element,
271 GstStateChange transition);
272 static void gst_dtmf_src_generate_tone(GstDTMFSrcEvent *event, DTMF_KEY key,
273 float duration, GstBuffer * buffer);
274 static void gst_dtmf_src_push_next_tone_packet (GstDTMFSrc *dtmfsrc);
275 static void gst_dtmf_src_start (GstDTMFSrc *dtmfsrc);
276 static void gst_dtmf_src_stop (GstDTMFSrc *dtmfsrc);
277 static void gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc,
278 gint event_number, gint event_volume);
279 static void gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc);
282 gst_dtmf_src_base_init (gpointer g_class)
284 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
286 GST_DEBUG_CATEGORY_INIT (gst_dtmf_src_debug,
287 "dtmfsrc", 0, "dtmfsrc element");
289 gst_element_class_add_pad_template (element_class,
290 gst_static_pad_template_get (&gst_dtmf_src_template));
292 gst_element_class_set_details (element_class, &gst_dtmf_src_details);
296 gst_dtmf_src_class_init (GstDTMFSrcClass * klass)
298 GObjectClass *gobject_class;
299 GstElementClass *gstelement_class;
301 gobject_class = G_OBJECT_CLASS (klass);
302 gstelement_class = GST_ELEMENT_CLASS (klass);
304 parent_class = g_type_class_peek_parent (klass);
306 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_dtmf_src_finalize);
307 gobject_class->set_property =
308 GST_DEBUG_FUNCPTR (gst_dtmf_src_set_property);
309 gobject_class->get_property =
310 GST_DEBUG_FUNCPTR (gst_dtmf_src_get_property);
312 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INTERVAL,
313 g_param_spec_int ("interval", "Interval between tone packets",
314 "Interval in ms between two tone packets", MIN_PACKET_INTERVAL,
315 MAX_PACKET_INTERVAL, DEFAULT_PACKET_INTERVAL, G_PARAM_READWRITE));
317 gstelement_class->change_state =
318 GST_DEBUG_FUNCPTR (gst_dtmf_src_change_state);
322 gst_dtmf_src_init (GstDTMFSrc * dtmfsrc, gpointer g_class)
325 gst_pad_new_from_static_template (&gst_dtmf_src_template, "src");
326 GST_DEBUG_OBJECT (dtmfsrc, "adding src pad");
327 gst_element_add_pad (GST_ELEMENT (dtmfsrc), dtmfsrc->srcpad);
329 gst_pad_set_event_function (dtmfsrc->srcpad, gst_dtmf_src_handle_event);
331 dtmfsrc->interval = DEFAULT_PACKET_INTERVAL;
333 dtmfsrc->event_queue = g_async_queue_new ();
334 dtmfsrc->last_event = NULL;
336 dtmfsrc->clock_id = NULL;
338 GST_DEBUG_OBJECT (dtmfsrc, "init done");
342 gst_dtmf_src_finalize (GObject * object)
346 dtmfsrc = GST_DTMF_SRC (object);
349 gst_dtmf_src_stop (dtmfsrc);
351 if (dtmfsrc->event_queue) {
352 g_async_queue_unref (dtmfsrc->event_queue);
353 dtmfsrc->event_queue = NULL;
356 G_OBJECT_CLASS (parent_class)->finalize (object);
360 gst_dtmf_src_handle_dtmf_event (GstDTMFSrc *dtmfsrc,
361 const GstStructure * event_structure)
367 if (!gst_structure_get_int (event_structure, "type", &event_type) ||
368 !gst_structure_get_boolean (event_structure, "start", &start) ||
369 (start == TRUE && event_type != GST_TONE_DTMF_TYPE_EVENT))
372 if (gst_structure_get_int (event_structure, "method", &method)) {
382 if (!gst_structure_get_int (event_structure, "number", &event_number) ||
383 !gst_structure_get_int (event_structure, "volume", &event_volume))
386 GST_DEBUG_OBJECT (dtmfsrc, "Received start event %d with volume %d",
387 event_number, event_volume);
388 gst_dtmf_src_add_start_event (dtmfsrc, event_number, event_volume);
392 GST_DEBUG_OBJECT (dtmfsrc, "Received stop event");
393 gst_dtmf_src_add_stop_event (dtmfsrc);
402 gst_dtmf_src_handle_custom_upstream (GstDTMFSrc *dtmfsrc,
405 gboolean result = FALSE;
406 const GstStructure *structure;
408 GstStateChangeReturn ret;
410 ret = gst_element_get_state (GST_ELEMENT (dtmfsrc), &state, NULL, 0);
411 if (ret != GST_STATE_CHANGE_SUCCESS || state != GST_STATE_PLAYING) {
412 GST_DEBUG_OBJECT (dtmfsrc, "Received event while not in PLAYING state");
416 GST_DEBUG_OBJECT (dtmfsrc, "Received event is of our interest");
417 structure = gst_event_get_structure (event);
418 if (structure && gst_structure_has_name (structure, "dtmf-event"))
419 result = gst_dtmf_src_handle_dtmf_event (dtmfsrc, structure);
426 gst_dtmf_src_handle_event (GstPad * pad, GstEvent * event)
429 gboolean result = FALSE;
430 GstElement *parent = gst_pad_get_parent_element (pad);
431 dtmfsrc = GST_DTMF_SRC (parent);
433 GST_DEBUG_OBJECT (dtmfsrc, "Received an event on the src pad");
434 switch (GST_EVENT_TYPE (event)) {
435 case GST_EVENT_CUSTOM_UPSTREAM:
437 result = gst_dtmf_src_handle_custom_upstream (dtmfsrc, event);
440 /* Ideally this element should not be flushed but let's handle the event
441 * just in case it is */
442 case GST_EVENT_FLUSH_START:
443 gst_dtmf_src_stop (dtmfsrc);
446 case GST_EVENT_FLUSH_STOP:
447 gst_segment_init (&dtmfsrc->segment, GST_FORMAT_TIME);
450 result = gst_pad_event_default (pad, event);
454 gst_object_unref (parent);
455 gst_event_unref (event);
460 gst_dtmf_src_set_property (GObject * object, guint prop_id,
461 const GValue * value, GParamSpec * pspec)
465 dtmfsrc = GST_DTMF_SRC (object);
469 dtmfsrc->interval = g_value_get_int (value);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478 gst_dtmf_src_get_property (GObject * object, guint prop_id, GValue * value,
483 dtmfsrc = GST_DTMF_SRC (object);
487 g_value_set_uint (value, dtmfsrc->interval);
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gst_dtmf_src_set_stream_lock (GstDTMFSrc *dtmfsrc, gboolean lock)
499 GstStructure *structure;
501 structure = gst_structure_new ("stream-lock",
502 "lock", G_TYPE_BOOLEAN, lock, NULL);
504 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, structure);
505 if (!gst_pad_push_event (dtmfsrc->srcpad, event)) {
506 GST_WARNING_OBJECT (dtmfsrc, "stream-lock event not handled");
511 gst_dtmf_prepare_timestamps (GstDTMFSrc *dtmfsrc)
515 clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
517 dtmfsrc->timestamp = gst_clock_get_time (clock);
518 gst_object_unref (clock);
520 gchar *dtmf_name = gst_element_get_name (dtmfsrc);
521 GST_ERROR_OBJECT (dtmfsrc, "No clock set for element %s", dtmf_name);
522 dtmfsrc->timestamp = GST_CLOCK_TIME_NONE;
528 gst_dtmf_src_start (GstDTMFSrc *dtmfsrc)
530 const GstCaps * caps = gst_pad_get_pad_template_caps (dtmfsrc->srcpad);
532 if (!gst_pad_set_caps (dtmfsrc->srcpad, (GstCaps *)caps))
533 GST_ERROR_OBJECT (dtmfsrc,
534 "Failed to set caps %" GST_PTR_FORMAT " on src pad", caps);
536 GST_DEBUG_OBJECT (dtmfsrc,
537 "caps %" GST_PTR_FORMAT " set on src pad", caps);
540 if (!gst_pad_start_task (dtmfsrc->srcpad,
541 (GstTaskFunction) gst_dtmf_src_push_next_tone_packet, dtmfsrc)) {
542 GST_ERROR_OBJECT (dtmfsrc, "Failed to start task on src pad");
547 gst_dtmf_src_stop (GstDTMFSrc *dtmfsrc)
549 GstDTMFSrcEvent *event = NULL;
551 if (dtmfsrc->clock_id != NULL) {
552 gst_clock_id_unschedule(dtmfsrc->clock_id);
553 gst_clock_id_unref (dtmfsrc->clock_id);
554 dtmfsrc->clock_id = NULL;
559 g_async_queue_lock (dtmfsrc->event_queue);
560 event = g_malloc (sizeof(GstDTMFSrcEvent));
561 event->event_type = DTMF_EVENT_TYPE_PAUSE_TASK;
562 g_async_queue_push_unlocked (dtmfsrc->event_queue, event);
563 g_async_queue_unlock (dtmfsrc->event_queue);
567 if (!gst_pad_pause_task (dtmfsrc->srcpad)) {
568 GST_ERROR_OBJECT (dtmfsrc, "Failed to pause task on src pad");
572 if (dtmfsrc->last_event) {
573 /* Don't forget to release the stream lock */
574 gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
575 g_free (dtmfsrc->last_event);
576 dtmfsrc->last_event = NULL;
579 /* Flushing the event queue */
580 event = g_async_queue_try_pop (dtmfsrc->event_queue);
582 while (event != NULL) {
584 event = g_async_queue_try_pop (dtmfsrc->event_queue);
590 gst_dtmf_src_add_start_event (GstDTMFSrc *dtmfsrc, gint event_number,
594 GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
595 event->event_type = DTMF_EVENT_TYPE_START;
597 event->event_number = CLAMP (event_number, MIN_EVENT, MAX_EVENT);
598 event->volume = CLAMP (event_volume, MIN_VOLUME, MAX_VOLUME);
600 g_async_queue_push (dtmfsrc->event_queue, event);
604 gst_dtmf_src_add_stop_event (GstDTMFSrc *dtmfsrc)
607 GstDTMFSrcEvent * event = g_malloc (sizeof(GstDTMFSrcEvent));
608 event->event_type = DTMF_EVENT_TYPE_STOP;
610 event->event_number = 0;
613 g_async_queue_push (dtmfsrc->event_queue, event);
617 gst_dtmf_src_generate_silence(GstBuffer * buffer, float duration)
621 /* Create a buffer with data set to 0 */
622 buf_size = ((duration/1000)*SAMPLE_RATE*SAMPLE_SIZE*CHANNELS)/8;
623 GST_BUFFER_SIZE (buffer) = buf_size;
624 GST_BUFFER_MALLOCDATA (buffer) = g_malloc0(buf_size);
625 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
630 gst_dtmf_src_generate_tone(GstDTMFSrcEvent *event, DTMF_KEY key, float duration,
636 double amplitude, f1, f2;
637 double volume_factor;
639 /* Create a buffer for the tone */
640 tone_size = ((duration/1000)*SAMPLE_RATE*SAMPLE_SIZE*CHANNELS)/8;
641 GST_BUFFER_SIZE (buffer) = tone_size;
642 GST_BUFFER_MALLOCDATA (buffer) = g_malloc(tone_size);
643 GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
645 p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
647 volume_factor = pow (10, (-event->volume) / 20);
650 * For each sample point we calculate 'x' as the
651 * the amplitude value.
653 for (i = 0; i < (tone_size / (SAMPLE_SIZE/8)); i++) {
655 * We add the fundamental frequencies together.
657 f1 = sin(2 * M_PI * key.low_frequency * (event->sample / SAMPLE_RATE));
658 f2 = sin(2 * M_PI * key.high_frequency * (event->sample / SAMPLE_RATE));
660 amplitude = (f1 + f2) / 2;
662 /* Adjust the volume */
663 amplitude *= volume_factor;
665 /* Make the [-1:1] interval into a [-32767:32767] interval */
668 /* Store it in the data buffer */
669 *(p++) = (gint16) amplitude;
676 gst_dtmf_src_wait_for_buffer_ts (GstDTMFSrc *dtmfsrc, GstBuffer * buf)
680 clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
682 GstClockReturn clock_ret;
684 dtmfsrc->clock_id = gst_clock_new_single_shot_id (clock, GST_BUFFER_TIMESTAMP (buf));
685 gst_object_unref (clock);
687 clock_ret = gst_clock_id_wait (dtmfsrc->clock_id, NULL);
688 if (clock_ret == GST_CLOCK_UNSCHEDULED) {
689 GST_DEBUG_OBJECT (dtmfsrc, "Clock wait unscheduled");
690 /* we don't free anything in case of an unscheduled, because it would be unscheduled
691 * by the stop function which will do the free itself. We can't handle it here
692 * in case we stop the task before the unref is done
695 if (clock_ret != GST_CLOCK_OK && clock_ret != GST_CLOCK_EARLY) {
696 gchar *clock_name = NULL;
698 clock = gst_element_get_clock (GST_ELEMENT (dtmfsrc));
699 clock_name = gst_element_get_name (clock);
700 gst_object_unref (clock);
702 GST_ERROR_OBJECT (dtmfsrc, "Failed to wait on clock %s", clock_name);
705 gst_clock_id_unref (dtmfsrc->clock_id);
706 dtmfsrc->clock_id = NULL;
709 gchar *dtmf_name = gst_element_get_name (dtmfsrc);
710 GST_ERROR_OBJECT (dtmfsrc, "No clock set for element %s", dtmf_name);
717 gst_dtmf_src_create_next_tone_packet (GstDTMFSrc *dtmfsrc,
718 GstDTMFSrcEvent *event)
720 GstBuffer *buf = NULL;
721 gboolean send_silence = FALSE;
723 GST_DEBUG_OBJECT (dtmfsrc, "Creating buffer for tone %s",
724 DTMF_KEYS[event->event_number].event_name);
726 /* create buffer to hold the tone */
727 buf = gst_buffer_new ();
729 if (event->packet_count * dtmfsrc->interval < MIN_INTER_DIGIT_INTERVAL) {
734 GST_DEBUG_OBJECT (dtmfsrc, "Generating silence");
735 gst_dtmf_src_generate_silence (buf, dtmfsrc->interval);
737 GST_DEBUG_OBJECT (dtmfsrc, "Generating tone");
738 gst_dtmf_src_generate_tone(event, DTMF_KEYS[event->event_number],
739 dtmfsrc->interval, buf);
741 event->packet_count++;
744 /* timestamp and duration of GstBuffer */
745 GST_BUFFER_DURATION (buf) = dtmfsrc->interval * GST_MSECOND;
746 GST_BUFFER_TIMESTAMP (buf) = dtmfsrc->timestamp;
747 dtmfsrc->timestamp += GST_BUFFER_DURATION (buf);
749 /* FIXME: Should we sync to clock ourselves or leave it to sink */
750 gst_dtmf_src_wait_for_buffer_ts (dtmfsrc, buf);
752 /* Set caps on the buffer before pushing it */
753 gst_buffer_set_caps (buf, GST_PAD_CAPS (dtmfsrc->srcpad));
759 gst_dtmf_src_push_next_tone_packet (GstDTMFSrc *dtmfsrc)
761 GstBuffer *buf = NULL;
763 GstDTMFSrcEvent *event;
765 g_async_queue_ref (dtmfsrc->event_queue);
767 if (dtmfsrc->last_event == NULL) {
768 event = g_async_queue_pop (dtmfsrc->event_queue);
770 if (event->event_type == DTMF_EVENT_TYPE_STOP) {
771 GST_WARNING_OBJECT (dtmfsrc,
772 "Received a DTMF stop event when already stopped");
773 } else if (event->event_type == DTMF_EVENT_TYPE_START) {
774 gst_dtmf_prepare_timestamps (dtmfsrc);
776 /* Don't forget to get exclusive access to the stream */
777 gst_dtmf_src_set_stream_lock (dtmfsrc, TRUE);
779 event->packet_count = 0;
780 dtmfsrc->last_event = event;
781 } else if (event->event_type == RTP_DTMF_EVENT_TYPE_PAUSE_TASK) {
783 g_async_queue_unref (dtmfsrc->event_queue);
786 } else if (dtmfsrc->last_event->packet_count * dtmfsrc->interval >=
788 event = g_async_queue_try_pop (dtmfsrc->event_queue);
791 if (event->event_type == DTMF_EVENT_TYPE_START) {
792 GST_WARNING_OBJECT (dtmfsrc,
793 "Received two consecutive DTMF start events");
794 } else if (event->event_type == DTMF_EVENT_TYPE_STOP) {
795 gst_dtmf_src_set_stream_lock (dtmfsrc, FALSE);
796 g_free (dtmfsrc->last_event);
797 dtmfsrc->last_event = NULL;
801 g_async_queue_unref (dtmfsrc->event_queue);
803 if (dtmfsrc->last_event) {
804 buf = gst_dtmf_src_create_next_tone_packet (dtmfsrc, dtmfsrc->last_event);
808 GST_DEBUG_OBJECT (dtmfsrc,
809 "pushing buffer on src pad of size %d", GST_BUFFER_SIZE (buf));
810 ret = gst_pad_push (dtmfsrc->srcpad, buf);
811 if (ret != GST_FLOW_OK) {
812 GST_ERROR_OBJECT (dtmfsrc, "Failed to push buffer on src pad");
815 gst_buffer_unref(buf);
816 GST_DEBUG_OBJECT (dtmfsrc, "pushed DTMF tone on src pad");
821 static GstStateChangeReturn
822 gst_dtmf_src_change_state (GstElement * element, GstStateChange transition)
825 GstStateChangeReturn result;
826 gboolean no_preroll = FALSE;
828 dtmfsrc = GST_DTMF_SRC (element);
830 switch (transition) {
831 case GST_STATE_CHANGE_READY_TO_PAUSED:
832 gst_segment_init (&dtmfsrc->segment, GST_FORMAT_TIME);
833 gst_pad_push_event (dtmfsrc->srcpad, gst_event_new_new_segment (FALSE,
834 dtmfsrc->segment.rate, dtmfsrc->segment.format,
835 dtmfsrc->segment.start, dtmfsrc->segment.stop,
836 dtmfsrc->segment.time));
837 /* Indicate that we don't do PRE_ROLL */
840 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
841 gst_dtmf_src_start (dtmfsrc);
848 GST_ELEMENT_CLASS (parent_class)->change_state (element,
849 transition)) == GST_STATE_CHANGE_FAILURE)
852 switch (transition) {
853 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
854 /* Indicate that we don't do PRE_ROLL */
855 gst_dtmf_src_stop (dtmfsrc);
862 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
863 result = GST_STATE_CHANGE_NO_PREROLL;
870 GST_ERROR_OBJECT (dtmfsrc, "parent failed state change");
876 gst_dtmf_src_plugin_init (GstPlugin * plugin)
878 return gst_element_register (plugin, "dtmfsrc",
879 GST_RANK_NONE, GST_TYPE_DTMF_SRC);