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);
49 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
50 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
51 static GstClock* gst_osssink_get_clock (GstElement *element);
52 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
54 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
55 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
56 GstFormat *dest_format, gint64 *dest_value);
57 static const GstQueryType* gst_osssink_get_query_types (GstPad *pad);
58 static gboolean gst_osssink_query (GstElement *element, GstQueryType type,
59 GstFormat *format, gint64 *value);
60 static gboolean gst_osssink_sink_query (GstPad *pad, GstQueryType type,
61 GstFormat *format, gint64 *value);
63 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
65 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
67 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
70 static void gst_osssink_chain (GstPad *pad,GstBuffer *buf);
72 /* OssSink signals and args */
88 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
95 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
96 "signed", GST_PROPS_LIST (
97 GST_PROPS_BOOLEAN (FALSE),
98 GST_PROPS_BOOLEAN (TRUE)
100 "width", GST_PROPS_LIST (
104 "depth", GST_PROPS_LIST (
108 "rate", GST_PROPS_INT_RANGE (1000, 48000),
109 "channels", GST_PROPS_INT_RANGE (1, 2)
113 static GstElementClass *parent_class = NULL;
114 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
117 gst_osssink_get_type (void)
119 static GType osssink_type = 0;
122 static const GTypeInfo osssink_info = {
123 sizeof(GstOssSinkClass),
126 (GClassInitFunc)gst_osssink_class_init,
131 (GInstanceInitFunc)gst_osssink_init,
133 osssink_type = g_type_register_static (GST_TYPE_OSSELEMENT, "GstOssSink", &osssink_info, 0);
139 static GstBufferPool*
140 gst_osssink_get_bufferpool (GstPad *pad)
144 oss = GST_OSSSINK (gst_pad_get_parent(pad));
146 /* 6 buffers per chunk by default */
148 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
150 return oss->sinkpool;
154 gst_osssink_dispose (GObject *object)
156 GstOssSink *osssink = (GstOssSink *) object;
158 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
160 G_OBJECT_CLASS (parent_class)->dispose (object);
164 gst_osssink_class_init (GstOssSinkClass *klass)
166 GObjectClass *gobject_class;
167 GstElementClass *gstelement_class;
169 gobject_class = (GObjectClass*)klass;
170 gstelement_class = (GstElementClass*)klass;
172 parent_class = g_type_class_ref(GST_TYPE_OSSELEMENT);
174 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
175 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
176 FALSE, 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_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
186 0, G_MAXINT, 4096, G_PARAM_READWRITE));
187 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
188 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
189 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
191 gst_osssink_signals[SIGNAL_HANDOFF] =
192 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
194 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
196 gobject_class->set_property = gst_osssink_set_property;
197 gobject_class->get_property = gst_osssink_get_property;
198 gobject_class->dispose = gst_osssink_dispose;
200 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
201 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
202 gstelement_class->set_clock = gst_osssink_set_clock;
203 gstelement_class->get_clock = gst_osssink_get_clock;
208 gst_osssink_init (GstOssSink *osssink)
210 osssink->sinkpad = gst_pad_new_from_template (
211 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
212 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
213 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
214 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
215 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
216 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
217 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
218 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
220 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
222 osssink->bufsize = 4096;
223 osssink->chunk_size = 4096;
224 osssink->resync = FALSE;
225 osssink->mute = FALSE;
226 osssink->sync = TRUE;
227 osssink->sinkpool = NULL;
228 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
229 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
230 osssink->handled = 0;
232 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
233 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
237 static GstPadLinkReturn
238 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
240 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
242 if (!GST_CAPS_IS_FIXED (caps))
243 return GST_PAD_LINK_DELAYED;
245 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
246 return GST_PAD_LINK_REFUSED;
248 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
249 return GST_PAD_LINK_REFUSED;
252 return GST_PAD_LINK_OK;
256 gst_osssink_get_delay (GstOssSink *osssink)
260 if (GST_OSSELEMENT (osssink)->fd == -1)
263 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
265 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
269 delay = (info.fragstotal * info.fragsize) - info.bytes;
276 gst_osssink_get_time (GstClock *clock, gpointer data)
278 GstOssSink *osssink = GST_OSSSINK (data);
282 if (!GST_OSSELEMENT (osssink)->bps)
285 delay = gst_osssink_get_delay (osssink);
287 /* sometimes delay is bigger than the number of bytes sent to the device,
288 * which screws up this calculation, we assume that everything is still
289 * in the device then */
290 if (((guint64)delay) > osssink->handled) {
291 delay = osssink->handled;
293 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
299 gst_osssink_get_clock (GstElement *element)
303 osssink = GST_OSSSINK (element);
305 return GST_CLOCK (osssink->provided_clock);
309 gst_osssink_set_clock (GstElement *element, GstClock *clock)
313 osssink = GST_OSSSINK (element);
315 osssink->clock = clock;
319 gst_osssink_chain (GstPad *pad, GstBuffer *buf)
322 GstClockTime buftime;
324 /* this has to be an audio buffer */
325 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
327 if (GST_IS_EVENT (buf)) {
328 GstEvent *event = GST_EVENT (buf);
330 switch (GST_EVENT_TYPE (event)) {
332 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC);
333 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
334 gst_pad_event_default (pad, event);
336 case GST_EVENT_DISCONTINUOUS:
340 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET);
341 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
342 if (!gst_clock_handle_discont (osssink->clock, value))
343 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
344 osssink->handled = 0;
346 osssink->resync = TRUE;
350 gst_pad_event_default (pad, event);
353 gst_event_unref (event);
357 if (!GST_OSSELEMENT (osssink)->bps) {
358 gst_buffer_unref (buf);
359 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
363 buftime = GST_BUFFER_TIMESTAMP (buf);
365 if (GST_OSSELEMENT (osssink)->fd >= 0) {
366 if (!osssink->mute) {
367 guchar *data = GST_BUFFER_DATA (buf);
368 gint size = GST_BUFFER_SIZE (buf);
371 if (osssink->clock) {
374 GstClockTimeDiff jitter;
376 delay = gst_osssink_get_delay (osssink);
377 queued = delay * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
379 if (osssink->resync && osssink->sync) {
380 GstClockID id = gst_clock_new_single_shot_id (osssink->clock, buftime - queued);
382 gst_element_clock_wait (GST_ELEMENT (osssink), id, &jitter);
383 gst_clock_id_free (id);
386 gst_clock_handle_discont (osssink->clock, buftime - queued + jitter);
388 gst_audio_clock_set_active ((GstAudioClock*)osssink->provided_clock, TRUE);
389 osssink->resync = FALSE;
396 /* no clock, try to be as fast as possible */
398 audio_buf_info ospace;
400 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &ospace);
402 if (ospace.bytes >= size) {
407 while (to_write > 0) {
408 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
409 MIN (to_write, osssink->chunk_size));
418 osssink->handled += done;
424 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, buftime);
426 gst_buffer_unref (buf);
429 static const GstFormat*
430 gst_osssink_get_formats (GstPad *pad)
432 static const GstFormat formats[] = {
442 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
443 GstFormat *dest_format, gint64 *dest_value)
447 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
449 return gst_osselement_convert (GST_OSSELEMENT (osssink),
450 src_format, src_value,
451 dest_format, dest_value);
454 static const GstQueryType*
455 gst_osssink_get_query_types (GstPad *pad)
457 static const GstQueryType query_types[] = {
466 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
471 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
474 case GST_QUERY_LATENCY:
475 if (!gst_osssink_convert (pad,
476 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
482 case GST_QUERY_POSITION:
483 if (!gst_osssink_convert (pad,
484 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
491 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
499 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
501 GstOssSink *osssink = GST_OSSSINK (element);
503 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
507 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
511 osssink = GST_OSSSINK (object);
515 osssink->mute = g_value_get_boolean (value);
516 g_object_notify (G_OBJECT (osssink), "mute");
519 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
520 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
522 case ARG_BUFFER_SIZE:
523 osssink->bufsize = g_value_get_uint (value);
524 osssink->sinkpool = gst_buffer_pool_get_default (osssink->bufsize, 6);
525 g_object_notify (object, "buffer_size");
528 osssink->sync = g_value_get_boolean (value);
529 g_object_notify (G_OBJECT (osssink), "sync");
532 osssink->chunk_size = g_value_get_uint (value);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
545 osssink = GST_OSSSINK (object);
549 g_value_set_boolean (value, osssink->mute);
552 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
554 case ARG_BUFFER_SIZE:
555 g_value_set_uint (value, osssink->bufsize);
558 g_value_set_boolean (value, osssink->sync);
561 g_value_set_uint (value, osssink->chunk_size);
564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569 static GstElementStateReturn
570 gst_osssink_change_state (GstElement *element)
574 osssink = GST_OSSSINK (element);
576 switch (GST_STATE_TRANSITION (element)) {
577 case GST_STATE_READY_TO_PAUSED:
579 case GST_STATE_PAUSED_TO_PLAYING:
580 osssink->resync = TRUE;
582 case GST_STATE_PLAYING_TO_PAUSED:
583 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
584 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
585 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
586 osssink->resync = TRUE;
588 case GST_STATE_PAUSED_TO_READY:
589 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
590 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
591 gst_osselement_reset (GST_OSSELEMENT (osssink));
597 if (GST_ELEMENT_CLASS (parent_class)->change_state)
598 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
600 return GST_STATE_SUCCESS;
604 gst_osssink_factory_init (GstPlugin *plugin)
606 GstElementFactory *factory;
608 factory = gst_element_factory_new ("osssink", GST_TYPE_OSSSINK, &gst_osssink_details);
609 g_return_val_if_fail (factory != NULL, FALSE);
611 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
613 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));