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 */
83 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
90 "format", GST_PROPS_STRING ("int"), /* hack */
91 "law", GST_PROPS_INT (0),
92 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
93 "signed", GST_PROPS_LIST (
94 GST_PROPS_BOOLEAN (FALSE),
95 GST_PROPS_BOOLEAN (TRUE)
97 "width", GST_PROPS_LIST (
101 "depth", GST_PROPS_LIST (
105 "rate", GST_PROPS_INT_RANGE (1000, 48000),
106 "channels", GST_PROPS_INT_RANGE (1, 2)
110 #define GST_TYPE_OSSSINK_CHANNELS (gst_osssink_channels_get_type())
112 gst_osssink_channels_get_type(void) {
113 static GType osssink_channels_type = 0;
114 static GEnumValue osssink_channels[] = {
120 if (!osssink_channels_type) {
121 osssink_channels_type = g_enum_register_static("GstAudiosinkChannels", osssink_channels);
123 return osssink_channels_type;
127 static GstElementClass *parent_class = NULL;
128 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
131 gst_osssink_get_type (void)
133 static GType osssink_type = 0;
136 static const GTypeInfo osssink_info = {
137 sizeof(GstOssSinkClass),
140 (GClassInitFunc)gst_osssink_class_init,
145 (GInstanceInitFunc)gst_osssink_init,
147 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
153 static GstBufferPool*
154 gst_osssink_get_bufferpool (GstPad *pad)
158 oss = GST_OSSSINK (gst_pad_get_parent(pad));
160 return oss->sinkpool;
164 gst_osssink_finalize (GObject *object)
166 GstOssSink *osssink = (GstOssSink *) object;
168 g_free (osssink->device);
170 G_OBJECT_CLASS (parent_class)->finalize (object);
174 gst_osssink_class_init (GstOssSinkClass *klass)
176 GObjectClass *gobject_class;
177 GstElementClass *gstelement_class;
179 gobject_class = (GObjectClass*)klass;
180 gstelement_class = (GstElementClass*)klass;
182 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
184 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE,
185 g_param_spec_string("device","device","device",
186 "/dev/dsp",G_PARAM_READWRITE)); /* CHECKME! */
187 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
188 g_param_spec_boolean("mute","mute","mute",
189 TRUE,G_PARAM_READWRITE));
190 g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_SYNC,
191 g_param_spec_boolean("sync","Sync","If syncing on timestamps should be anabled",
192 TRUE, G_PARAM_READWRITE));
194 /* it would be nice to show format in symbolic form, oh well */
195 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FORMAT,
196 g_param_spec_int ("format","format","format",
197 0, G_MAXINT, AFMT_S16_LE, G_PARAM_READWRITE));
199 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNELS,
200 g_param_spec_enum("channels","channels","channels",
201 GST_TYPE_OSSSINK_CHANNELS,2,G_PARAM_READWRITE));
202 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FREQUENCY,
203 g_param_spec_int("frequency","frequency","frequency",
204 0,G_MAXINT,44100,G_PARAM_READWRITE));
205 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAGMENT,
206 g_param_spec_int("fragment","fragment","fragment",
207 0,G_MAXINT,6,G_PARAM_READWRITE));
208 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFER_SIZE,
209 g_param_spec_int("buffer_size","buffer_size","buffer_size",
210 0,G_MAXINT,4096,G_PARAM_READWRITE));
212 gst_osssink_signals[SIGNAL_HANDOFF] =
213 g_signal_new("handoff",G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
214 G_STRUCT_OFFSET(GstOssSinkClass,handoff), NULL, NULL,
215 g_cclosure_marshal_VOID__VOID,G_TYPE_NONE,0);
217 gobject_class->set_property = gst_osssink_set_property;
218 gobject_class->get_property = gst_osssink_get_property;
219 gobject_class->finalize = gst_osssink_finalize;
221 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
225 gst_osssink_init (GstOssSink *osssink)
227 osssink->sinkpad = gst_pad_new_from_template (
228 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
229 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
230 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
231 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
233 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
235 osssink->device = g_strdup ("/dev/dsp");
237 osssink->channels = 1;
238 osssink->frequency = 11025;
239 osssink->fragment = 6;
240 /* AFMT_*_BE not available on all OSS includes (e.g. FBSD) */
241 #ifdef WORDS_BIGENDIAN
242 osssink->format = AFMT_S16_BE;
244 osssink->format = AFMT_S16_LE;
245 #endif /* WORDS_BIGENDIAN */
246 osssink->bufsize = 4096;
248 osssink->resync = FALSE;
249 osssink->sync = TRUE;
250 /* 6 buffers per chunk by default */
251 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
253 GST_ELEMENT (osssink)->setclockfunc = gst_osssink_set_clock;
255 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
256 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
259 static GstPadConnectReturn
260 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
262 gint law, endianness, width, depth;
265 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
267 if (!GST_CAPS_IS_FIXED (caps))
268 return GST_PAD_CONNECT_DELAYED;
270 gst_caps_get_int (caps, "width", &width);
271 gst_caps_get_int (caps, "depth", &depth);
274 return GST_PAD_CONNECT_REFUSED;
276 /* laws 1 and 2 are 1 bps anyway */
279 gst_caps_get_int (caps, "law", &law);
280 gst_caps_get_int (caps, "endianness", &endianness);
281 gst_caps_get_boolean (caps, "signed", &sign);
286 if (endianness == G_LITTLE_ENDIAN)
287 format = AFMT_S16_LE;
288 else if (endianness == G_BIG_ENDIAN)
289 format = AFMT_S16_BE;
292 if (endianness == G_LITTLE_ENDIAN)
293 format = AFMT_U16_LE;
294 else if (endianness == G_BIG_ENDIAN)
295 format = AFMT_U16_BE;
299 else if (width == 8) {
308 } else if (law == 1) {
309 format = AFMT_MU_LAW;
310 } else if (law == 2) {
313 g_critical ("unknown law");
314 return GST_PAD_CONNECT_REFUSED;
318 return GST_PAD_CONNECT_REFUSED;
320 osssink->format = format;
321 gst_caps_get_int (caps, "channels", &osssink->channels);
322 gst_caps_get_int (caps, "rate", &osssink->frequency);
324 osssink->bps *= osssink->channels;
325 osssink->bps *= osssink->frequency;
327 if (!gst_osssink_sync_parms (osssink)) {
328 return GST_PAD_CONNECT_REFUSED;
331 return GST_PAD_CONNECT_OK;
335 gst_osssink_sync_parms (GstOssSink *osssink)
337 audio_buf_info ospace;
340 gint target_channels;
341 gint target_frequency;
344 g_return_val_if_fail (osssink != NULL, FALSE);
345 g_return_val_if_fail (GST_IS_OSSSINK (osssink), FALSE);
347 if (osssink->fd == -1)
350 if (osssink->fragment >> 16)
351 frag = osssink->fragment;
353 frag = 0x7FFF0000 | osssink->fragment;
355 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: trying to set sound card to %dHz %d bit %s (%08x fragment)",
356 osssink->frequency, osssink->format,
357 (osssink->channels == 2) ? "stereo" : "mono",frag);
359 ioctl (osssink->fd, SNDCTL_DSP_SETFRAGMENT, &frag);
361 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
363 target_format = osssink->format;
364 target_channels = osssink->channels;
365 target_frequency = osssink->frequency;
367 ioctl (osssink->fd, SNDCTL_DSP_SETFMT, &osssink->format);
368 ioctl (osssink->fd, SNDCTL_DSP_CHANNELS, &osssink->channels);
369 ioctl (osssink->fd, SNDCTL_DSP_SPEED, &osssink->frequency);
371 ioctl (osssink->fd, SNDCTL_DSP_GETBLKSIZE, &osssink->fragment);
372 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
374 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: set sound card to %dHz %d bit %s (%d bytes buffer, %08x fragment)",
375 osssink->frequency, osssink->format,
376 (osssink->channels == 2) ? "stereo" : "mono", ospace.bytes, osssink->fragment);
378 object = G_OBJECT (osssink);
379 g_object_freeze_notify (object);
380 g_object_notify (object, "channels");
381 g_object_notify (object, "frequency");
382 g_object_notify (object, "fragment");
383 g_object_notify (object, "format");
384 g_object_thaw_notify (object);
386 osssink->fragment_time = (1000000 * osssink->fragment) / osssink->bps;
387 GST_INFO (GST_CAT_PLUGIN_INFO, "fragment time %u %llu\n", osssink->bps, osssink->fragment_time);
389 if (target_format != osssink->format ||
390 target_channels != osssink->channels ||
391 target_frequency != osssink->frequency)
393 g_warning ("could not configure oss with required parameters, enjoy the noise :)");
394 /* we could eventually return FALSE here, or just do some additional tests
395 * to see that the frequencies don't differ too much etc.. */
401 gst_osssink_set_clock (GstElement *element, GstClock *clock)
405 osssink = GST_OSSSINK (element);
407 osssink->clock = clock;
411 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
414 GstClockTime buftime;
416 /* this has to be an audio buffer */
417 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
419 if (GST_IS_EVENT (buf)) {
420 GstEvent *event = GST_EVENT (buf);
423 switch (GST_EVENT_TYPE (event)) {
425 ioctl (osssink->fd, SNDCTL_DSP_SYNC);
426 gst_pad_event_default (pad, event);
428 case GST_EVENT_NEW_MEDIA:
429 g_print ("new media\n");
431 case GST_EVENT_DISCONTINUOUS:
435 ioctl (osssink->fd, SNDCTL_DSP_RESET);
436 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
437 gst_clock_handle_discont (osssink->clock, value);
439 osssink->resync = TRUE;
443 gst_pad_event_default (pad, event);
446 gst_event_free (event);
450 gst_buffer_unref (buf);
451 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
455 buftime = GST_BUFFER_TIMESTAMP (buf);
457 if (osssink->fd >= 0) {
458 if (!osssink->mute) {
459 guchar *data = GST_BUFFER_DATA (buf);
460 gint size = GST_BUFFER_SIZE (buf);
462 if (osssink->clock) {
465 GstClockTimeDiff jitter;
467 if (ioctl (osssink->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
469 if (ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
473 delay = (info.fragstotal * info.fragsize) - info.bytes;
476 queued = delay * GST_SECOND / osssink->bps;
478 if (osssink->resync && osssink->sync) {
479 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock,
480 buftime - queued, &jitter);
483 write (osssink->fd, data, size);
484 osssink->resync = FALSE;
488 write (osssink->fd, data, size);
491 /* no clock, try to be as fast as possible */
493 audio_buf_info ospace;
495 ioctl (osssink->fd, SNDCTL_DSP_GETOSPACE, &ospace);
497 if (ospace.bytes >= size) {
498 write (osssink->fd, data, size);
503 gst_buffer_unref (buf);
507 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
511 /* it's not null if we got it, but it might not be ours */
512 g_return_if_fail (GST_IS_OSSSINK (object));
514 osssink = GST_OSSSINK (object);
518 /* disallow changing the device while it is opened
519 get_property("device") should return the right one */
520 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
522 g_free (osssink->device);
523 osssink->device = g_strdup (g_value_get_string (value));
524 g_object_notify (object, "device");
528 osssink->mute = g_value_get_boolean (value);
529 g_object_notify (G_OBJECT (osssink), "mute");
532 osssink->format = g_value_get_int (value);
533 gst_osssink_sync_parms (osssink);
536 osssink->channels = g_value_get_enum (value);
537 gst_osssink_sync_parms (osssink);
540 osssink->frequency = g_value_get_int (value);
541 gst_osssink_sync_parms (osssink);
544 osssink->fragment = g_value_get_int (value);
545 gst_osssink_sync_parms (osssink);
547 case ARG_BUFFER_SIZE:
548 if (osssink->bufsize == g_value_get_int (value)) break;
549 osssink->bufsize = g_value_get_int (value);
550 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
551 g_object_notify (object, "buffer_size");
554 osssink->sync = g_value_get_boolean (value);
555 g_object_notify (G_OBJECT (osssink), "sync");
562 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
566 /* it's not null if we got it, but it might not be ours */
567 g_return_if_fail (GST_IS_OSSSINK (object));
569 osssink = GST_OSSSINK (object);
573 g_value_set_string (value, osssink->device);
576 g_value_set_boolean (value, osssink->mute);
579 g_value_set_int (value, osssink->format);
582 g_value_set_enum (value, osssink->channels);
585 g_value_set_int (value, osssink->frequency);
588 g_value_set_int (value, osssink->fragment);
590 case ARG_BUFFER_SIZE:
591 g_value_set_int (value, osssink->bufsize);
594 g_value_set_boolean (value, osssink->sync);
602 gst_osssink_open_audio (GstOssSink *sink)
605 g_return_val_if_fail (sink->fd == -1, FALSE);
607 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: attempting to open sound device");
609 /* first try to open the sound card */
610 sink->fd = open(sink->device, O_WRONLY | O_NONBLOCK);
611 if (errno == EBUSY) {
612 g_warning ("osssink: unable to open the sound device (in use ?)\n");
616 /* re-open the sound device in blocking mode */
618 sink->fd = open(sink->device, O_WRONLY);
620 /* if we have it, set the default parameters and go have fun */
623 ioctl(sink->fd, SNDCTL_DSP_GETCAPS, &caps);
625 GST_INFO(GST_CAT_PLUGIN_INFO, "osssink: Capabilities %08x", caps);
627 if (caps & DSP_CAP_DUPLEX) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Full duplex");
628 if (caps & DSP_CAP_REALTIME) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Realtime");
629 if (caps & DSP_CAP_BATCH) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Batch");
630 if (caps & DSP_CAP_COPROC) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Has coprocessor");
631 if (caps & DSP_CAP_TRIGGER) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Trigger");
632 if (caps & DSP_CAP_MMAP) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Direct access");
635 if (caps & DSP_CAP_MULTI) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Multiple open");
636 #endif /* DSP_CAP_MULTI */
639 if (caps & DSP_CAP_BIND) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Channel binding");
640 #endif /* DSP_CAP_BIND */
642 ioctl(sink->fd, SNDCTL_DSP_GETFMTS, &caps);
644 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: Formats %08x", caps);
645 if (caps & AFMT_MU_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MU_LAW");
646 if (caps & AFMT_A_LAW) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: A_LAW");
647 if (caps & AFMT_IMA_ADPCM) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: IMA_ADPCM");
648 if (caps & AFMT_U8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U8");
649 if (caps & AFMT_S16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_LE");
650 if (caps & AFMT_S16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S16_BE");
651 if (caps & AFMT_S8) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: S8");
652 if (caps & AFMT_U16_LE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_LE");
653 if (caps & AFMT_U16_BE) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: U16_BE");
654 if (caps & AFMT_MPEG) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: MPEG");
656 if (caps & AFMT_AC3) GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: AC3");
659 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: opened audio (%s) with fd=%d", sink->device, sink->fd);
660 GST_FLAG_SET (sink, GST_OSSSINK_OPEN);
669 gst_osssink_close_audio (GstOssSink *sink)
671 if (sink->fd < 0) return;
676 GST_FLAG_UNSET (sink, GST_OSSSINK_OPEN);
678 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
681 static GstElementStateReturn
682 gst_osssink_change_state (GstElement *element)
686 g_return_val_if_fail (GST_IS_OSSSINK (element), FALSE);
688 osssink = GST_OSSSINK (element);
690 switch (GST_STATE_TRANSITION (element)) {
691 case GST_STATE_NULL_TO_READY:
692 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
693 if (!gst_osssink_open_audio (osssink)) {
694 return GST_STATE_FAILURE;
698 case GST_STATE_READY_TO_PAUSED:
699 case GST_STATE_PAUSED_TO_PLAYING:
700 osssink->resync = TRUE;
702 case GST_STATE_PLAYING_TO_PAUSED:
704 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
705 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
708 case GST_STATE_PAUSED_TO_READY:
709 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
710 ioctl (osssink->fd, SNDCTL_DSP_RESET, 0);
712 case GST_STATE_READY_TO_NULL:
713 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
714 gst_osssink_close_audio (osssink);
718 if (GST_ELEMENT_CLASS (parent_class)->change_state)
719 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
721 return GST_STATE_SUCCESS;
725 gst_osssink_factory_init (GstPlugin *plugin)
727 GstElementFactory *factory;
729 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
730 g_return_val_if_fail (factory != NULL, FALSE);
732 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
734 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));