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 GstCaps * gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps);
62 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps);
64 static void gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value,
66 static void gst_osssink_get_property (GObject *object, guint prop_id, GValue *value,
69 static void gst_osssink_chain (GstPad *pad,GstData *_data);
71 /* OssSink signals and args */
87 static GstStaticPadTemplate osssink_sink_factory =
88 GST_STATIC_PAD_TEMPLATE (
92 GST_STATIC_CAPS ("audio/x-raw-int, "
93 "endianness = (int) BYTE_ORDER, "
94 "signed = (boolean) { TRUE, FALSE }, "
95 "width = (int) { 8, 16 }, "
96 "depth = (int) { 8, 16 }, "
97 "rate = (int) [ 1000, 48000 ], "
98 "channels = (int) [ 1, 2 ]"
102 static GstElementClass *parent_class = NULL;
103 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
106 gst_osssink_get_type (void)
108 static GType osssink_type = 0;
111 static const GTypeInfo osssink_info = {
112 sizeof(GstOssSinkClass),
113 gst_osssink_base_init,
115 (GClassInitFunc)gst_osssink_class_init,
120 (GInstanceInitFunc)gst_osssink_init,
122 osssink_type = g_type_register_static (GST_TYPE_OSSELEMENT, "GstOssSink", &osssink_info, 0);
129 gst_osssink_dispose (GObject *object)
131 GstOssSink *osssink = (GstOssSink *) object;
133 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
135 G_OBJECT_CLASS (parent_class)->dispose (object);
139 gst_osssink_base_init (gpointer g_class)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
143 gst_element_class_set_details (element_class, &gst_osssink_details);
144 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&osssink_sink_factory));
147 gst_osssink_class_init (GstOssSinkClass *klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
152 gobject_class = (GObjectClass*)klass;
153 gstelement_class = (GstElementClass*)klass;
155 parent_class = g_type_class_ref(GST_TYPE_OSSELEMENT);
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
158 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
159 FALSE, G_PARAM_READWRITE));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
161 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
162 TRUE, G_PARAM_READWRITE));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
164 g_param_spec_int ("fragment", "Fragment",
165 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
166 0, G_MAXINT, 6, G_PARAM_READWRITE));
167 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
168 g_param_spec_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
169 0, G_MAXINT, 4096, G_PARAM_READWRITE));
170 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
171 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
172 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
174 gst_osssink_signals[SIGNAL_HANDOFF] =
175 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
177 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
179 gobject_class->set_property = gst_osssink_set_property;
180 gobject_class->get_property = gst_osssink_get_property;
181 gobject_class->dispose = gst_osssink_dispose;
183 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
184 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
185 gstelement_class->set_clock = gst_osssink_set_clock;
186 gstelement_class->get_clock = gst_osssink_get_clock;
191 gst_osssink_init (GstOssSink *osssink)
193 osssink->sinkpad = gst_pad_new_from_template (
194 gst_static_pad_template_get (&osssink_sink_factory), "sink");
195 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
196 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
197 gst_pad_set_fixate_function (osssink->sinkpad, gst_osssink_sink_fixate);
198 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
199 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
200 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
201 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
203 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
205 osssink->bufsize = 4096;
206 osssink->chunk_size = 4096;
207 osssink->mute = FALSE;
208 osssink->sync = TRUE;
209 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
210 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
211 osssink->handled = 0;
213 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
214 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
218 gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps)
221 GstStructure *structure;
223 newcaps = gst_caps_new_full (gst_structure_copy(gst_caps_get_structure (caps, 0)), NULL);
224 structure = gst_caps_get_structure (newcaps, 0);
226 if (gst_caps_structure_fixate_field_nearest_int (structure, "rate", 44100)) {
229 if (gst_caps_structure_fixate_field_nearest_int (structure, "depth", 16)) {
232 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 16)) {
235 if (gst_caps_structure_fixate_field_nearest_int (structure, "channels", 2)) {
239 gst_caps_free (newcaps);
244 static GstPadLinkReturn
245 gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps)
247 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
249 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
250 return GST_PAD_LINK_REFUSED;
252 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
253 return GST_PAD_LINK_REFUSED;
256 return GST_PAD_LINK_OK;
260 gst_osssink_get_delay (GstOssSink *osssink)
264 if (GST_OSSELEMENT (osssink)->fd == -1)
267 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
269 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
273 delay = (info.fragstotal * info.fragsize) - info.bytes;
280 gst_osssink_get_time (GstClock *clock, gpointer data)
282 GstOssSink *osssink = GST_OSSSINK (data);
286 if (!GST_OSSELEMENT (osssink)->bps)
289 delay = gst_osssink_get_delay (osssink);
291 /* sometimes delay is bigger than the number of bytes sent to the device,
292 * which screws up this calculation, we assume that everything is still
293 * in the device then */
294 if (((guint64)delay) > osssink->handled) {
295 delay = osssink->handled;
297 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
303 gst_osssink_get_clock (GstElement *element)
307 osssink = GST_OSSSINK (element);
309 return GST_CLOCK (osssink->provided_clock);
313 gst_osssink_set_clock (GstElement *element, GstClock *clock)
317 osssink = GST_OSSSINK (element);
319 osssink->clock = clock;
323 gst_osssink_chain (GstPad *pad, GstData *_data)
325 GstBuffer *buf = GST_BUFFER (_data);
327 GstClockTime buftime;
329 /* this has to be an audio buffer */
330 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
332 if (GST_IS_EVENT (buf)) {
333 GstEvent *event = GST_EVENT (buf);
335 switch (GST_EVENT_TYPE (event)) {
337 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC);
338 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
339 gst_pad_event_default (pad, event);
342 gst_pad_event_default (pad, event);
345 gst_event_unref (event);
349 if (!GST_OSSELEMENT (osssink)->bps) {
350 gst_buffer_unref (buf);
351 gst_element_error (GST_ELEMENT (osssink), "capsnego was never performed, unknown data type");
355 buftime = GST_BUFFER_TIMESTAMP (buf);
357 if (GST_OSSELEMENT (osssink)->fd >= 0) {
358 if (!osssink->mute) {
359 guchar *data = GST_BUFFER_DATA (buf);
360 gint to_write = GST_BUFFER_SIZE (buf);
362 while (to_write > 0) {
363 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
364 MIN (to_write, osssink->chunk_size));
373 osssink->handled += done;
377 g_warning ("muting osssinks unimplemented wrt clocks!");
381 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, buftime);
383 gst_buffer_unref (buf);
386 static const GstFormat*
387 gst_osssink_get_formats (GstPad *pad)
389 static const GstFormat formats[] = {
399 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
400 GstFormat *dest_format, gint64 *dest_value)
404 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
406 return gst_osselement_convert (GST_OSSELEMENT (osssink),
407 src_format, src_value,
408 dest_format, dest_value);
411 static const GstQueryType*
412 gst_osssink_get_query_types (GstPad *pad)
414 static const GstQueryType query_types[] = {
423 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
428 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
431 case GST_QUERY_LATENCY:
432 if (!gst_osssink_convert (pad,
433 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
439 case GST_QUERY_POSITION:
440 if (!gst_osssink_convert (pad,
441 GST_FORMAT_TIME, gst_clock_get_time (osssink->provided_clock),
448 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
456 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
458 GstOssSink *osssink = GST_OSSSINK (element);
460 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
464 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
468 osssink = GST_OSSSINK (object);
472 osssink->mute = g_value_get_boolean (value);
473 g_object_notify (G_OBJECT (osssink), "mute");
476 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
477 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
479 case ARG_BUFFER_SIZE:
480 osssink->bufsize = g_value_get_uint (value);
481 g_object_notify (object, "buffer_size");
484 osssink->sync = g_value_get_boolean (value);
485 g_object_notify (G_OBJECT (osssink), "sync");
488 osssink->chunk_size = g_value_get_uint (value);
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
497 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
501 osssink = GST_OSSSINK (object);
505 g_value_set_boolean (value, osssink->mute);
508 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
510 case ARG_BUFFER_SIZE:
511 g_value_set_uint (value, osssink->bufsize);
514 g_value_set_boolean (value, osssink->sync);
517 g_value_set_uint (value, osssink->chunk_size);
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525 static GstElementStateReturn
526 gst_osssink_change_state (GstElement *element)
530 osssink = GST_OSSSINK (element);
532 switch (GST_STATE_TRANSITION (element)) {
533 case GST_STATE_READY_TO_PAUSED:
535 case GST_STATE_PAUSED_TO_PLAYING:
537 case GST_STATE_PLAYING_TO_PAUSED:
538 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
539 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
540 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
542 case GST_STATE_PAUSED_TO_READY:
543 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
544 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
545 gst_osselement_reset (GST_OSSELEMENT (osssink));
551 if (GST_ELEMENT_CLASS (parent_class)->change_state)
552 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
554 return GST_STATE_SUCCESS;