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.
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
28 #include <sys/soundcard.h>
33 #include <gstosssink.h>
34 #include <gstosscommon.h>
36 /* elementfactory information */
37 static GstElementDetails gst_osssink_details = {
41 "Output to a sound card via OSS",
43 "Erik Walthinsen <omega@cse.ogi.edu>, "
44 "Wim Taymans <wim.taymans@chello.be>",
48 static void gst_osssink_class_init (GstOssSinkClass *klass);
49 static void gst_osssink_init (GstOssSink *osssink);
50 static void gst_osssink_finalize (GObject *object);
52 static gboolean gst_osssink_open_audio (GstOssSink *sink);
53 static void gst_osssink_close_audio (GstOssSink *sink);
54 static gboolean gst_osssink_sync_parms (GstOssSink *osssink);
55 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
56 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
57 static GstClock* gst_osssink_get_clock (GstElement *element);
58 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
60 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
61 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
62 GstFormat *dest_format, gint64 *dest_value);
63 static const GstPadQueryType* gst_osssink_get_query_types (GstPad *pad);
64 static gboolean gst_osssink_query (GstElement *element, GstPadQueryType type,
65 GstFormat *format, gint64 *value);
66 static gboolean gst_osssink_sink_query (GstPad *pad, GstPadQueryType type,
67 GstFormat *format, gint64 *value);
69 static GstPadConnectReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
71 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
73 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
76 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
78 /* OssSink signals and args */
94 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
101 "format", GST_PROPS_STRING ("int"), /* hack */
102 "law", GST_PROPS_INT (0),
103 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
104 "signed", GST_PROPS_LIST (
105 GST_PROPS_BOOLEAN (FALSE),
106 GST_PROPS_BOOLEAN (TRUE)
108 "width", GST_PROPS_LIST (
112 "depth", GST_PROPS_LIST (
116 "rate", GST_PROPS_INT_RANGE (1000, 48000),
117 "channels", GST_PROPS_INT_RANGE (1, 2)
121 static GstElementClass *parent_class = NULL;
122 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
125 gst_osssink_get_type (void)
127 static GType osssink_type = 0;
130 static const GTypeInfo osssink_info = {
131 sizeof(GstOssSinkClass),
134 (GClassInitFunc)gst_osssink_class_init,
139 (GInstanceInitFunc)gst_osssink_init,
141 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
147 static GstBufferPool*
148 gst_osssink_get_bufferpool (GstPad *pad)
152 oss = GST_OSSSINK (gst_pad_get_parent(pad));
154 /* 6 buffers per chunk by default */
156 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
158 return oss->sinkpool;
162 gst_osssink_finalize (GObject *object)
164 GstOssSink *osssink = (GstOssSink *) object;
166 g_free (osssink->device);
168 G_OBJECT_CLASS (parent_class)->finalize (object);
172 gst_osssink_class_init (GstOssSinkClass *klass)
174 GObjectClass *gobject_class;
175 GstElementClass *gstelement_class;
177 gobject_class = (GObjectClass*)klass;
178 gstelement_class = (GstElementClass*)klass;
180 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
182 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
183 g_param_spec_string ("device", "Device", "The device to use for output",
184 "/dev/dsp", G_PARAM_READWRITE)); /* CHECKME! */
185 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
186 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
187 TRUE, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
189 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
190 TRUE, G_PARAM_READWRITE));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
192 g_param_spec_int ("fragment", "Fragment",
193 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
194 0, G_MAXINT, 6, G_PARAM_READWRITE));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
196 g_param_spec_int ("buffer_size", "Buffer size", "The buffer size",
197 0, G_MAXINT, 4096, G_PARAM_READWRITE));
199 gst_osssink_signals[SIGNAL_HANDOFF] =
200 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
201 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
202 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
204 gobject_class->set_property = gst_osssink_set_property;
205 gobject_class->get_property = gst_osssink_get_property;
206 gobject_class->finalize = gst_osssink_finalize;
208 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
209 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
210 gstelement_class->set_clock = gst_osssink_set_clock;
211 gstelement_class->get_clock = gst_osssink_get_clock;
216 gst_osssink_init (GstOssSink *osssink)
218 osssink->sinkpad = gst_pad_new_from_template (
219 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
220 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
221 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
222 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
223 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
224 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
225 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
226 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
228 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
230 osssink->device = g_strdup ("/dev/dsp");
232 osssink->channels = 1;
233 osssink->frequency = 11025;
234 osssink->fragment = 6;
235 /* AFMT_*_BE not available on all OSS includes (e.g. FBSD) */
236 #ifdef WORDS_BIGENDIAN
237 osssink->format = AFMT_S16_BE;
239 osssink->format = AFMT_S16_LE;
240 #endif /* WORDS_BIGENDIAN */
241 osssink->bufsize = 4096;
243 osssink->resync = FALSE;
244 osssink->sync = TRUE;
245 osssink->sinkpool = NULL;
246 osssink->provided_clock = GST_CLOCK (gst_oss_clock_new ("ossclock", gst_osssink_get_time, osssink));
247 osssink->handled = 0;
249 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
250 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
254 static GstPadConnectReturn
255 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
257 gint law, endianness, width, depth, bps;
260 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
262 if (!GST_CAPS_IS_FIXED (caps))
263 return GST_PAD_CONNECT_DELAYED;
265 gst_caps_get_int (caps, "width", &width);
266 gst_caps_get_int (caps, "depth", &depth);
269 return GST_PAD_CONNECT_REFUSED;
271 osssink->width = width;
273 /* laws 1 and 2 are 1 bps anyway */
276 gst_caps_get_int (caps, "law", &law);
277 gst_caps_get_int (caps, "endianness", &endianness);
278 gst_caps_get_boolean (caps, "signed", &sign);
280 if (!gst_ossformat_get (law, endianness, sign,
281 width, depth, &format, &bps))
283 GST_DEBUG (GST_CAT_PLUGIN_INFO, "could not get format");
284 return GST_PAD_CONNECT_REFUSED;
288 osssink->format = format;
290 gst_caps_get_int (caps, "channels", &osssink->channels);
291 gst_caps_get_int (caps, "rate", &osssink->frequency);
293 osssink->bps *= osssink->channels;
294 osssink->bps *= osssink->frequency;
296 if (!gst_osssink_sync_parms (osssink)) {
297 return GST_PAD_CONNECT_REFUSED;
300 return GST_PAD_CONNECT_OK;
304 gst_osssink_sync_parms (GstOssSink *osssink)
306 audio_buf_info ospace;
309 gint target_channels;
310 gint target_frequency;
312 gint fragscale, frag_ln;
314 g_return_val_if_fail (osssink != NULL, FALSE);
315 g_return_val_if_fail (GST_IS_OSSSINK (osssink), FALSE);
317 if (osssink->fd == -1)
320 if (osssink->fragment >> 16)
321 frag = osssink->fragment;
323 frag = 0x7FFF0000 | osssink->fragment;
325 GST_INFO (GST_CAT_PLUGIN_INFO,
326 "osssink: setting sound card to %dHz %d format %s (%08x fragment)",
327 osssink->frequency, osssink->format,
328 (osssink->channels == 2) ? "stereo" : "mono", frag);
330 ioctl (osssink->fd, SNDCTL_DSP_SETFRAGMENT, &frag);
332 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
334 target_format = osssink->format;
335 target_channels = osssink->channels;
336 target_frequency = osssink->frequency;
338 ioctl (osssink->fd, SNDCTL_DSP_SETFMT, &osssink->format);
339 ioctl (osssink->fd, SNDCTL_DSP_CHANNELS, &osssink->channels);
340 ioctl (osssink->fd, SNDCTL_DSP_SPEED, &osssink->frequency);
342 ioctl (osssink->fd, SNDCTL_DSP_GETBLKSIZE, &osssink->fragment_size);
343 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
345 /* calculate new fragment using a poor man's logarithm function */
348 while (fragscale < ospace.fragsize) {
352 osssink->fragment = ospace.fragstotal << 16 | frag_ln;
354 GST_INFO (GST_CAT_PLUGIN_INFO,
355 "osssink: set sound card to %dHz %d format %s "
356 "(%d bytes buffer, %08x fragment)",
357 osssink->frequency, osssink->format,
358 (osssink->channels == 2) ? "stereo" : "mono",
359 ospace.bytes, osssink->fragment);
361 object = G_OBJECT (osssink);
362 g_object_freeze_notify (object);
363 g_object_notify (object, "fragment");
364 g_object_thaw_notify (object);
366 osssink->fragment_time = (GST_SECOND * osssink->fragment_size) / osssink->bps;
367 GST_INFO (GST_CAT_PLUGIN_INFO, "fragment time %u %llu\n",
368 osssink->bps, osssink->fragment_time);
370 if (target_format != osssink->format ||
371 target_channels != osssink->channels ||
372 target_frequency != osssink->frequency)
374 g_warning ("couldn't set requested OSS parameters, enjoy the noise :)");
375 /* we could eventually return FALSE here, or just do some additional tests
376 * to see that the frequencies don't differ too much etc.. */
382 gst_osssink_get_delay (GstOssSink *osssink)
386 if (osssink->fd == -1)
389 if (ioctl (osssink->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
391 if (ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
395 delay = (info.fragstotal * info.fragsize) - info.bytes;
402 gst_osssink_get_time (GstClock *clock, gpointer data)
404 GstOssSink *osssink = GST_OSSSINK (data);
411 delay = gst_osssink_get_delay (osssink);
413 /* sometimes delay is bigger than the number of bytes sent to the device,
414 * which screws up this calculation, we assume that everything is still
415 * in the device then */
416 if (((guint64)delay) > osssink->handled) {
417 delay = osssink->handled;
419 res = (osssink->handled - delay) * GST_SECOND / osssink->bps;
425 gst_osssink_get_clock (GstElement *element)
429 osssink = GST_OSSSINK (element);
431 return GST_CLOCK (osssink->provided_clock);
435 gst_osssink_set_clock (GstElement *element, GstClock *clock)
439 osssink = GST_OSSSINK (element);
441 osssink->clock = clock;
445 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
448 GstClockTime buftime;
450 /* this has to be an audio buffer */
451 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
453 if (GST_IS_EVENT (buf)) {
454 GstEvent *event = GST_EVENT (buf);
456 switch (GST_EVENT_TYPE (event)) {
458 ioctl (osssink->fd, SNDCTL_DSP_SYNC);
459 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
460 gst_pad_event_default (pad, event);
462 case GST_EVENT_NEW_MEDIA:
463 g_print ("new media\n");
465 case GST_EVENT_DISCONTINUOUS:
469 ioctl (osssink->fd, SNDCTL_DSP_RESET);
470 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
471 if (!gst_clock_handle_discont (osssink->clock, value))
472 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
473 osssink->handled = 0;
475 osssink->resync = TRUE;
479 gst_pad_event_default (pad, event);
482 gst_event_unref (event);
487 gst_buffer_unref (buf);
488 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
492 buftime = GST_BUFFER_TIMESTAMP (buf);
494 if (osssink->fd >= 0) {
495 if (!osssink->mute) {
496 guchar *data = GST_BUFFER_DATA (buf);
497 gint size = GST_BUFFER_SIZE (buf);
499 if (osssink->clock) {
502 GstClockTimeDiff jitter;
504 delay = gst_osssink_get_delay (osssink);
505 queued = delay * GST_SECOND / osssink->bps;
507 if (osssink->resync && osssink->sync) {
508 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock,
509 buftime - queued, &jitter);
512 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
513 write (osssink->fd, data, size);
514 gst_oss_clock_set_active (osssink->provided_clock, TRUE);
515 osssink->resync = FALSE;
516 osssink->handled += size;
520 write (osssink->fd, data, size);
521 osssink->handled += size;
524 /* no clock, try to be as fast as possible */
526 audio_buf_info ospace;
528 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
530 if (ospace.bytes >= size) {
531 write (osssink->fd, data, size);
536 gst_buffer_unref (buf);
539 static const GstFormat*
540 gst_osssink_get_formats (GstPad *pad)
542 static const GstFormat formats[] = {
552 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
553 GstFormat *dest_format, gint64 *dest_value)
559 if (src_format == *dest_format) {
560 *dest_value = src_value;
564 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
566 if (osssink->bps == 0 || osssink->channels == 0 || osssink->width == 0)
569 switch (src_format) {
570 case GST_FORMAT_BYTES:
571 switch (*dest_format) {
572 case GST_FORMAT_DEFAULT:
573 *dest_format = GST_FORMAT_TIME;
574 case GST_FORMAT_TIME:
575 *dest_value = src_value * GST_SECOND / osssink->bps;
577 case GST_FORMAT_UNITS:
578 *dest_value = src_value / (osssink->channels * osssink->width);
584 case GST_FORMAT_TIME:
585 switch (*dest_format) {
586 case GST_FORMAT_DEFAULT:
587 *dest_format = GST_FORMAT_BYTES;
588 case GST_FORMAT_BYTES:
589 *dest_value = src_value * osssink->bps / GST_SECOND;
591 case GST_FORMAT_UNITS:
592 *dest_value = src_value * osssink->frequency / GST_SECOND;
598 case GST_FORMAT_UNITS:
599 switch (*dest_format) {
600 case GST_FORMAT_DEFAULT:
601 *dest_format = GST_FORMAT_TIME;
602 case GST_FORMAT_TIME:
603 *dest_value = src_value * GST_SECOND / osssink->frequency;
605 case GST_FORMAT_BYTES:
606 *dest_value = src_value * osssink->channels * osssink->width;
619 static const GstPadQueryType*
620 gst_osssink_get_query_types (GstPad *pad)
622 static const GstPadQueryType query_types[] = {
623 GST_PAD_QUERY_LATENCY,
624 GST_PAD_QUERY_POSITION,
631 gst_osssink_sink_query (GstPad *pad, GstPadQueryType type, GstFormat *format, gint64 *value)
636 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
639 case GST_PAD_QUERY_LATENCY:
640 if (!gst_osssink_convert (pad,
641 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
647 case GST_PAD_QUERY_POSITION:
648 if (!gst_osssink_convert (pad,
649 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
656 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
664 gst_osssink_query (GstElement *element, GstPadQueryType type, GstFormat *format, gint64 *value)
666 GstOssSink *osssink = GST_OSSSINK (element);
668 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
672 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
676 /* it's not null if we got it, but it might not be ours */
677 g_return_if_fail (GST_IS_OSSSINK (object));
679 osssink = GST_OSSSINK (object);
683 /* disallow changing the device while it is opened
684 get_property("device") should return the right one */
685 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
687 g_free (osssink->device);
688 osssink->device = g_strdup (g_value_get_string (value));
689 g_object_notify (object, "device");
693 osssink->mute = g_value_get_boolean (value);
694 g_object_notify (G_OBJECT (osssink), "mute");
697 osssink->fragment = g_value_get_int (value);
698 gst_osssink_sync_parms (osssink);
700 case ARG_BUFFER_SIZE:
701 if (osssink->bufsize == g_value_get_int (value)) break;
702 osssink->bufsize = g_value_get_int (value);
703 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
704 g_object_notify (object, "buffer_size");
707 osssink->sync = g_value_get_boolean (value);
708 g_object_notify (G_OBJECT (osssink), "sync");
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
717 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
721 /* it's not null if we got it, but it might not be ours */
722 g_return_if_fail (GST_IS_OSSSINK (object));
724 osssink = GST_OSSSINK (object);
728 g_value_set_string (value, osssink->device);
731 g_value_set_boolean (value, osssink->mute);
734 g_value_set_int (value, osssink->fragment);
736 case ARG_BUFFER_SIZE:
737 g_value_set_int (value, osssink->bufsize);
740 g_value_set_boolean (value, osssink->sync);
743 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
749 gst_osssink_open_audio (GstOssSink *sink)
752 g_return_val_if_fail (sink->fd == -1, FALSE);
754 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: attempting to open sound device");
756 /* first try to open the sound card */
757 /* FIXME: this code is dubious, why do we need to open and close this ?*/
758 sink->fd = open (sink->device, O_WRONLY | O_NONBLOCK);
759 if (errno == EBUSY) {
760 g_warning ("osssink: unable to open the sound device (in use ?)\n");
766 /* re-open the sound device in blocking mode */
767 sink->fd = open (sink->device, O_WRONLY);
772 gst_element_error (GST_ELEMENT (sink),
773 "osssink: Device %s is a directory",
778 gst_element_error (GST_ELEMENT (sink),
779 "osssink: Cannot access %s, check permissions",
785 gst_element_error (GST_ELEMENT (sink),
786 "osssink: Cannot access %s, does it exist ?",
790 gst_element_error (GST_ELEMENT (sink),
791 "osssink: Cannot access %s, read-only filesystem ?",
794 /* FIXME: strerror is not threadsafe */
795 gst_element_error (GST_ELEMENT (sink),
796 "osssink: Cannot open %s, generic error: %s",
797 sink->device, strerror (errno));
802 /* we have it, set the default parameters and go have fun */
804 ioctl (sink->fd, SNDCTL_DSP_GETCAPS, &caps);
806 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Capabilities %08x", caps);
808 if (caps & DSP_CAP_DUPLEX) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Full duplex");
809 if (caps & DSP_CAP_REALTIME) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Realtime");
810 if (caps & DSP_CAP_BATCH) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Batch");
811 if (caps & DSP_CAP_COPROC) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Has coprocessor");
812 if (caps & DSP_CAP_TRIGGER) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Trigger");
813 if (caps & DSP_CAP_MMAP) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Direct access");
816 if (caps & DSP_CAP_MULTI) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Multiple open");
817 #endif /* DSP_CAP_MULTI */
820 if (caps & DSP_CAP_BIND) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Channel binding");
821 #endif /* DSP_CAP_BIND */
823 ioctl(sink->fd, SNDCTL_DSP_GETFMTS, &caps);
825 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Formats %08x", caps);
826 if (caps & AFMT_MU_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MU_LAW");
827 if (caps & AFMT_A_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: A_LAW");
828 if (caps & AFMT_IMA_ADPCM) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: IMA_ADPCM");
829 if (caps & AFMT_U8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U8");
830 if (caps & AFMT_S16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_LE");
831 if (caps & AFMT_S16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_BE");
832 if (caps & AFMT_S8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S8");
833 if (caps & AFMT_U16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_LE");
834 if (caps & AFMT_U16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_BE");
835 if (caps & AFMT_MPEG) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MPEG");
837 if (caps & AFMT_AC3) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: AC3");
840 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: opened audio (%s) with fd=%d", sink->device, sink->fd);
841 GST_FLAG_SET (sink, GST_OSSSINK_OPEN);
847 gst_osssink_close_audio (GstOssSink *sink)
849 if (sink->fd < 0) return;
854 GST_FLAG_UNSET (sink, GST_OSSSINK_OPEN);
856 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
859 static GstElementStateReturn
860 gst_osssink_change_state (GstElement *element)
864 g_return_val_if_fail (GST_IS_OSSSINK (element), FALSE);
866 osssink = GST_OSSSINK (element);
868 switch (GST_STATE_TRANSITION (element)) {
869 case GST_STATE_NULL_TO_READY:
870 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
871 if (!gst_osssink_open_audio (osssink)) {
872 return GST_STATE_FAILURE;
876 case GST_STATE_READY_TO_PAUSED:
878 case GST_STATE_PAUSED_TO_PLAYING:
879 osssink->resync = TRUE;
881 case GST_STATE_PLAYING_TO_PAUSED:
883 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
884 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
885 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
886 osssink->resync = TRUE;
889 case GST_STATE_PAUSED_TO_READY:
890 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
891 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
893 case GST_STATE_READY_TO_NULL:
894 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
895 gst_osssink_close_audio (osssink);
899 if (GST_ELEMENT_CLASS (parent_class)->change_state)
900 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
902 return GST_STATE_SUCCESS;
906 gst_osssink_factory_init (GstPlugin *plugin)
908 GstElementFactory *factory;
910 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
911 g_return_val_if_fail (factory != NULL, FALSE);
913 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
915 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));