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 = GST_ELEMENT_DETAILS (
37 "Output to a sound card via OSS",
38 "Erik Walthinsen <omega@cse.ogi.edu>, "
39 "Wim Taymans <wim.taymans@chello.be>"
42 static void gst_osssink_base_init (gpointer g_class);
43 static void gst_osssink_class_init (GstOssSinkClass *klass);
44 static void gst_osssink_init (GstOssSink *osssink);
45 static void gst_osssink_dispose (GObject *object);
47 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
48 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
49 static GstClock* gst_osssink_get_clock (GstElement *element);
50 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
52 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
53 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
54 GstFormat *dest_format, gint64 *dest_value);
55 static const GstQueryType* gst_osssink_get_query_types (GstPad *pad);
56 static gboolean gst_osssink_query (GstElement *element, GstQueryType type,
57 GstFormat *format, gint64 *value);
58 static gboolean gst_osssink_sink_query (GstPad *pad, GstQueryType type,
59 GstFormat *format, gint64 *value);
61 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps);
63 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
65 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
68 static void gst_osssink_chain (GstPad *pad,GstData *_data);
70 /* OssSink signals and args */
86 GST_PAD_TEMPLATE_FACTORY (osssink_sink_factory,
93 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
94 "signed", GST_PROPS_LIST (
95 GST_PROPS_BOOLEAN (FALSE),
96 GST_PROPS_BOOLEAN (TRUE)
98 "width", GST_PROPS_LIST (
102 "depth", GST_PROPS_LIST (
106 "rate", GST_PROPS_INT_RANGE (1000, 48000),
107 "channels", GST_PROPS_INT_RANGE (1, 2)
111 static GstElementClass *parent_class = NULL;
112 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
115 gst_osssink_get_type (void)
117 static GType osssink_type = 0;
120 static const GTypeInfo osssink_info = {
121 sizeof(GstOssSinkClass),
122 gst_osssink_base_init,
124 (GClassInitFunc)gst_osssink_class_init,
129 (GInstanceInitFunc)gst_osssink_init,
131 osssink_type = g_type_register_static (GST_TYPE_OSSELEMENT, "GstOssSink", &osssink_info, 0);
137 static GstBufferPool*
138 gst_osssink_get_bufferpool (GstPad *pad)
142 oss = GST_OSSSINK (gst_pad_get_parent(pad));
144 /* 6 buffers per chunk by default */
146 oss->sinkpool = gst_buffer_pool_get_default (oss->bufsize, 6);
148 return oss->sinkpool;
152 gst_osssink_dispose (GObject *object)
154 GstOssSink *osssink = (GstOssSink *) object;
156 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
158 G_OBJECT_CLASS (parent_class)->dispose (object);
162 gst_osssink_base_init (gpointer g_class)
164 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166 gst_element_class_set_details (element_class, &gst_osssink_details);
167 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (osssink_sink_factory));
170 gst_osssink_class_init (GstOssSinkClass *klass)
172 GObjectClass *gobject_class;
173 GstElementClass *gstelement_class;
175 gobject_class = (GObjectClass*)klass;
176 gstelement_class = (GstElementClass*)klass;
178 parent_class = g_type_class_ref(GST_TYPE_OSSELEMENT);
180 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
181 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
182 FALSE, G_PARAM_READWRITE));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
184 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
185 TRUE, G_PARAM_READWRITE));
186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
187 g_param_spec_int ("fragment", "Fragment",
188 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
189 0, G_MAXINT, 6, G_PARAM_READWRITE));
190 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
191 g_param_spec_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
192 0, G_MAXINT, 4096, G_PARAM_READWRITE));
193 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
194 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
195 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
197 gst_osssink_signals[SIGNAL_HANDOFF] =
198 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
199 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
200 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
202 gobject_class->set_property = gst_osssink_set_property;
203 gobject_class->get_property = gst_osssink_get_property;
204 gobject_class->dispose = gst_osssink_dispose;
206 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
207 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
208 gstelement_class->set_clock = gst_osssink_set_clock;
209 gstelement_class->get_clock = gst_osssink_get_clock;
214 gst_osssink_init (GstOssSink *osssink)
216 osssink->sinkpad = gst_pad_new_from_template (
217 GST_PAD_TEMPLATE_GET (osssink_sink_factory), "sink");
218 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
219 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
220 gst_pad_set_bufferpool_function (osssink->sinkpad, gst_osssink_get_bufferpool);
221 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
222 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
223 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
224 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
226 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
228 osssink->bufsize = 4096;
229 osssink->chunk_size = 4096;
230 osssink->resync = FALSE;
231 osssink->mute = FALSE;
232 osssink->sync = TRUE;
233 osssink->sinkpool = NULL;
234 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
235 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
236 osssink->handled = 0;
238 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
239 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
243 static GstPadLinkReturn
244 gst_osssink_sinkconnect (GstPad *pad, GstCaps *caps)
246 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
248 if (!GST_CAPS_IS_FIXED (caps))
249 return GST_PAD_LINK_DELAYED;
251 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
252 return GST_PAD_LINK_REFUSED;
254 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
255 return GST_PAD_LINK_REFUSED;
258 return GST_PAD_LINK_OK;
262 gst_osssink_get_delay (GstOssSink *osssink)
266 if (GST_OSSELEMENT (osssink)->fd == -1)
269 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
271 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
275 delay = (info.fragstotal * info.fragsize) - info.bytes;
282 gst_osssink_get_time (GstClock *clock, gpointer data)
284 GstOssSink *osssink = GST_OSSSINK (data);
288 if (!GST_OSSELEMENT (osssink)->bps)
291 delay = gst_osssink_get_delay (osssink);
293 /* sometimes delay is bigger than the number of bytes sent to the device,
294 * which screws up this calculation, we assume that everything is still
295 * in the device then */
296 if (((guint64)delay) > osssink->handled) {
297 delay = osssink->handled;
299 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
305 gst_osssink_get_clock (GstElement *element)
309 osssink = GST_OSSSINK (element);
311 return GST_CLOCK (osssink->provided_clock);
315 gst_osssink_set_clock (GstElement *element, GstClock *clock)
319 osssink = GST_OSSSINK (element);
321 osssink->clock = clock;
325 gst_osssink_chain (GstPad *pad, GstData *_data)
327 GstBuffer *buf = GST_BUFFER (_data);
329 GstClockTime buftime;
330 g_print ("OSS data\n");
331 /* this has to be an audio buffer */
332 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
334 if (GST_IS_EVENT (buf)) {
335 GstEvent *event = GST_EVENT (buf);
337 switch (GST_EVENT_TYPE (event)) {
339 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC);
340 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
341 gst_pad_event_default (pad, event);
343 case GST_EVENT_DISCONTINUOUS:
346 g_print ("OSS reset\n");
347 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET);
348 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
349 if (!gst_clock_handle_discont (osssink->clock, value))
350 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
351 osssink->handled = 0;
353 osssink->resync = TRUE;
354 g_print ("OSS reset done\n");
358 gst_pad_event_default (pad, event);
361 gst_event_unref (event);
365 if (!GST_OSSELEMENT (osssink)->bps) {
366 gst_buffer_unref (buf);
367 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
371 buftime = GST_BUFFER_TIMESTAMP (buf);
373 if (GST_OSSELEMENT (osssink)->fd >= 0) {
374 if (!osssink->mute) {
375 guchar *data = GST_BUFFER_DATA (buf);
376 gint size = GST_BUFFER_SIZE (buf);
379 if (osssink->clock) {
383 delay = gst_osssink_get_delay (osssink);
384 queued = delay * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
386 if (osssink->resync && osssink->sync) {
387 gst_audio_clock_set_active ((GstAudioClock*)osssink->provided_clock, TRUE);
388 gst_clock_handle_discont (osssink->clock, buftime - queued);
390 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;
423 g_print ("OSS data done\n");
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;