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>
32 #include <gstosssink.h>
34 static GstElementDetails gst_osssink_details = {
37 "Output to a sound card via OSS",
39 "Erik Walthinsen <omega@cse.ogi.edu>, "
40 "Wim Taymans <wim.taymans@chello.be>",
44 static void gst_osssink_class_init (GstOssSinkClass *klass);
45 static void gst_osssink_init (GstOssSink *osssink);
46 static void gst_osssink_finalize (GObject *object);
48 static gboolean gst_osssink_open_audio (GstOssSink *sink);
49 static void gst_osssink_close_audio (GstOssSink *sink);
50 static gboolean gst_osssink_sync_parms (GstOssSink *osssink);
51 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
52 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
53 static GstClock* gst_osssink_get_clock (GstElement *element);
54 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
56 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
57 GstFormat *dest_format, gint64 *dest_value);
58 static gboolean gst_osssink_query (GstPad *pad, GstPadQueryType type,
59 GstFormat *format, gint64 *value);
61 static GstPadConnectReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
63 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
65 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
68 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
70 /* OssSink signals and args */
89 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
96 "format", GST_PROPS_STRING ("int"), /* hack */
97 "law", GST_PROPS_INT (0),
98 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
99 "signed", GST_PROPS_LIST (
100 GST_PROPS_BOOLEAN (FALSE),
101 GST_PROPS_BOOLEAN (TRUE)
103 "width", GST_PROPS_LIST (
107 "depth", GST_PROPS_LIST (
111 "rate", GST_PROPS_INT_RANGE (1000, 48000),
112 "channels", GST_PROPS_INT_RANGE (1, 2)
116 #define GST_TYPE_OSSSINK_CHANNELS (gst_osssink_channels_get_type())
118 gst_osssink_channels_get_type(void) {
119 static GType osssink_channels_type = 0;
120 static GEnumValue osssink_channels[] = {
126 if (!osssink_channels_type) {
127 osssink_channels_type = g_enum_register_static("GstAudiosinkChannels", osssink_channels);
129 return osssink_channels_type;
133 static GstElementClass *parent_class = NULL;
134 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
137 gst_osssink_get_type (void)
139 static GType osssink_type = 0;
142 static const GTypeInfo osssink_info = {
143 sizeof(GstOssSinkClass),
146 (GClassInitFunc)gst_osssink_class_init,
151 (GInstanceInitFunc)gst_osssink_init,
153 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
159 static GstBufferPool*
160 gst_osssink_get_bufferpool (GstPad *pad)
164 oss = GST_OSSSINK (gst_pad_get_parent(pad));
166 return oss->sinkpool;
170 gst_osssink_finalize (GObject *object)
172 GstOssSink *osssink = (GstOssSink *) object;
174 g_free (osssink->device);
176 G_OBJECT_CLASS (parent_class)->finalize (object);
180 gst_osssink_class_init (GstOssSinkClass *klass)
182 GObjectClass *gobject_class;
183 GstElementClass *gstelement_class;
185 gobject_class = (GObjectClass*)klass;
186 gstelement_class = (GstElementClass*)klass;
188 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
190 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE,
191 g_param_spec_string("device","device","device",
192 "/dev/dsp",G_PARAM_READWRITE)); /* CHECKME! */
193 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
194 g_param_spec_boolean("mute","mute","mute",
195 TRUE,G_PARAM_READWRITE));
196 g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_SYNC,
197 g_param_spec_boolean("sync","Sync","If syncing on timestamps should be enabled",
198 TRUE, G_PARAM_READWRITE));
200 /* it would be nice to show format in symbolic form, oh well */
201 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FORMAT,
202 g_param_spec_int ("format","format","format",
203 0, G_MAXINT, AFMT_S16_LE, G_PARAM_READWRITE));
205 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNELS,
206 g_param_spec_enum("channels","channels","channels",
207 GST_TYPE_OSSSINK_CHANNELS,2,G_PARAM_READWRITE));
208 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FREQUENCY,
209 g_param_spec_int("frequency","frequency","frequency",
210 0,G_MAXINT,44100,G_PARAM_READWRITE));
211 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAGMENT,
212 g_param_spec_int("fragment","fragment","fragment",
213 0,G_MAXINT,6,G_PARAM_READWRITE));
214 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFER_SIZE,
215 g_param_spec_int("buffer_size","buffer_size","buffer_size",
216 0,G_MAXINT,4096,G_PARAM_READWRITE));
218 gst_osssink_signals[SIGNAL_HANDOFF] =
219 g_signal_new("handoff",G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
220 G_STRUCT_OFFSET(GstOssSinkClass,handoff), NULL, NULL,
221 g_cclosure_marshal_VOID__VOID,G_TYPE_NONE,0);
223 gobject_class->set_property = gst_osssink_set_property;
224 gobject_class->get_property = gst_osssink_get_property;
225 gobject_class->finalize = gst_osssink_finalize;
227 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
231 gst_osssink_init (GstOssSink *osssink)
233 osssink->sinkpad = gst_pad_new_from_template (
234 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
235 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
236 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
237 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
238 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
239 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_query);
241 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
243 osssink->device = g_strdup ("/dev/dsp");
245 osssink->channels = 1;
246 osssink->frequency = 11025;
247 osssink->fragment = 6;
248 /* AFMT_*_BE not available on all OSS includes (e.g. FBSD) */
249 #ifdef WORDS_BIGENDIAN
250 osssink->format = AFMT_S16_BE;
252 osssink->format = AFMT_S16_LE;
253 #endif /* WORDS_BIGENDIAN */
254 osssink->bufsize = 4096;
256 osssink->resync = FALSE;
257 osssink->sync = TRUE;
258 /* 6 buffers per chunk by default */
259 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
260 osssink->provided_clock = GST_CLOCK (gst_oss_clock_new ("ossclock", gst_osssink_get_time, osssink));
261 osssink->handled = 0;
263 GST_ELEMENT (osssink)->setclockfunc = gst_osssink_set_clock;
264 GST_ELEMENT (osssink)->getclockfunc = gst_osssink_get_clock;
266 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
267 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
271 static GstPadConnectReturn
272 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
274 gint law, endianness, depth;
277 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
279 if (!GST_CAPS_IS_FIXED (caps))
280 return GST_PAD_CONNECT_DELAYED;
282 gst_caps_get_int (caps, "width", &osssink->width);
283 gst_caps_get_int (caps, "depth", &depth);
285 if (osssink->width != depth)
286 return GST_PAD_CONNECT_REFUSED;
288 /* laws 1 and 2 are 1 bps anyway */
291 gst_caps_get_int (caps, "law", &law);
292 gst_caps_get_int (caps, "endianness", &endianness);
293 gst_caps_get_boolean (caps, "signed", &sign);
296 if (osssink->width == 16) {
298 if (endianness == G_LITTLE_ENDIAN)
299 format = AFMT_S16_LE;
300 else if (endianness == G_BIG_ENDIAN)
301 format = AFMT_S16_BE;
304 if (endianness == G_LITTLE_ENDIAN)
305 format = AFMT_U16_LE;
306 else if (endianness == G_BIG_ENDIAN)
307 format = AFMT_U16_BE;
311 else if (osssink->width == 8) {
320 } else if (law == 1) {
321 format = AFMT_MU_LAW;
322 } else if (law == 2) {
325 g_critical ("unknown law");
326 return GST_PAD_CONNECT_REFUSED;
330 return GST_PAD_CONNECT_REFUSED;
332 osssink->format = format;
333 gst_caps_get_int (caps, "channels", &osssink->channels);
334 gst_caps_get_int (caps, "rate", &osssink->frequency);
336 osssink->bps *= osssink->channels;
337 osssink->bps *= osssink->frequency;
339 if (!gst_osssink_sync_parms (osssink)) {
340 return GST_PAD_CONNECT_REFUSED;
343 return GST_PAD_CONNECT_OK;
347 gst_osssink_sync_parms (GstOssSink *osssink)
349 audio_buf_info ospace;
352 gint target_channels;
353 gint target_frequency;
356 g_return_val_if_fail (osssink != NULL, FALSE);
357 g_return_val_if_fail (GST_IS_OSSSINK (osssink), FALSE);
359 if (osssink->fd == -1)
362 if (osssink->fragment >> 16)
363 frag = osssink->fragment;
365 frag = 0x7FFF0000 | osssink->fragment;
367 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: trying to set sound card to %dHz %d bit %s (%08x fragment)",
368 osssink->frequency, osssink->format,
369 (osssink->channels == 2) ? "stereo" : "mono",frag);
371 ioctl (osssink->fd, SNDCTL_DSP_SETFRAGMENT, &frag);
373 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
375 target_format = osssink->format;
376 target_channels = osssink->channels;
377 target_frequency = osssink->frequency;
379 ioctl (osssink->fd, SNDCTL_DSP_SETFMT, &osssink->format);
380 ioctl (osssink->fd, SNDCTL_DSP_CHANNELS, &osssink->channels);
381 ioctl (osssink->fd, SNDCTL_DSP_SPEED, &osssink->frequency);
383 ioctl (osssink->fd, SNDCTL_DSP_GETBLKSIZE, &osssink->fragment);
384 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
386 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: set sound card to %dHz %d bit %s (%d bytes buffer, %08x fragment)",
387 osssink->frequency, osssink->format,
388 (osssink->channels == 2) ? "stereo" : "mono", ospace.bytes, osssink->fragment);
390 object = G_OBJECT (osssink);
391 g_object_freeze_notify (object);
392 g_object_notify (object, "channels");
393 g_object_notify (object, "frequency");
394 g_object_notify (object, "fragment");
395 g_object_notify (object, "format");
396 g_object_thaw_notify (object);
398 osssink->fragment_time = (1000000 * osssink->fragment) / osssink->bps;
399 GST_INFO (GST_CAT_PLUGIN_INFO, "fragment time %u %llu\n", osssink->bps, osssink->fragment_time);
401 if (target_format != osssink->format ||
402 target_channels != osssink->channels ||
403 target_frequency != osssink->frequency)
405 g_warning ("could not configure oss with required parameters, enjoy the noise :)");
406 /* we could eventually return FALSE here, or just do some additional tests
407 * to see that the frequencies don't differ too much etc.. */
413 gst_osssink_get_delay (GstOssSink *osssink)
417 if (osssink->fd == -1)
420 if (ioctl (osssink->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
422 if (ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
426 delay = (info.fragstotal * info.fragsize) - info.bytes;
433 gst_osssink_get_time (GstClock *clock, gpointer data)
435 GstOssSink *osssink = GST_OSSSINK (data);
442 delay = gst_osssink_get_delay (osssink);
444 /* sometimes delay is bigger than the number of bytes sent to the device, which screws
445 * up this calculation, we assume that everything is still in the device then */
446 if (((guint64)delay) > osssink->handled) {
447 delay = osssink->handled;
449 res = (osssink->handled - delay) * GST_SECOND / osssink->bps;
452 g_print ("from osssink: %lld %d %lld %d\n", res, delay, osssink->handled, osssink->bps);
459 gst_osssink_get_clock (GstElement *element)
463 osssink = GST_OSSSINK (element);
466 return GST_CLOCK (osssink->provided_clock);
470 gst_osssink_set_clock (GstElement *element, GstClock *clock)
474 osssink = GST_OSSSINK (element);
476 osssink->clock = clock;
480 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
483 GstClockTime buftime;
485 /* this has to be an audio buffer */
486 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
488 if (GST_IS_EVENT (buf)) {
489 GstEvent *event = GST_EVENT (buf);
491 switch (GST_EVENT_TYPE (event)) {
493 ioctl (osssink->fd, SNDCTL_DSP_SYNC);
494 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
495 gst_pad_event_default (pad, event);
497 case GST_EVENT_NEW_MEDIA:
498 g_print ("new media\n");
500 case GST_EVENT_DISCONTINUOUS:
504 ioctl (osssink->fd, SNDCTL_DSP_RESET);
505 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
506 if (!gst_clock_handle_discont (osssink->clock, value))
507 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
508 osssink->handled = 0;
510 osssink->resync = TRUE;
514 gst_pad_event_default (pad, event);
517 gst_event_free (event);
521 gst_buffer_unref (buf);
522 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
526 buftime = GST_BUFFER_TIMESTAMP (buf);
528 if (osssink->fd >= 0) {
529 if (!osssink->mute) {
530 guchar *data = GST_BUFFER_DATA (buf);
531 gint size = GST_BUFFER_SIZE (buf);
533 if (osssink->clock) {
536 GstClockTimeDiff jitter;
538 delay = gst_osssink_get_delay (osssink);
539 queued = delay * GST_SECOND / osssink->bps;
541 if (osssink->resync && osssink->sync) {
542 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock,
543 buftime - queued, &jitter);
546 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
547 write (osssink->fd, data, size);
548 gst_oss_clock_set_active (osssink->provided_clock, TRUE);
549 osssink->resync = FALSE;
550 osssink->handled += size;
554 write (osssink->fd, data, size);
555 osssink->handled += size;
558 /* no clock, try to be as fast as possible */
560 audio_buf_info ospace;
562 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
564 if (ospace.bytes >= size) {
565 write (osssink->fd, data, size);
570 gst_buffer_unref (buf);
574 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
575 GstFormat *dest_format, gint64 *dest_value)
581 if (src_format == *dest_format) {
582 *dest_value = src_value;
586 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
588 if (osssink->bps == 0 || osssink->channels == 0 || osssink->width == 0)
591 switch (src_format) {
592 case GST_FORMAT_BYTES:
593 switch (*dest_format) {
594 case GST_FORMAT_DEFAULT:
595 *dest_format = GST_FORMAT_TIME;
596 case GST_FORMAT_TIME:
597 *dest_value = src_value * GST_SECOND / osssink->bps;
599 case GST_FORMAT_UNIT:
600 *dest_value = src_value / (osssink->channels * osssink->width);
606 case GST_FORMAT_TIME:
607 switch (*dest_format) {
608 case GST_FORMAT_DEFAULT:
609 *dest_format = GST_FORMAT_BYTES;
610 case GST_FORMAT_BYTES:
611 *dest_value = src_value * osssink->bps / GST_SECOND;
613 case GST_FORMAT_UNIT:
614 *dest_value = osssink->frequency;
620 case GST_FORMAT_UNIT:
621 switch (*dest_format) {
622 case GST_FORMAT_DEFAULT:
623 *dest_format = GST_FORMAT_TIME;
624 case GST_FORMAT_TIME:
625 *dest_value = src_value * GST_SECOND / osssink->frequency;
627 case GST_FORMAT_BYTES:
628 *dest_value = src_value * osssink->channels * osssink->width;
642 gst_osssink_query (GstPad *pad, GstPadQueryType type, GstFormat *format, gint64 *value)
647 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
650 case GST_PAD_QUERY_LATENCY:
651 if (!gst_osssink_convert (pad,
652 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
667 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
671 /* it's not null if we got it, but it might not be ours */
672 g_return_if_fail (GST_IS_OSSSINK (object));
674 osssink = GST_OSSSINK (object);
678 /* disallow changing the device while it is opened
679 get_property("device") should return the right one */
680 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
682 g_free (osssink->device);
683 osssink->device = g_strdup (g_value_get_string (value));
684 g_object_notify (object, "device");
688 osssink->mute = g_value_get_boolean (value);
689 g_object_notify (G_OBJECT (osssink), "mute");
692 osssink->format = g_value_get_int (value);
693 gst_osssink_sync_parms (osssink);
696 osssink->channels = g_value_get_enum (value);
697 gst_osssink_sync_parms (osssink);
700 osssink->frequency = g_value_get_int (value);
701 gst_osssink_sync_parms (osssink);
704 osssink->fragment = g_value_get_int (value);
705 gst_osssink_sync_parms (osssink);
707 case ARG_BUFFER_SIZE:
708 if (osssink->bufsize == g_value_get_int (value)) break;
709 osssink->bufsize = g_value_get_int (value);
710 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
711 g_object_notify (object, "buffer_size");
714 osssink->sync = g_value_get_boolean (value);
715 g_object_notify (G_OBJECT (osssink), "sync");
722 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
726 /* it's not null if we got it, but it might not be ours */
727 g_return_if_fail (GST_IS_OSSSINK (object));
729 osssink = GST_OSSSINK (object);
733 g_value_set_string (value, osssink->device);
736 g_value_set_boolean (value, osssink->mute);
739 g_value_set_int (value, osssink->format);
742 g_value_set_enum (value, osssink->channels);
745 g_value_set_int (value, osssink->frequency);
748 g_value_set_int (value, osssink->fragment);
750 case ARG_BUFFER_SIZE:
751 g_value_set_int (value, osssink->bufsize);
754 g_value_set_boolean (value, osssink->sync);
762 gst_osssink_open_audio (GstOssSink *sink)
765 g_return_val_if_fail (sink->fd == -1, FALSE);
767 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: attempting to open sound device");
769 /* first try to open the sound card */
770 sink->fd = open(sink->device, O_WRONLY | O_NONBLOCK);
771 if (errno == EBUSY) {
772 g_warning ("osssink: unable to open the sound device (in use ?)\n");
776 /* re-open the sound device in blocking mode */
778 sink->fd = open(sink->device, O_WRONLY);
780 /* if we have it, set the default parameters and go have fun */
783 ioctl(sink->fd, SNDCTL_DSP_GETCAPS, &caps);
785 GST_INFO(GST_CAT_PLUGIN_INFO, "osssink: Capabilities %08x", caps);
787 if (caps & DSP_CAP_DUPLEX) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Full duplex");
788 if (caps & DSP_CAP_REALTIME) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Realtime");
789 if (caps & DSP_CAP_BATCH) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Batch");
790 if (caps & DSP_CAP_COPROC) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Has coprocessor");
791 if (caps & DSP_CAP_TRIGGER) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Trigger");
792 if (caps & DSP_CAP_MMAP) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Direct access");
795 if (caps & DSP_CAP_MULTI) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Multiple open");
796 #endif /* DSP_CAP_MULTI */
799 if (caps & DSP_CAP_BIND) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Channel binding");
800 #endif /* DSP_CAP_BIND */
802 ioctl(sink->fd, SNDCTL_DSP_GETFMTS, &caps);
804 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Formats %08x", caps);
805 if (caps & AFMT_MU_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MU_LAW");
806 if (caps & AFMT_A_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: A_LAW");
807 if (caps & AFMT_IMA_ADPCM) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: IMA_ADPCM");
808 if (caps & AFMT_U8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U8");
809 if (caps & AFMT_S16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_LE");
810 if (caps & AFMT_S16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_BE");
811 if (caps & AFMT_S8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S8");
812 if (caps & AFMT_U16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_LE");
813 if (caps & AFMT_U16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_BE");
814 if (caps & AFMT_MPEG) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MPEG");
816 if (caps & AFMT_AC3) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: AC3");
819 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: opened audio (%s) with fd=%d", sink->device, sink->fd);
820 GST_FLAG_SET (sink, GST_OSSSINK_OPEN);
829 gst_osssink_close_audio (GstOssSink *sink)
831 if (sink->fd < 0) return;
836 GST_FLAG_UNSET (sink, GST_OSSSINK_OPEN);
838 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
841 static GstElementStateReturn
842 gst_osssink_change_state (GstElement *element)
846 g_return_val_if_fail (GST_IS_OSSSINK (element), FALSE);
848 osssink = GST_OSSSINK (element);
850 switch (GST_STATE_TRANSITION (element)) {
851 case GST_STATE_NULL_TO_READY:
852 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
853 if (!gst_osssink_open_audio (osssink)) {
854 return GST_STATE_FAILURE;
858 case GST_STATE_READY_TO_PAUSED:
859 case GST_STATE_PAUSED_TO_PLAYING:
860 osssink->resync = TRUE;
862 case GST_STATE_PLAYING_TO_PAUSED:
864 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
865 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
866 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
867 osssink->resync = TRUE;
870 case GST_STATE_PAUSED_TO_READY:
871 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
872 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
874 case GST_STATE_READY_TO_NULL:
875 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
876 gst_osssink_close_audio (osssink);
880 if (GST_ELEMENT_CLASS (parent_class)->change_state)
881 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
883 return GST_STATE_SUCCESS;
887 gst_osssink_factory_init (GstPlugin *plugin)
889 GstElementFactory *factory;
891 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
892 g_return_val_if_fail (factory != NULL, FALSE);
894 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
896 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));