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