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.
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
31 #include "gstosssink.h"
33 /* elementfactory information */
34 static GstElementDetails gst_osssink_details = {
38 "Output to a sound card via OSS",
40 "Erik Walthinsen <omega@cse.ogi.edu>, "
41 "Wim Taymans <wim.taymans@chello.be>",
45 static void gst_osssink_class_init (GstOssSinkClass *klass);
46 static void gst_osssink_init (GstOssSink *osssink);
47 static void gst_osssink_dispose (GObject *object);
48 static void gst_osssink_finalize (GObject *object);
50 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
51 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
52 static GstClock* gst_osssink_get_clock (GstElement *element);
53 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
55 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
56 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
57 GstFormat *dest_format, gint64 *dest_value);
58 static const GstQueryType* gst_osssink_get_query_types (GstPad *pad);
59 static gboolean gst_osssink_query (GstElement *element, GstQueryType type,
60 GstFormat *format, gint64 *value);
61 static gboolean gst_osssink_sink_query (GstPad *pad, GstQueryType type,
62 GstFormat *format, gint64 *value);
64 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
66 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
68 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
71 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
73 /* OssSink signals and args */
90 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
97 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
98 "signed", GST_PROPS_LIST (
99 GST_PROPS_BOOLEAN (FALSE),
100 GST_PROPS_BOOLEAN (TRUE)
102 "width", GST_PROPS_LIST (
106 "depth", GST_PROPS_LIST (
110 "rate", GST_PROPS_INT_RANGE (1000, 48000),
111 "channels", GST_PROPS_INT_RANGE (1, 2)
115 static GstElementClass *parent_class = NULL;
116 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
119 gst_osssink_get_type (void)
121 static GType osssink_type = 0;
124 static const GTypeInfo osssink_info = {
125 sizeof(GstOssSinkClass),
128 (GClassInitFunc)gst_osssink_class_init,
133 (GInstanceInitFunc)gst_osssink_init,
135 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
141 static GstBufferPool*
142 gst_osssink_get_bufferpool (GstPad *pad)
146 oss = GST_OSSSINK (gst_pad_get_parent(pad));
148 /* 6 buffers per chunk by default */
150 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
152 return oss->sinkpool;
156 gst_osssink_dispose (GObject *object)
158 GstOssSink *osssink = (GstOssSink *) object;
160 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
162 G_OBJECT_CLASS (parent_class)->dispose (object);
166 gst_osssink_finalize (GObject *object)
168 GstOssSink *osssink = (GstOssSink *) object;
170 g_free (osssink->common.device);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
176 gst_osssink_class_init (GstOssSinkClass *klass)
178 GObjectClass *gobject_class;
179 GstElementClass *gstelement_class;
181 gobject_class = (GObjectClass*)klass;
182 gstelement_class = (GstElementClass*)klass;
184 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
187 g_param_spec_string ("device", "Device", "The device to use for output",
188 "/dev/dsp", G_PARAM_READWRITE));
189 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
190 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
191 FALSE, G_PARAM_READWRITE));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
193 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
194 TRUE, G_PARAM_READWRITE));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
196 g_param_spec_int ("fragment", "Fragment",
197 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
198 0, G_MAXINT, 6, G_PARAM_READWRITE));
199 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
200 g_param_spec_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
201 0, G_MAXINT, 4096, G_PARAM_READWRITE));
202 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
203 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
204 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
206 gst_osssink_signals[SIGNAL_HANDOFF] =
207 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
208 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
209 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
211 gobject_class->set_property = gst_osssink_set_property;
212 gobject_class->get_property = gst_osssink_get_property;
213 gobject_class->dispose = gst_osssink_dispose;
214 gobject_class->finalize = gst_osssink_finalize;
216 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
217 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
218 gstelement_class->set_clock = gst_osssink_set_clock;
219 gstelement_class->get_clock = gst_osssink_get_clock;
224 gst_osssink_init (GstOssSink *osssink)
226 osssink->sinkpad = gst_pad_new_from_template (
227 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
228 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
229 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
230 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
231 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
232 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
233 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
234 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
236 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
238 gst_osscommon_init (&osssink->common);
240 osssink->bufsize = 4096;
241 osssink->chunk_size = 4096;
242 osssink->resync = FALSE;
243 osssink->mute = 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 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
248 osssink->handled = 0;
250 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
251 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
255 static GstPadLinkReturn
256 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
258 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
260 if (!GST_CAPS_IS_FIXED (caps))
261 return GST_PAD_LINK_DELAYED;
263 if (!gst_osscommon_parse_caps (&osssink->common, caps))
264 return GST_PAD_LINK_REFUSED;
266 if (!gst_osscommon_sync_parms (&osssink->common)) {
267 return GST_PAD_LINK_REFUSED;
270 return GST_PAD_LINK_OK;
274 gst_osssink_get_delay (GstOssSink *osssink)
278 if (osssink->common.fd == -1)
281 if (ioctl (osssink->common.fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
283 if (ioctl (osssink->common.fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
287 delay = (info.fragstotal * info.fragsize) - info.bytes;
294 gst_osssink_get_time (GstClock *clock, gpointer data)
296 GstOssSink *osssink = GST_OSSSINK (data);
300 if (!osssink->common.bps)
303 delay = gst_osssink_get_delay (osssink);
305 /* sometimes delay is bigger than the number of bytes sent to the device,
306 * which screws up this calculation, we assume that everything is still
307 * in the device then */
308 if (((guint64)delay) > osssink->handled) {
309 delay = osssink->handled;
311 res = (osssink->handled - delay) * GST_SECOND / osssink->common.bps;
317 gst_osssink_get_clock (GstElement *element)
321 osssink = GST_OSSSINK (element);
323 return GST_CLOCK (osssink->provided_clock);
327 gst_osssink_set_clock (GstElement *element, GstClock *clock)
331 osssink = GST_OSSSINK (element);
333 osssink->clock = clock;
337 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
340 GstClockTime buftime;
342 /* this has to be an audio buffer */
343 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
345 if (GST_IS_EVENT (buf)) {
346 GstEvent *event = GST_EVENT (buf);
348 switch (GST_EVENT_TYPE (event)) {
350 ioctl (osssink->common.fd, SNDCTL_DSP_SYNC);
351 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
352 gst_pad_event_default (pad, event);
354 case GST_EVENT_DISCONTINUOUS:
358 ioctl (osssink->common.fd, SNDCTL_DSP_RESET);
359 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
360 if (!gst_clock_handle_discont (osssink->clock, value))
361 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
362 osssink->handled = 0;
364 osssink->resync = TRUE;
368 gst_pad_event_default (pad, event);
371 gst_event_unref (event);
375 if (!osssink->common.bps) {
376 gst_buffer_unref (buf);
377 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
381 buftime = GST_BUFFER_TIMESTAMP (buf);
383 if (osssink->common.fd >= 0) {
384 if (!osssink->mute) {
385 guchar *data = GST_BUFFER_DATA (buf);
386 gint size = GST_BUFFER_SIZE (buf);
389 if (osssink->clock) {
392 GstClockTimeDiff jitter;
394 delay = gst_osssink_get_delay (osssink);
395 queued = delay * GST_SECOND / osssink->common.bps;
397 if (osssink->resync && osssink->sync) {
398 GstClockID id = gst_clock_new_single_shot_id (osssink->clock, buftime - queued);
400 gst_element_clock_wait (GST_ELEMENT (osssink), id, &jitter);
401 gst_clock_id_free (id);
404 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
406 gst_oss_clock_set_active (osssink->provided_clock, TRUE);
407 osssink->resync = FALSE;
414 /* no clock, try to be as fast as possible */
416 audio_buf_info ospace;
418 ioctl (osssink->common.fd, SNDCTL_DSP_GETOSPACE, &ospace);
420 if (ospace.bytes >= size) {
425 while (to_write > 0) {
426 gint done = write (osssink->common.fd, data,
427 MIN (to_write, osssink->chunk_size));
436 osssink->handled += done;
443 gst_oss_clock_update_time (osssink->clock, buftime);
445 gst_buffer_unref (buf);
448 static const GstFormat*
449 gst_osssink_get_formats (GstPad *pad)
451 static const GstFormat formats[] = {
461 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
462 GstFormat *dest_format, gint64 *dest_value)
466 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
468 return gst_osscommon_convert (&osssink->common, src_format, src_value,
469 dest_format, dest_value);
472 static const GstQueryType*
473 gst_osssink_get_query_types (GstPad *pad)
475 static const GstQueryType query_types[] = {
484 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
489 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
492 case GST_QUERY_LATENCY:
493 if (!gst_osssink_convert (pad,
494 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
500 case GST_QUERY_POSITION:
501 if (!gst_osssink_convert (pad,
502 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
509 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
517 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
519 GstOssSink *osssink = GST_OSSSINK (element);
521 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
525 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
529 osssink = GST_OSSSINK (object);
533 /* disallow changing the device while it is opened
534 get_property("device") should return the right one */
535 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
537 g_free (osssink->common.device);
538 osssink->common.device = g_strdup (g_value_get_string (value));
539 g_object_notify (object, "device");
543 osssink->mute = g_value_get_boolean (value);
544 g_object_notify (G_OBJECT (osssink), "mute");
547 osssink->common.fragment = g_value_get_int (value);
548 gst_osscommon_sync_parms (&osssink->common);
550 case ARG_BUFFER_SIZE:
551 if (osssink->bufsize == g_value_get_int (value)) break;
552 osssink->bufsize = g_value_get_uint (value);
553 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
554 g_object_notify (object, "buffer_size");
557 osssink->sync = g_value_get_boolean (value);
558 g_object_notify (G_OBJECT (osssink), "sync");
561 osssink->chunk_size = g_value_get_uint (value);
564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
570 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
574 osssink = GST_OSSSINK (object);
578 g_value_set_string (value, osssink->common.device);
581 g_value_set_boolean (value, osssink->mute);
584 g_value_set_int (value, osssink->common.fragment);
586 case ARG_BUFFER_SIZE:
587 g_value_set_uint (value, osssink->bufsize);
590 g_value_set_boolean (value, osssink->sync);
593 g_value_set_uint (value, osssink->chunk_size);
596 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
601 static GstElementStateReturn
602 gst_osssink_change_state (GstElement *element)
606 osssink = GST_OSSSINK (element);
608 switch (GST_STATE_TRANSITION (element)) {
609 case GST_STATE_NULL_TO_READY:
610 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
613 if (!gst_osscommon_open_audio (&osssink->common, GST_OSSCOMMON_WRITE, &error)) {
614 gst_element_error (GST_ELEMENT (osssink), error);
616 return GST_STATE_FAILURE;
618 GST_FLAG_SET (element, GST_OSSSINK_OPEN);
621 case GST_STATE_READY_TO_PAUSED:
623 case GST_STATE_PAUSED_TO_PLAYING:
624 osssink->resync = TRUE;
626 case GST_STATE_PLAYING_TO_PAUSED:
628 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
629 ioctl (osssink->common.fd, SNDCTL_DSP_RESET, 0);
630 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
631 osssink->resync = TRUE;
634 case GST_STATE_PAUSED_TO_READY:
635 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
636 ioctl (osssink->common.fd, SNDCTL_DSP_RESET, 0);
637 gst_osscommon_reset (&osssink->common);
639 case GST_STATE_READY_TO_NULL:
640 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
641 gst_osscommon_close_audio (&osssink->common);
642 GST_FLAG_UNSET (osssink, GST_OSSSINK_OPEN);
644 GST_INFO ( "osssink: closed sound device");
649 if (GST_ELEMENT_CLASS (parent_class)->change_state)
650 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
652 return GST_STATE_SUCCESS;
656 gst_osssink_factory_init (GstPlugin *plugin)
658 GstElementFactory *factory;
660 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
661 g_return_val_if_fail (factory != NULL, FALSE);
663 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
665 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));