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 const GstFormat* gst_osssink_get_formats (GstPad *pad);
57 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
58 GstFormat *dest_format, gint64 *dest_value);
59 static const GstPadQueryType* gst_osssink_get_query_types (GstPad *pad);
60 static gboolean gst_osssink_query (GstElement *element, GstPadQueryType type,
61 GstFormat *format, gint64 *value);
62 static gboolean gst_osssink_sink_query (GstPad *pad, GstPadQueryType type,
63 GstFormat *format, gint64 *value);
65 static GstPadConnectReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
67 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
69 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
72 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
74 /* OssSink signals and args */
93 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
100 "format", GST_PROPS_STRING ("int"), /* hack */
101 "law", GST_PROPS_INT (0),
102 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
103 "signed", GST_PROPS_LIST (
104 GST_PROPS_BOOLEAN (FALSE),
105 GST_PROPS_BOOLEAN (TRUE)
107 "width", GST_PROPS_LIST (
111 "depth", GST_PROPS_LIST (
115 "rate", GST_PROPS_INT_RANGE (1000, 48000),
116 "channels", GST_PROPS_INT_RANGE (1, 2)
120 #define GST_TYPE_OSSSINK_CHANNELS (gst_osssink_channels_get_type())
122 gst_osssink_channels_get_type(void) {
123 static GType osssink_channels_type = 0;
124 static GEnumValue osssink_channels[] = {
130 if (!osssink_channels_type) {
131 osssink_channels_type = g_enum_register_static("GstAudiosinkChannels", osssink_channels);
133 return osssink_channels_type;
136 #define GST_TYPE_OSSSINK_FORMAT (gst_osssink_format_get_type())
138 gst_osssink_format_get_type(void) {
139 static GType osssink_format_type = 0;
140 static GEnumValue osssink_format[] = {
141 {AFMT_MU_LAW, G_STRINGIFY(AFMT_MU_LAW), "mulaw"},
142 {AFMT_A_LAW, G_STRINGIFY(AFMT_A_LAW), "alaw"},
143 {AFMT_IMA_ADPCM, G_STRINGIFY(AFMT_IMA_ADPCM), "IMA ADPCM"},
144 {AFMT_U8, G_STRINGIFY(AFMT_U8), "Unsigned 8 bits"},
145 {AFMT_S16_LE, G_STRINGIFY(AFMT_S16_LE), "Signed 16 bits little endian"},
146 {AFMT_S16_BE, G_STRINGIFY(AFMT_S16_BE), "Signed 16 bits big endian"},
147 {AFMT_S8, G_STRINGIFY(AFMT_S8), "Signed 8 bits"},
148 {AFMT_U16_LE, G_STRINGIFY(AFMT_U16_LE), "Unsigned 16 bits little endian"},
149 {AFMT_U16_BE, G_STRINGIFY(AFMT_U16_BE), "Unsigned 16 bits big endian"},
150 {AFMT_MPEG, G_STRINGIFY(AFMT_MPEG), "MPEG"},
151 {AFMT_AC3, G_STRINGIFY(AFMT_AC3), "AC3"},
154 if (!osssink_format_type) {
155 osssink_format_type = g_enum_register_static("GstAudiosinkFormat", osssink_format);
157 return osssink_format_type;
160 static GstElementClass *parent_class = NULL;
161 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
164 gst_osssink_get_type (void)
166 static GType osssink_type = 0;
169 static const GTypeInfo osssink_info = {
170 sizeof(GstOssSinkClass),
173 (GClassInitFunc)gst_osssink_class_init,
178 (GInstanceInitFunc)gst_osssink_init,
180 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
186 static GstBufferPool*
187 gst_osssink_get_bufferpool (GstPad *pad)
191 oss = GST_OSSSINK (gst_pad_get_parent(pad));
193 /* 6 buffers per chunk by default */
195 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
197 return oss->sinkpool;
201 gst_osssink_finalize (GObject *object)
203 GstOssSink *osssink = (GstOssSink *) object;
205 g_free (osssink->device);
207 G_OBJECT_CLASS (parent_class)->finalize (object);
211 gst_osssink_class_init (GstOssSinkClass *klass)
213 GObjectClass *gobject_class;
214 GstElementClass *gstelement_class;
216 gobject_class = (GObjectClass*)klass;
217 gstelement_class = (GstElementClass*)klass;
219 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
221 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
222 g_param_spec_string ("device", "Device", "The device to use for output",
223 "/dev/dsp", G_PARAM_READWRITE)); /* CHECKME! */
224 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
225 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
226 TRUE, G_PARAM_READWRITE));
227 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
228 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
229 TRUE, G_PARAM_READWRITE));
230 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FORMAT,
231 g_param_spec_enum ("format", "Format", "The format the device is configured for",
232 GST_TYPE_OSSSINK_FORMAT, AFMT_S16_LE, G_PARAM_READWRITE));
233 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHANNELS,
234 g_param_spec_enum ("channels", "Channels", "The number of channels used for playback",
235 GST_TYPE_OSSSINK_CHANNELS, 2, G_PARAM_READWRITE));
236 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
237 g_param_spec_int ("frequency", "Frequency", "The frequency of the device",
238 0, 48000, 44100, G_PARAM_READWRITE));
239 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
240 g_param_spec_int ("fragment", "Fragment",
241 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
242 0, G_MAXINT, 6, G_PARAM_READWRITE));
243 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
244 g_param_spec_int ("buffer_size", "Buffer size", "The buffer size",
245 0, G_MAXINT, 4096, G_PARAM_READWRITE));
247 gst_osssink_signals[SIGNAL_HANDOFF] =
248 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
249 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
250 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
252 gobject_class->set_property = gst_osssink_set_property;
253 gobject_class->get_property = gst_osssink_get_property;
254 gobject_class->finalize = gst_osssink_finalize;
256 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
257 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
261 gst_osssink_init (GstOssSink *osssink)
263 osssink->sinkpad = gst_pad_new_from_template (
264 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
265 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
266 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
267 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
268 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
269 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
270 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
271 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
273 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
275 osssink->device = g_strdup ("/dev/dsp");
277 osssink->channels = 1;
278 osssink->frequency = 11025;
279 osssink->fragment = 6;
280 /* AFMT_*_BE not available on all OSS includes (e.g. FBSD) */
281 #ifdef WORDS_BIGENDIAN
282 osssink->format = AFMT_S16_BE;
284 osssink->format = AFMT_S16_LE;
285 #endif /* WORDS_BIGENDIAN */
286 osssink->bufsize = 4096;
288 osssink->resync = FALSE;
289 osssink->sync = TRUE;
290 osssink->sinkpool = NULL;
291 osssink->provided_clock = GST_CLOCK (gst_oss_clock_new ("ossclock", gst_osssink_get_time, osssink));
292 osssink->handled = 0;
294 GST_ELEMENT (osssink)->setclockfunc = gst_osssink_set_clock;
295 GST_ELEMENT (osssink)->getclockfunc = gst_osssink_get_clock;
297 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
298 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
302 static GstPadConnectReturn
303 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
305 gint law, endianness, depth;
308 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
310 if (!GST_CAPS_IS_FIXED (caps))
311 return GST_PAD_CONNECT_DELAYED;
313 gst_caps_get_int (caps, "width", &osssink->width);
314 gst_caps_get_int (caps, "depth", &depth);
316 if (osssink->width != depth)
317 return GST_PAD_CONNECT_REFUSED;
319 /* laws 1 and 2 are 1 bps anyway */
322 gst_caps_get_int (caps, "law", &law);
323 gst_caps_get_int (caps, "endianness", &endianness);
324 gst_caps_get_boolean (caps, "signed", &sign);
327 if (osssink->width == 16) {
329 if (endianness == G_LITTLE_ENDIAN)
330 format = AFMT_S16_LE;
331 else if (endianness == G_BIG_ENDIAN)
332 format = AFMT_S16_BE;
335 if (endianness == G_LITTLE_ENDIAN)
336 format = AFMT_U16_LE;
337 else if (endianness == G_BIG_ENDIAN)
338 format = AFMT_U16_BE;
342 else if (osssink->width == 8) {
351 } else if (law == 1) {
352 format = AFMT_MU_LAW;
353 } else if (law == 2) {
356 g_critical ("unknown law");
357 return GST_PAD_CONNECT_REFUSED;
361 return GST_PAD_CONNECT_REFUSED;
363 osssink->format = format;
364 gst_caps_get_int (caps, "channels", &osssink->channels);
365 gst_caps_get_int (caps, "rate", &osssink->frequency);
367 osssink->bps *= osssink->channels;
368 osssink->bps *= osssink->frequency;
370 if (!gst_osssink_sync_parms (osssink)) {
371 return GST_PAD_CONNECT_REFUSED;
374 return GST_PAD_CONNECT_OK;
378 gst_osssink_sync_parms (GstOssSink *osssink)
380 audio_buf_info ospace;
383 gint target_channels;
384 gint target_frequency;
386 gint fragscale, frag_ln;
388 g_return_val_if_fail (osssink != NULL, FALSE);
389 g_return_val_if_fail (GST_IS_OSSSINK (osssink), FALSE);
391 if (osssink->fd == -1)
394 if (osssink->fragment >> 16)
395 frag = osssink->fragment;
397 frag = 0x7FFF0000 | osssink->fragment;
399 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: trying to set sound card to %dHz %d bit %s (%08x fragment)",
400 osssink->frequency, osssink->format,
401 (osssink->channels == 2) ? "stereo" : "mono",frag);
403 ioctl (osssink->fd, SNDCTL_DSP_SETFRAGMENT, &frag);
405 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
407 target_format = osssink->format;
408 target_channels = osssink->channels;
409 target_frequency = osssink->frequency;
411 ioctl (osssink->fd, SNDCTL_DSP_SETFMT, &osssink->format);
412 ioctl (osssink->fd, SNDCTL_DSP_CHANNELS, &osssink->channels);
413 ioctl (osssink->fd, SNDCTL_DSP_SPEED, &osssink->frequency);
415 ioctl (osssink->fd, SNDCTL_DSP_GETBLKSIZE, &osssink->fragment_size);
416 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
418 /* calculate new fragment using a poor man's logarithm function */
421 while (fragscale < ospace.fragsize) {
425 osssink->fragment = ospace.fragstotal << 16 | frag_ln;
427 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: set sound card to %dHz %d bit %s (%d bytes buffer, %08x fragment)",
428 osssink->frequency, osssink->format,
429 (osssink->channels == 2) ? "stereo" : "mono", ospace.bytes, osssink->fragment);
431 object = G_OBJECT (osssink);
432 g_object_freeze_notify (object);
433 g_object_notify (object, "channels");
434 g_object_notify (object, "frequency");
435 g_object_notify (object, "fragment");
436 g_object_notify (object, "format");
437 g_object_thaw_notify (object);
439 osssink->fragment_time = (GST_SECOND * osssink->fragment_size) / osssink->bps;
440 GST_INFO (GST_CAT_PLUGIN_INFO, "fragment time %u %llu\n", osssink->bps, osssink->fragment_time);
442 if (target_format != osssink->format ||
443 target_channels != osssink->channels ||
444 target_frequency != osssink->frequency)
446 g_warning ("could not configure oss with required parameters, enjoy the noise :)");
447 /* we could eventually return FALSE here, or just do some additional tests
448 * to see that the frequencies don't differ too much etc.. */
454 gst_osssink_get_delay (GstOssSink *osssink)
458 if (osssink->fd == -1)
461 if (ioctl (osssink->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
463 if (ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
467 delay = (info.fragstotal * info.fragsize) - info.bytes;
474 gst_osssink_get_time (GstClock *clock, gpointer data)
476 GstOssSink *osssink = GST_OSSSINK (data);
483 delay = gst_osssink_get_delay (osssink);
485 /* sometimes delay is bigger than the number of bytes sent to the device, which screws
486 * up this calculation, we assume that everything is still in the device then */
487 if (((guint64)delay) > osssink->handled) {
488 delay = osssink->handled;
490 res = (osssink->handled - delay) * GST_SECOND / osssink->bps;
496 gst_osssink_get_clock (GstElement *element)
500 osssink = GST_OSSSINK (element);
502 return GST_CLOCK (osssink->provided_clock);
506 gst_osssink_set_clock (GstElement *element, GstClock *clock)
510 osssink = GST_OSSSINK (element);
512 osssink->clock = clock;
516 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
519 GstClockTime buftime;
521 /* this has to be an audio buffer */
522 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
524 if (GST_IS_EVENT (buf)) {
525 GstEvent *event = GST_EVENT (buf);
527 switch (GST_EVENT_TYPE (event)) {
529 ioctl (osssink->fd, SNDCTL_DSP_SYNC);
530 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
531 gst_pad_event_default (pad, event);
533 case GST_EVENT_NEW_MEDIA:
534 g_print ("new media\n");
536 case GST_EVENT_DISCONTINUOUS:
540 ioctl (osssink->fd, SNDCTL_DSP_RESET);
541 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
542 if (!gst_clock_handle_discont (osssink->clock, value))
543 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
544 osssink->handled = 0;
546 osssink->resync = TRUE;
550 gst_pad_event_default (pad, event);
553 gst_event_unref (event);
558 gst_buffer_unref (buf);
559 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
563 buftime = GST_BUFFER_TIMESTAMP (buf);
565 if (osssink->fd >= 0) {
566 if (!osssink->mute) {
567 guchar *data = GST_BUFFER_DATA (buf);
568 gint size = GST_BUFFER_SIZE (buf);
570 if (osssink->clock) {
573 GstClockTimeDiff jitter;
575 delay = gst_osssink_get_delay (osssink);
576 queued = delay * GST_SECOND / osssink->bps;
578 if (osssink->resync && osssink->sync) {
579 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock,
580 buftime - queued, &jitter);
583 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
584 write (osssink->fd, data, size);
585 gst_oss_clock_set_active (osssink->provided_clock, TRUE);
586 osssink->resync = FALSE;
587 osssink->handled += size;
591 write (osssink->fd, data, size);
592 osssink->handled += size;
595 /* no clock, try to be as fast as possible */
597 audio_buf_info ospace;
599 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
601 if (ospace.bytes >= size) {
602 write (osssink->fd, data, size);
607 gst_buffer_unref (buf);
610 static const GstFormat*
611 gst_osssink_get_formats (GstPad *pad)
613 static const GstFormat formats[] = {
623 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
624 GstFormat *dest_format, gint64 *dest_value)
630 if (src_format == *dest_format) {
631 *dest_value = src_value;
635 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
637 if (osssink->bps == 0 || osssink->channels == 0 || osssink->width == 0)
640 switch (src_format) {
641 case GST_FORMAT_BYTES:
642 switch (*dest_format) {
643 case GST_FORMAT_DEFAULT:
644 *dest_format = GST_FORMAT_TIME;
645 case GST_FORMAT_TIME:
646 *dest_value = src_value * GST_SECOND / osssink->bps;
648 case GST_FORMAT_UNITS:
649 *dest_value = src_value / (osssink->channels * osssink->width);
655 case GST_FORMAT_TIME:
656 switch (*dest_format) {
657 case GST_FORMAT_DEFAULT:
658 *dest_format = GST_FORMAT_BYTES;
659 case GST_FORMAT_BYTES:
660 *dest_value = src_value * osssink->bps / GST_SECOND;
662 case GST_FORMAT_UNITS:
663 *dest_value = src_value * osssink->frequency / GST_SECOND;
669 case GST_FORMAT_UNITS:
670 switch (*dest_format) {
671 case GST_FORMAT_DEFAULT:
672 *dest_format = GST_FORMAT_TIME;
673 case GST_FORMAT_TIME:
674 *dest_value = src_value * GST_SECOND / osssink->frequency;
676 case GST_FORMAT_BYTES:
677 *dest_value = src_value * osssink->channels * osssink->width;
690 static const GstPadQueryType*
691 gst_osssink_get_query_types (GstPad *pad)
693 static const GstPadQueryType query_types[] = {
694 GST_PAD_QUERY_LATENCY,
695 GST_PAD_QUERY_POSITION,
702 gst_osssink_sink_query (GstPad *pad, GstPadQueryType type, GstFormat *format, gint64 *value)
707 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
710 case GST_PAD_QUERY_LATENCY:
711 if (!gst_osssink_convert (pad,
712 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
718 case GST_PAD_QUERY_POSITION:
719 if (!gst_osssink_convert (pad,
720 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
727 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
735 gst_osssink_query (GstElement *element, GstPadQueryType type, GstFormat *format, gint64 *value)
737 GstOssSink *osssink = GST_OSSSINK (element);
739 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
743 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
747 /* it's not null if we got it, but it might not be ours */
748 g_return_if_fail (GST_IS_OSSSINK (object));
750 osssink = GST_OSSSINK (object);
754 /* disallow changing the device while it is opened
755 get_property("device") should return the right one */
756 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
758 g_free (osssink->device);
759 osssink->device = g_strdup (g_value_get_string (value));
760 g_object_notify (object, "device");
764 osssink->mute = g_value_get_boolean (value);
765 g_object_notify (G_OBJECT (osssink), "mute");
768 osssink->format = g_value_get_enum (value);
769 gst_osssink_sync_parms (osssink);
772 osssink->channels = g_value_get_enum (value);
773 gst_osssink_sync_parms (osssink);
776 osssink->frequency = g_value_get_int (value);
777 gst_osssink_sync_parms (osssink);
780 osssink->fragment = g_value_get_int (value);
781 gst_osssink_sync_parms (osssink);
783 case ARG_BUFFER_SIZE:
784 if (osssink->bufsize == g_value_get_int (value)) break;
785 osssink->bufsize = g_value_get_int (value);
786 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
787 g_object_notify (object, "buffer_size");
790 osssink->sync = g_value_get_boolean (value);
791 g_object_notify (G_OBJECT (osssink), "sync");
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
800 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
804 /* it's not null if we got it, but it might not be ours */
805 g_return_if_fail (GST_IS_OSSSINK (object));
807 osssink = GST_OSSSINK (object);
811 g_value_set_string (value, osssink->device);
814 g_value_set_boolean (value, osssink->mute);
817 g_value_set_enum (value, osssink->format);
820 g_value_set_enum (value, osssink->channels);
823 g_value_set_int (value, osssink->frequency);
826 g_value_set_int (value, osssink->fragment);
828 case ARG_BUFFER_SIZE:
829 g_value_set_int (value, osssink->bufsize);
832 g_value_set_boolean (value, osssink->sync);
835 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
841 gst_osssink_open_audio (GstOssSink *sink)
844 g_return_val_if_fail (sink->fd == -1, FALSE);
846 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: attempting to open sound device");
848 /* first try to open the sound card */
849 sink->fd = open (sink->device, O_WRONLY | O_NONBLOCK);
850 if (errno == EBUSY) {
851 g_warning ("osssink: unable to open the sound device (in use ?)\n");
855 /* re-open the sound device in blocking mode */
857 sink->fd = open (sink->device, O_WRONLY);
860 g_warning ("osssink: unable to open the sound device (errno=%d)\n", errno);
864 /* we have it, set the default parameters and go have fun */
866 ioctl (sink->fd, SNDCTL_DSP_GETCAPS, &caps);
868 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Capabilities %08x", caps);
870 if (caps & DSP_CAP_DUPLEX) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Full duplex");
871 if (caps & DSP_CAP_REALTIME) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Realtime");
872 if (caps & DSP_CAP_BATCH) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Batch");
873 if (caps & DSP_CAP_COPROC) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Has coprocessor");
874 if (caps & DSP_CAP_TRIGGER) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Trigger");
875 if (caps & DSP_CAP_MMAP) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Direct access");
878 if (caps & DSP_CAP_MULTI) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Multiple open");
879 #endif /* DSP_CAP_MULTI */
882 if (caps & DSP_CAP_BIND) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Channel binding");
883 #endif /* DSP_CAP_BIND */
885 ioctl(sink->fd, SNDCTL_DSP_GETFMTS, &caps);
887 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Formats %08x", caps);
888 if (caps & AFMT_MU_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MU_LAW");
889 if (caps & AFMT_A_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: A_LAW");
890 if (caps & AFMT_IMA_ADPCM) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: IMA_ADPCM");
891 if (caps & AFMT_U8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U8");
892 if (caps & AFMT_S16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_LE");
893 if (caps & AFMT_S16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_BE");
894 if (caps & AFMT_S8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S8");
895 if (caps & AFMT_U16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_LE");
896 if (caps & AFMT_U16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_BE");
897 if (caps & AFMT_MPEG) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MPEG");
899 if (caps & AFMT_AC3) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: AC3");
902 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: opened audio (%s) with fd=%d", sink->device, sink->fd);
903 GST_FLAG_SET (sink, GST_OSSSINK_OPEN);
909 gst_osssink_close_audio (GstOssSink *sink)
911 if (sink->fd < 0) return;
916 GST_FLAG_UNSET (sink, GST_OSSSINK_OPEN);
918 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
921 static GstElementStateReturn
922 gst_osssink_change_state (GstElement *element)
926 g_return_val_if_fail (GST_IS_OSSSINK (element), FALSE);
928 osssink = GST_OSSSINK (element);
930 switch (GST_STATE_TRANSITION (element)) {
931 case GST_STATE_NULL_TO_READY:
932 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
933 if (!gst_osssink_open_audio (osssink)) {
934 return GST_STATE_FAILURE;
938 case GST_STATE_READY_TO_PAUSED:
940 case GST_STATE_PAUSED_TO_PLAYING:
941 osssink->resync = TRUE;
943 case GST_STATE_PLAYING_TO_PAUSED:
945 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
946 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
947 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
948 osssink->resync = TRUE;
951 case GST_STATE_PAUSED_TO_READY:
952 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
953 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
955 case GST_STATE_READY_TO_NULL:
956 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
957 gst_osssink_close_audio (osssink);
961 if (GST_ELEMENT_CLASS (parent_class)->change_state)
962 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
964 return GST_STATE_SUCCESS;
968 gst_osssink_factory_init (GstPlugin *plugin)
970 GstElementFactory *factory;
972 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
973 g_return_val_if_fail (factory != NULL, FALSE);
975 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
977 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));