2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
31 #include "gstosssink.h"
33 /* elementfactory information */
34 static GstElementDetails gst_osssink_details = GST_ELEMENT_DETAILS (
37 "Output to a sound card via OSS",
38 "Erik Walthinsen <omega@cse.ogi.edu>, "
39 "Wim Taymans <wim.taymans@chello.be>"
42 static void gst_osssink_base_init (gpointer g_class);
43 static void gst_osssink_class_init (GstOssSinkClass *klass);
44 static void gst_osssink_init (GstOssSink *osssink);
45 static void gst_osssink_dispose (GObject *object);
47 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
48 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
49 static GstClock* gst_osssink_get_clock (GstElement *element);
50 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
52 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
53 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
54 GstFormat *dest_format, gint64 *dest_value);
55 static const GstQueryType* gst_osssink_get_query_types (GstPad *pad);
56 static gboolean gst_osssink_query (GstElement *element, GstQueryType type,
57 GstFormat *format, gint64 *value);
58 static gboolean gst_osssink_sink_query (GstPad *pad, GstQueryType type,
59 GstFormat *format, gint64 *value);
61 static GstCaps * gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps);
62 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps);
64 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
66 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
69 static void gst_osssink_chain (GstPad *pad,GstData *_data);
71 /* OssSink signals and args */
87 static GstStaticPadTemplate osssink_sink_factory =
88 GST_STATIC_PAD_TEMPLATE (
92 GST_STATIC_CAPS ("audio/x-raw-int, "
93 "endianness = (int) BYTE_ORDER, "
94 "signed = (boolean) { TRUE, FALSE }, "
95 "width = (int) { 8, 16 }, "
96 "depth = (int) { 8, 16 }, "
97 "rate = (int) [ 1000, 48000 ], "
98 "channels = (int) [ 1, 2 ]"
102 static GstElementClass *parent_class = NULL;
103 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
106 gst_osssink_get_type (void)
108 static GType osssink_type = 0;
111 static const GTypeInfo osssink_info = {
112 sizeof(GstOssSinkClass),
113 gst_osssink_base_init,
115 (GClassInitFunc)gst_osssink_class_init,
120 (GInstanceInitFunc)gst_osssink_init,
122 osssink_type = g_type_register_static (GST_TYPE_OSSELEMENT, "GstOssSink", &osssink_info, 0);
129 gst_osssink_dispose (GObject *object)
131 GstOssSink *osssink = (GstOssSink *) object;
133 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
135 G_OBJECT_CLASS (parent_class)->dispose (object);
139 gst_osssink_base_init (gpointer g_class)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
143 gst_element_class_set_details (element_class, &gst_osssink_details);
144 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&osssink_sink_factory));
147 gst_osssink_class_init (GstOssSinkClass *klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
152 gobject_class = (GObjectClass*)klass;
153 gstelement_class = (GstElementClass*)klass;
155 parent_class = g_type_class_ref(GST_TYPE_OSSELEMENT);
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
158 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
159 FALSE, G_PARAM_READWRITE));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
161 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
162 TRUE, G_PARAM_READWRITE));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
164 g_param_spec_int ("fragment", "Fragment",
165 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
166 0, G_MAXINT, 6, G_PARAM_READWRITE));
167 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
168 g_param_spec_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
169 0, G_MAXINT, 4096, G_PARAM_READWRITE));
170 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
171 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
172 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
174 gst_osssink_signals[SIGNAL_HANDOFF] =
175 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
177 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
179 gobject_class->set_property = gst_osssink_set_property;
180 gobject_class->get_property = gst_osssink_get_property;
181 gobject_class->dispose = gst_osssink_dispose;
183 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
184 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
185 gstelement_class->set_clock = gst_osssink_set_clock;
186 gstelement_class->get_clock = gst_osssink_get_clock;
191 gst_osssink_init (GstOssSink *osssink)
193 osssink->sinkpad = gst_pad_new_from_template (
194 gst_static_pad_template_get (&osssink_sink_factory), "sink");
195 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
196 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
197 gst_pad_set_fixate_function (osssink->sinkpad, gst_osssink_sink_fixate);
198 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
199 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
200 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
201 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
203 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
205 osssink->bufsize = 4096;
206 osssink->chunk_size = 4096;
207 osssink->resync = FALSE;
208 osssink->mute = FALSE;
209 osssink->sync = TRUE;
210 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
211 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
212 osssink->handled = 0;
214 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
215 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
219 gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps)
222 GstStructure *structure;
224 structure = gst_structure_copy(gst_caps_get_structure (caps, 0));
225 newcaps = gst_caps_new_full (structure, NULL);
227 if (gst_caps_structure_fixate_field_nearest_int (structure, "rate", 44100)) {
230 if (gst_caps_structure_fixate_field_nearest_int (structure, "depth", 16)) {
233 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 16)) {
236 if (gst_caps_structure_fixate_field_nearest_int (structure, "channels", 2)) {
240 gst_caps_free (newcaps);
245 static GstPadLinkReturn
246 gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps)
248 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
250 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
251 return GST_PAD_LINK_REFUSED;
253 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
254 return GST_PAD_LINK_REFUSED;
257 return GST_PAD_LINK_OK;
261 gst_osssink_get_delay (GstOssSink *osssink)
265 if (GST_OSSELEMENT (osssink)->fd == -1)
268 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
270 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
274 delay = (info.fragstotal * info.fragsize) - info.bytes;
281 gst_osssink_get_time (GstClock *clock, gpointer data)
283 GstOssSink *osssink = GST_OSSSINK (data);
287 if (!GST_OSSELEMENT (osssink)->bps)
290 delay = gst_osssink_get_delay (osssink);
292 /* sometimes delay is bigger than the number of bytes sent to the device,
293 * which screws up this calculation, we assume that everything is still
294 * in the device then */
295 if (((guint64)delay) > osssink->handled) {
296 delay = osssink->handled;
298 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
304 gst_osssink_get_clock (GstElement *element)
308 osssink = GST_OSSSINK (element);
310 return GST_CLOCK (osssink->provided_clock);
314 gst_osssink_set_clock (GstElement *element, GstClock *clock)
318 osssink = GST_OSSSINK (element);
320 osssink->clock = clock;
324 gst_osssink_chain (GstPad *pad, GstData *_data)
326 GstBuffer *buf = GST_BUFFER (_data);
328 GstClockTime buftime;
330 /* this has to be an audio buffer */
331 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
333 if (GST_IS_EVENT (buf)) {
334 GstEvent *event = GST_EVENT (buf);
336 switch (GST_EVENT_TYPE (event)) {
338 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC);
339 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
340 gst_pad_event_default (pad, event);
342 case GST_EVENT_DISCONTINUOUS:
346 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET);
347 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
348 if (!gst_clock_handle_discont (osssink->clock, value))
349 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
350 osssink->handled = 0;
352 osssink->resync = TRUE;
357 gst_pad_event_default (pad, event);
360 gst_event_unref (event);
364 if (!GST_OSSELEMENT (osssink)->bps) {
365 gst_buffer_unref (buf);
366 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
370 buftime = GST_BUFFER_TIMESTAMP (buf);
372 if (GST_OSSELEMENT (osssink)->fd >= 0) {
373 if (!osssink->mute) {
374 guchar *data = GST_BUFFER_DATA (buf);
375 gint size = GST_BUFFER_SIZE (buf);
378 if (osssink->clock) {
381 GstClockTimeDiff jitter;
383 delay = gst_osssink_get_delay (osssink);
384 queued = delay * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
386 if (osssink->resync && osssink->sync) {
387 GstClockID id = gst_clock_new_single_shot_id (osssink->clock, buftime - queued);
389 gst_element_clock_wait (GST_ELEMENT (osssink), id, &jitter);
390 gst_clock_id_free (id);
393 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
395 gst_audio_clock_set_active ((GstAudioClock*)osssink->provided_clock, TRUE);
396 osssink->resync = FALSE;
403 /* no clock, try to be as fast as possible */
405 audio_buf_info ospace;
407 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &ospace);
409 if (ospace.bytes >= size) {
414 while (to_write > 0) {
415 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
416 MIN (to_write, osssink->chunk_size));
425 osssink->handled += done;
431 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, buftime);
433 gst_buffer_unref (buf);
436 static const GstFormat*
437 gst_osssink_get_formats (GstPad *pad)
439 static const GstFormat formats[] = {
449 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
450 GstFormat *dest_format, gint64 *dest_value)
454 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
456 return gst_osselement_convert (GST_OSSELEMENT (osssink),
457 src_format, src_value,
458 dest_format, dest_value);
461 static const GstQueryType*
462 gst_osssink_get_query_types (GstPad *pad)
464 static const GstQueryType query_types[] = {
473 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
478 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
481 case GST_QUERY_LATENCY:
482 if (!gst_osssink_convert (pad,
483 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
489 case GST_QUERY_POSITION:
490 if (!gst_osssink_convert (pad,
491 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
498 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
506 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
508 GstOssSink *osssink = GST_OSSSINK (element);
510 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
514 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
518 osssink = GST_OSSSINK (object);
522 osssink->mute = g_value_get_boolean (value);
523 g_object_notify (G_OBJECT (osssink), "mute");
526 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
527 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
529 case ARG_BUFFER_SIZE:
530 osssink->bufsize = g_value_get_uint (value);
531 g_object_notify (object, "buffer_size");
534 osssink->sync = g_value_get_boolean (value);
535 g_object_notify (G_OBJECT (osssink), "sync");
538 osssink->chunk_size = g_value_get_uint (value);
541 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
551 osssink = GST_OSSSINK (object);
555 g_value_set_boolean (value, osssink->mute);
558 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
560 case ARG_BUFFER_SIZE:
561 g_value_set_uint (value, osssink->bufsize);
564 g_value_set_boolean (value, osssink->sync);
567 g_value_set_uint (value, osssink->chunk_size);
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
575 static GstElementStateReturn
576 gst_osssink_change_state (GstElement *element)
580 osssink = GST_OSSSINK (element);
582 switch (GST_STATE_TRANSITION (element)) {
583 case GST_STATE_READY_TO_PAUSED:
585 case GST_STATE_PAUSED_TO_PLAYING:
586 osssink->resync = TRUE;
588 case GST_STATE_PLAYING_TO_PAUSED:
589 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
590 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
591 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
592 osssink->resync = TRUE;
594 case GST_STATE_PAUSED_TO_READY:
595 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
596 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
597 gst_osselement_reset (GST_OSSELEMENT (osssink));
603 if (GST_ELEMENT_CLASS (parent_class)->change_state)
604 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
606 return GST_STATE_SUCCESS;