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);
55 static GstPadConnectReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
57 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
59 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
62 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
64 /* OssSink signals and args */
82 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
89 "format", GST_PROPS_STRING ("int"), /* hack */
90 "law", GST_PROPS_INT (0),
91 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
92 "signed", GST_PROPS_LIST (
93 GST_PROPS_BOOLEAN (FALSE),
94 GST_PROPS_BOOLEAN (TRUE)
96 "width", GST_PROPS_LIST (
100 "depth", GST_PROPS_LIST (
104 "rate", GST_PROPS_INT_RANGE (1000, 48000),
105 "channels", GST_PROPS_INT_RANGE (1, 2)
109 #define GST_TYPE_OSSSINK_CHANNELS (gst_osssink_channels_get_type())
111 gst_osssink_channels_get_type(void) {
112 static GType osssink_channels_type = 0;
113 static GEnumValue osssink_channels[] = {
119 if (!osssink_channels_type) {
120 osssink_channels_type = g_enum_register_static("GstAudiosinkChannels", osssink_channels);
122 return osssink_channels_type;
126 static GstElementClass *parent_class = NULL;
127 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
130 gst_osssink_get_type (void)
132 static GType osssink_type = 0;
135 static const GTypeInfo osssink_info = {
136 sizeof(GstOssSinkClass),
139 (GClassInitFunc)gst_osssink_class_init,
144 (GInstanceInitFunc)gst_osssink_init,
146 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
152 static GstBufferPool*
153 gst_osssink_get_bufferpool (GstPad *pad)
157 oss = GST_OSSSINK (gst_pad_get_parent(pad));
159 return oss->sinkpool;
163 gst_osssink_finalize (GObject *object)
165 GstOssSink *osssink = (GstOssSink *) object;
167 g_free (osssink->device);
169 G_OBJECT_CLASS (parent_class)->finalize (object);
173 gst_osssink_class_init (GstOssSinkClass *klass)
175 GObjectClass *gobject_class;
176 GstElementClass *gstelement_class;
178 gobject_class = (GObjectClass*)klass;
179 gstelement_class = (GstElementClass*)klass;
181 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
183 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE,
184 g_param_spec_string("device","device","device",
185 "/dev/dsp",G_PARAM_READWRITE)); /* CHECKME! */
186 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
187 g_param_spec_boolean("mute","mute","mute",
188 TRUE,G_PARAM_READWRITE));
190 /* it would be nice to show format in symbolic form, oh well */
191 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FORMAT,
192 g_param_spec_int ("format","format","format",
193 0, G_MAXINT, AFMT_S16_LE, G_PARAM_READWRITE));
195 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNELS,
196 g_param_spec_enum("channels","channels","channels",
197 GST_TYPE_OSSSINK_CHANNELS,2,G_PARAM_READWRITE));
198 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FREQUENCY,
199 g_param_spec_int("frequency","frequency","frequency",
200 0,G_MAXINT,44100,G_PARAM_READWRITE));
201 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAGMENT,
202 g_param_spec_int("fragment","fragment","fragment",
203 0,G_MAXINT,6,G_PARAM_READWRITE));
204 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFER_SIZE,
205 g_param_spec_int("buffer_size","buffer_size","buffer_size",
206 0,G_MAXINT,4096,G_PARAM_READWRITE));
208 gst_osssink_signals[SIGNAL_HANDOFF] =
209 g_signal_new("handoff",G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
210 G_STRUCT_OFFSET(GstOssSinkClass,handoff), NULL, NULL,
211 g_cclosure_marshal_VOID__VOID,G_TYPE_NONE,0);
213 gobject_class->set_property = gst_osssink_set_property;
214 gobject_class->get_property = gst_osssink_get_property;
215 gobject_class->finalize = gst_osssink_finalize;
217 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
221 gst_osssink_get_time (GstClock *clock, gpointer data)
223 GstOssSink *osssink = GST_OSSSINK (data);
229 ioctl (osssink->fd, SNDCTL_DSP_GETODELAY, &delay);
231 return osssink->offset + (osssink->handled - delay) * 1000000LL / osssink->bps;
235 gst_osssink_init (GstOssSink *osssink)
237 osssink->sinkpad = gst_pad_new_from_template (
238 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
239 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
240 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
241 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
243 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
245 osssink->device = g_strdup ("/dev/dsp");
247 osssink->channels = 1;
248 osssink->frequency = 11025;
249 osssink->fragment = 6;
250 /* AFMT_*_BE not available on all OSS includes (e.g. FBSD) */
251 #ifdef WORDS_BIGENDIAN
252 osssink->format = AFMT_S16_BE;
254 osssink->format = AFMT_S16_LE;
255 #endif /* WORDS_BIGENDIAN */
256 /* gst_clock_register (osssink->clock, GST_OBJECT (osssink)); */
257 osssink->bufsize = 4096;
258 osssink->offset = -1LL;
259 osssink->handled = 0LL;
260 /* 6 buffers per chunk by default */
261 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
263 osssink->provided_clock = NULL;
264 osssink->provided_clock = GST_CLOCK (gst_oss_clock_new ("ossclock", gst_osssink_get_time, osssink));
266 GST_ELEMENT (osssink)->setclockfunc = gst_osssink_set_clock;
267 GST_ELEMENT (osssink)->getclockfunc = gst_osssink_get_clock;
269 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
270 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
273 static GstPadConnectReturn
274 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
276 gint law, endianness, width, depth;
279 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
281 if (!GST_CAPS_IS_FIXED (caps))
282 return GST_PAD_CONNECT_DELAYED;
284 gst_caps_get_int (caps, "width", &width);
285 gst_caps_get_int (caps, "depth", &depth);
288 return GST_PAD_CONNECT_REFUSED;
290 /* laws 1 and 2 are 1 bps anyway */
293 gst_caps_get_int (caps, "law", &law);
294 gst_caps_get_int (caps, "endianness", &endianness);
295 gst_caps_get_boolean (caps, "signed", &sign);
300 if (endianness == G_LITTLE_ENDIAN)
301 format = AFMT_S16_LE;
302 else if (endianness == G_BIG_ENDIAN)
303 format = AFMT_S16_BE;
306 if (endianness == G_LITTLE_ENDIAN)
307 format = AFMT_U16_LE;
308 else if (endianness == G_BIG_ENDIAN)
309 format = AFMT_U16_BE;
313 else if (width == 8) {
322 } else if (law == 1) {
323 format = AFMT_MU_LAW;
324 } else if (law == 2) {
327 g_critical ("unknown law");
328 return GST_PAD_CONNECT_REFUSED;
332 return GST_PAD_CONNECT_REFUSED;
334 osssink->format = format;
335 gst_caps_get_int (caps, "channels", &osssink->channels);
336 gst_caps_get_int (caps, "rate", &osssink->frequency);
338 osssink->bps *= osssink->channels;
339 osssink->bps *= osssink->frequency;
341 if (!gst_osssink_sync_parms (osssink)) {
342 return GST_PAD_CONNECT_REFUSED;
345 return GST_PAD_CONNECT_OK;
349 gst_osssink_sync_parms (GstOssSink *osssink)
351 audio_buf_info ospace;
354 gint target_channels;
355 gint target_frequency;
358 g_return_val_if_fail (osssink != NULL, FALSE);
359 g_return_val_if_fail (GST_IS_OSSSINK (osssink), FALSE);
361 if (osssink->fd == -1)
364 if (osssink->fragment >> 16)
365 frag = osssink->fragment;
367 frag = 0x7FFF0000 | osssink->fragment;
369 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: trying to set sound card to %dHz %d bit %s (%08x fragment)",
370 osssink->frequency, osssink->format,
371 (osssink->channels == 2) ? "stereo" : "mono",frag);
373 ioctl (osssink->fd, SNDCTL_DSP_SETFRAGMENT, &frag);
375 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
377 target_format = osssink->format;
378 target_channels = osssink->channels;
379 target_frequency = osssink->frequency;
381 ioctl (osssink->fd, SNDCTL_DSP_SETFMT, &osssink->format);
382 ioctl (osssink->fd, SNDCTL_DSP_CHANNELS, &osssink->channels);
383 ioctl (osssink->fd, SNDCTL_DSP_SPEED, &osssink->frequency);
385 ioctl (osssink->fd, SNDCTL_DSP_GETBLKSIZE, &osssink->fragment);
386 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
388 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: set sound card to %dHz %d bit %s (%d bytes buffer, %08x fragment)",
389 osssink->frequency, osssink->format,
390 (osssink->channels == 2) ? "stereo" : "mono", ospace.bytes, osssink->fragment);
392 object = G_OBJECT (osssink);
393 g_object_freeze_notify (object);
394 g_object_notify (object, "channels");
395 g_object_notify (object, "frequency");
396 g_object_notify (object, "fragment");
397 g_object_notify (object, "format");
398 g_object_thaw_notify (object);
400 osssink->fragment_time = (1000000 * osssink->fragment) / osssink->bps;
401 GST_INFO (GST_CAT_PLUGIN_INFO, "fragment time %u %llu\n", osssink->bps, osssink->fragment_time);
403 if (target_format != osssink->format ||
404 target_channels != osssink->channels ||
405 target_frequency != osssink->frequency)
407 g_warning ("could not configure oss with required parameters, enjoy the noise :)");
408 /* we could eventually return FALSE here, or just do some additional tests
409 * to see that the frequencies don't differ too much etc.. */
415 gst_osssink_get_clock (GstElement *element)
419 osssink = GST_OSSSINK (element);
421 return GST_CLOCK (osssink->provided_clock);
425 gst_osssink_set_clock (GstElement *element, GstClock *clock)
429 osssink = GST_OSSSINK (element);
431 osssink->clock = clock;
435 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
438 GstClockTime buftime;
440 /* this has to be an audio buffer */
441 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
443 if (GST_IS_EVENT (buf)) {
444 switch (GST_EVENT_TYPE (buf)) {
446 ioctl (osssink->fd, SNDCTL_DSP_SYNC);
447 gst_pad_event_default (pad, GST_EVENT (buf));
454 buftime = GST_BUFFER_TIMESTAMP (buf);
457 gst_buffer_unref (buf);
458 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
461 if (osssink->fd >= 0) {
462 if (!osssink->mute) {
463 guchar *data = GST_BUFFER_DATA (buf);
464 gint size = GST_BUFFER_SIZE (buf);
465 /* gint frag = osssink->fragment; <-- unused, for reason see above */
467 if (osssink->clock) {
468 /* FIXME, NEW_MEDIA/DISCONT?. Try to get our start point */
469 if (osssink->offset == -1LL && buftime != -1LL) {
470 /* g_print ("%lld %lld %lld\n", osssink->offset, buftime, gst_clock_get_time (osssink->clock)); */
471 osssink->offset = buftime;
472 osssink->handled = 0;
473 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock, buftime);
477 /* this doesn't work on BE machines, apparently
479 gint tosend = MIN (size, frag);
480 write (osssink->fd, data, tosend);
483 osssink->handled += tosend;
485 write (osssink->fd, data, size);
487 /* no clock, try to be as fast as possible */
489 audio_buf_info ospace;
491 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
493 if (ospace.bytes >= size) {
494 write (osssink->fd, data, size);
496 osssink->handled += size;
500 gst_buffer_unref (buf);
504 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
508 /* it's not null if we got it, but it might not be ours */
509 g_return_if_fail (GST_IS_OSSSINK (object));
511 osssink = GST_OSSSINK (object);
515 /* disallow changing the device while it is opened
516 get_property("device") should return the right one */
517 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
519 g_free (osssink->device);
520 osssink->device = g_strdup (g_value_get_string (value));
521 g_object_notify (object, "device");
525 osssink->mute = g_value_get_boolean (value);
526 g_object_notify (G_OBJECT (osssink), "mute");
529 osssink->format = g_value_get_int (value);
530 gst_osssink_sync_parms (osssink);
533 osssink->channels = g_value_get_enum (value);
534 gst_osssink_sync_parms (osssink);
537 osssink->frequency = g_value_get_int (value);
538 gst_osssink_sync_parms (osssink);
541 osssink->fragment = g_value_get_int (value);
542 gst_osssink_sync_parms (osssink);
544 case ARG_BUFFER_SIZE:
545 if (osssink->bufsize == g_value_get_int (value)) break;
546 osssink->bufsize = g_value_get_int (value);
547 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
548 g_object_notify (object, "buffer_size");
556 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
560 /* it's not null if we got it, but it might not be ours */
561 g_return_if_fail (GST_IS_OSSSINK (object));
563 osssink = GST_OSSSINK (object);
567 g_value_set_string (value, osssink->device);
570 g_value_set_boolean (value, osssink->mute);
573 g_value_set_int (value, osssink->format);
576 g_value_set_enum (value, osssink->channels);
579 g_value_set_int (value, osssink->frequency);
582 g_value_set_int (value, osssink->fragment);
584 case ARG_BUFFER_SIZE:
585 g_value_set_int (value, osssink->bufsize);
593 gst_osssink_open_audio (GstOssSink *sink)
596 g_return_val_if_fail (sink->fd == -1, FALSE);
598 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: attempting to open sound device");
600 /* first try to open the sound card */
601 sink->fd = open(sink->device, O_WRONLY | O_NONBLOCK);
602 if (errno == EBUSY) {
603 g_warning ("osssink: unable to open the sound device (in use ?)\n");
607 /* re-open the sound device in blocking mode */
609 sink->fd = open(sink->device, O_WRONLY);
611 /* if we have it, set the default parameters and go have fun */
614 ioctl(sink->fd, SNDCTL_DSP_GETCAPS, &caps);
616 GST_INFO(GST_CAT_PLUGIN_INFO, "osssink: Capabilities %08x", caps);
618 if (caps & DSP_CAP_DUPLEX) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Full duplex");
619 if (caps & DSP_CAP_REALTIME) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Realtime");
620 if (caps & DSP_CAP_BATCH) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Batch");
621 if (caps & DSP_CAP_COPROC) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Has coprocessor");
622 if (caps & DSP_CAP_TRIGGER) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Trigger");
623 if (caps & DSP_CAP_MMAP) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Direct access");
626 if (caps & DSP_CAP_MULTI) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Multiple open");
627 #endif /* DSP_CAP_MULTI */
630 if (caps & DSP_CAP_BIND) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Channel binding");
631 #endif /* DSP_CAP_BIND */
633 ioctl(sink->fd, SNDCTL_DSP_GETFMTS, &caps);
635 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Formats %08x", caps);
636 if (caps & AFMT_MU_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MU_LAW");
637 if (caps & AFMT_A_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: A_LAW");
638 if (caps & AFMT_IMA_ADPCM) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: IMA_ADPCM");
639 if (caps & AFMT_U8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U8");
640 if (caps & AFMT_S16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_LE");
641 if (caps & AFMT_S16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_BE");
642 if (caps & AFMT_S8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S8");
643 if (caps & AFMT_U16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_LE");
644 if (caps & AFMT_U16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_BE");
645 if (caps & AFMT_MPEG) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MPEG");
647 if (caps & AFMT_AC3) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: AC3");
650 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: opened audio (%s) with fd=%d", sink->device, sink->fd);
651 GST_FLAG_SET (sink, GST_OSSSINK_OPEN);
660 gst_osssink_close_audio (GstOssSink *sink)
662 if (sink->fd < 0) return;
667 GST_FLAG_UNSET (sink, GST_OSSSINK_OPEN);
669 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
672 static GstElementStateReturn
673 gst_osssink_change_state (GstElement *element)
677 g_return_val_if_fail (GST_IS_OSSSINK (element), FALSE);
679 osssink = GST_OSSSINK (element);
681 switch (GST_STATE_TRANSITION (element)) {
682 case GST_STATE_NULL_TO_READY:
683 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
684 if (!gst_osssink_open_audio (osssink)) {
685 return GST_STATE_FAILURE;
689 case GST_STATE_READY_TO_PAUSED:
690 osssink->offset = -1LL;
692 case GST_STATE_PAUSED_TO_PLAYING:
693 /* gst_clock_adjust (osssink->clock, osssink->offset - gst_clock_get_time (osssink->clock)); */
695 case GST_STATE_PLAYING_TO_PAUSED:
697 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
700 audio_buf_info ospace;
703 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
704 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
706 queued = (ospace.fragstotal * ospace.fragsize) - ospace.bytes;
707 time = osssink->offset + (osssink->handled - queued) * 1000000LL / osssink->bps;
709 //gst_clock_adjust (osssink->clock, time - gst_clock_get_time (osssink->clock));
712 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
718 case GST_STATE_PAUSED_TO_READY:
719 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
720 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
722 case GST_STATE_READY_TO_NULL:
723 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
724 gst_osssink_close_audio (osssink);
728 if (GST_ELEMENT_CLASS (parent_class)->change_state)
729 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
731 return GST_STATE_SUCCESS;
735 gst_osssink_factory_init (GstPlugin *plugin)
737 GstElementFactory *factory;
739 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
740 g_return_val_if_fail (factory != NULL, FALSE);
742 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
744 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));