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.
23 #include <sys/ioctl.h>
24 #include <sys/soundcard.h>
26 #include <gstosssink.h>
28 /* elementfactory information */
29 static GstElementDetails gst_osssink_details = {
33 "Output to a sound card via OSS",
35 "Erik Walthinsen <omega@cse.ogi.edu>, "
36 "Wim Taymans <wim.taymans@chello.be>",
40 static void gst_osssink_class_init (GstOssSinkClass *klass);
41 static void gst_osssink_init (GstOssSink *osssink);
42 static void gst_osssink_finalize (GObject *object);
44 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
45 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
46 static GstClock* gst_osssink_get_clock (GstElement *element);
47 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
49 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
50 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
51 GstFormat *dest_format, gint64 *dest_value);
52 static const GstPadQueryType* gst_osssink_get_query_types (GstPad *pad);
53 static gboolean gst_osssink_query (GstElement *element, GstPadQueryType type,
54 GstFormat *format, gint64 *value);
55 static gboolean gst_osssink_sink_query (GstPad *pad, GstPadQueryType type,
56 GstFormat *format, gint64 *value);
58 static GstPadConnectReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
60 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
62 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
65 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
67 /* 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 static GstElementClass *parent_class = NULL;
111 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
114 gst_osssink_get_type (void)
116 static GType osssink_type = 0;
119 static const GTypeInfo osssink_info = {
120 sizeof(GstOssSinkClass),
123 (GClassInitFunc)gst_osssink_class_init,
128 (GInstanceInitFunc)gst_osssink_init,
130 osssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssSink", &osssink_info, 0);
136 static GstBufferPool*
137 gst_osssink_get_bufferpool (GstPad *pad)
141 oss = GST_OSSSINK (gst_pad_get_parent(pad));
143 /* 6 buffers per chunk by default */
145 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
147 return oss->sinkpool;
151 gst_osssink_finalize (GObject *object)
153 GstOssSink *osssink = (GstOssSink *) object;
155 g_free (osssink->common.device);
157 G_OBJECT_CLASS (parent_class)->finalize (object);
161 gst_osssink_class_init (GstOssSinkClass *klass)
163 GObjectClass *gobject_class;
164 GstElementClass *gstelement_class;
166 gobject_class = (GObjectClass*)klass;
167 gstelement_class = (GstElementClass*)klass;
169 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
171 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
172 g_param_spec_string ("device", "Device", "The device to use for output",
173 "/dev/dsp", G_PARAM_READWRITE)); /* CHECKME! */
174 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
175 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
176 TRUE, G_PARAM_READWRITE));
177 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
178 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
179 TRUE, G_PARAM_READWRITE));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
181 g_param_spec_int ("fragment", "Fragment",
182 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
183 0, G_MAXINT, 6, G_PARAM_READWRITE));
184 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
185 g_param_spec_int ("buffer_size", "Buffer size", "The buffer size",
186 0, G_MAXINT, 4096, G_PARAM_READWRITE));
188 gst_osssink_signals[SIGNAL_HANDOFF] =
189 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
190 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
191 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
193 gobject_class->set_property = gst_osssink_set_property;
194 gobject_class->get_property = gst_osssink_get_property;
195 gobject_class->finalize = gst_osssink_finalize;
197 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
198 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
199 gstelement_class->set_clock = gst_osssink_set_clock;
200 gstelement_class->get_clock = gst_osssink_get_clock;
205 gst_osssink_init (GstOssSink *osssink)
207 osssink->sinkpad = gst_pad_new_from_template (
208 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
209 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
210 gst_pad_set_connect_function (osssink->sinkpad, gst_osssink_sinkconnect);
211 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
212 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
213 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
214 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
215 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
217 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
219 gst_osscommon_init (&osssink->common);
221 osssink->bufsize = 4096;
222 osssink->resync = FALSE;
223 osssink->sync = TRUE;
224 osssink->sinkpool = NULL;
225 osssink->provided_clock = GST_CLOCK (gst_oss_clock_new ("ossclock", gst_osssink_get_time, osssink));
226 osssink->handled = 0;
228 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
229 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
233 static GstPadConnectReturn
234 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
236 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
238 if (!GST_CAPS_IS_FIXED (caps))
239 return GST_PAD_CONNECT_DELAYED;
241 if (!gst_osscommon_parse_caps (&osssink->common, caps))
242 return GST_PAD_CONNECT_REFUSED;
244 if (!gst_osscommon_sync_parms (&osssink->common)) {
245 return GST_PAD_CONNECT_REFUSED;
248 return GST_PAD_CONNECT_OK;
252 gst_osssink_get_delay (GstOssSink *osssink)
256 if (osssink->common.fd == -1)
259 if (ioctl (osssink->common.fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
261 if (ioctl (osssink->common.fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
265 delay = (info.fragstotal * info.fragsize) - info.bytes;
272 gst_osssink_get_time (GstClock *clock, gpointer data)
274 GstOssSink *osssink = GST_OSSSINK (data);
278 if (!osssink->common.bps)
281 delay = gst_osssink_get_delay (osssink);
283 /* sometimes delay is bigger than the number of bytes sent to the device,
284 * which screws up this calculation, we assume that everything is still
285 * in the device then */
286 if (((guint64)delay) > osssink->handled) {
287 delay = osssink->handled;
289 res = (osssink->handled - delay) * GST_SECOND / osssink->common.bps;
295 gst_osssink_get_clock (GstElement *element)
299 osssink = GST_OSSSINK (element);
301 return GST_CLOCK (osssink->provided_clock);
305 gst_osssink_set_clock (GstElement *element, GstClock *clock)
309 osssink = GST_OSSSINK (element);
311 osssink->clock = clock;
315 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
318 GstClockTime buftime;
320 /* this has to be an audio buffer */
321 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
323 if (GST_IS_EVENT (buf)) {
324 GstEvent *event = GST_EVENT (buf);
326 switch (GST_EVENT_TYPE (event)) {
328 ioctl (osssink->common.fd, SNDCTL_DSP_SYNC);
329 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
330 gst_pad_event_default (pad, event);
332 case GST_EVENT_NEW_MEDIA:
333 g_print ("new media\n");
335 case GST_EVENT_DISCONTINUOUS:
339 ioctl (osssink->common.fd, SNDCTL_DSP_RESET);
340 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
341 if (!gst_clock_handle_discont (osssink->clock, value))
342 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
343 osssink->handled = 0;
345 osssink->resync = TRUE;
349 gst_pad_event_default (pad, event);
352 gst_event_unref (event);
356 if (!osssink->common.bps) {
357 gst_buffer_unref (buf);
358 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
362 buftime = GST_BUFFER_TIMESTAMP (buf);
364 if (osssink->common.fd >= 0) {
365 if (!osssink->mute) {
366 guchar *data = GST_BUFFER_DATA (buf);
367 gint size = GST_BUFFER_SIZE (buf);
369 if (osssink->clock) {
372 GstClockTimeDiff jitter;
374 delay = gst_osssink_get_delay (osssink);
375 queued = delay * GST_SECOND / osssink->common.bps;
377 if (osssink->resync && osssink->sync) {
378 gst_element_clock_wait (GST_ELEMENT (osssink), osssink->clock,
379 buftime - queued, &jitter);
382 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
383 write (osssink->common.fd, data, size);
384 gst_oss_clock_set_active (osssink->provided_clock, TRUE);
385 osssink->resync = FALSE;
386 osssink->handled += size;
390 write (osssink->common.fd, data, size);
391 osssink->handled += size;
394 /* no clock, try to be as fast as possible */
396 audio_buf_info ospace;
398 ioctl (osssink->common.fd, SNDCTL_DSP_GETOSPACE, &ospace);
400 if (ospace.bytes >= size) {
401 write (osssink->common.fd, data, size);
406 gst_buffer_unref (buf);
409 static const GstFormat*
410 gst_osssink_get_formats (GstPad *pad)
412 static const GstFormat formats[] = {
422 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
423 GstFormat *dest_format, gint64 *dest_value)
427 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
429 return gst_osscommon_convert (&osssink->common, src_format, src_value,
430 dest_format, dest_value);
433 static const GstPadQueryType*
434 gst_osssink_get_query_types (GstPad *pad)
436 static const GstPadQueryType query_types[] = {
437 GST_PAD_QUERY_LATENCY,
438 GST_PAD_QUERY_POSITION,
445 gst_osssink_sink_query (GstPad *pad, GstPadQueryType type, GstFormat *format, gint64 *value)
450 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
453 case GST_PAD_QUERY_LATENCY:
454 if (!gst_osssink_convert (pad,
455 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
461 case GST_PAD_QUERY_POSITION:
462 if (!gst_osssink_convert (pad,
463 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
470 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
478 gst_osssink_query (GstElement *element, GstPadQueryType type, GstFormat *format, gint64 *value)
480 GstOssSink *osssink = GST_OSSSINK (element);
482 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
486 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
490 osssink = GST_OSSSINK (object);
494 /* disallow changing the device while it is opened
495 get_property("device") should return the right one */
496 if (!GST_FLAG_IS_SET (osssink, GST_OSSSINK_OPEN))
498 g_free (osssink->common.device);
499 osssink->common.device = g_strdup (g_value_get_string (value));
500 g_object_notify (object, "device");
504 osssink->mute = g_value_get_boolean (value);
505 g_object_notify (G_OBJECT (osssink), "mute");
508 osssink->common.fragment = g_value_get_int (value);
509 gst_osscommon_sync_parms (&osssink->common);
511 case ARG_BUFFER_SIZE:
512 if (osssink->bufsize == g_value_get_int (value)) break;
513 osssink->bufsize = g_value_get_int (value);
514 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
515 g_object_notify (object, "buffer_size");
518 osssink->sync = g_value_get_boolean (value);
519 g_object_notify (G_OBJECT (osssink), "sync");
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
532 osssink = GST_OSSSINK (object);
536 g_value_set_string (value, osssink->common.device);
539 g_value_set_boolean (value, osssink->mute);
542 g_value_set_int (value, osssink->common.fragment);
544 case ARG_BUFFER_SIZE:
545 g_value_set_int (value, osssink->bufsize);
548 g_value_set_boolean (value, osssink->sync);
551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556 static GstElementStateReturn
557 gst_osssink_change_state (GstElement *element)
561 osssink = GST_OSSSINK (element);
563 switch (GST_STATE_TRANSITION (element)) {
564 case GST_STATE_NULL_TO_READY:
565 if (!GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
568 if (!gst_osscommon_open_audio (&osssink->common, GST_OSSCOMMON_WRITE, &error)) {
569 gst_element_error (GST_ELEMENT (osssink), error);
571 return GST_STATE_FAILURE;
573 GST_FLAG_SET (element, GST_OSSSINK_OPEN);
576 case GST_STATE_READY_TO_PAUSED:
578 case GST_STATE_PAUSED_TO_PLAYING:
579 osssink->resync = TRUE;
581 case GST_STATE_PLAYING_TO_PAUSED:
583 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
584 ioctl (osssink->common.fd, SNDCTL_DSP_RESET, 0);
585 gst_oss_clock_set_active (osssink->provided_clock, FALSE);
586 osssink->resync = TRUE;
589 case GST_STATE_PAUSED_TO_READY:
590 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
591 ioctl (osssink->common.fd, SNDCTL_DSP_RESET, 0);
592 gst_osscommon_reset (&osssink->common);
594 case GST_STATE_READY_TO_NULL:
595 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN)) {
596 gst_osscommon_close_audio (&osssink->common);
597 GST_FLAG_UNSET (osssink, GST_OSSSINK_OPEN);
599 GST_INFO (GST_CAT_PLUGIN_INFO, "osssink: closed sound device");
604 if (GST_ELEMENT_CLASS (parent_class)->change_state)
605 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
607 return GST_STATE_SUCCESS;
611 gst_osssink_factory_init (GstPlugin *plugin)
613 GstElementFactory *factory;
615 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
616 g_return_val_if_fail (factory != NULL, FALSE);
618 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
620 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));