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 GST_DEBUG ("initializing osssink");
206 osssink->bufsize = 4096;
207 osssink->chunk_size = 4096;
208 osssink->mute = FALSE;
209 osssink->sync = TRUE;
210 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
211 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
212 osssink->handled = 0;
214 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
215 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
219 gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps)
222 GstStructure *structure;
224 newcaps = gst_caps_new_full (gst_structure_copy(gst_caps_get_structure (caps, 0)), NULL);
225 structure = gst_caps_get_structure (newcaps, 0);
227 if (gst_caps_structure_fixate_field_nearest_int (structure, "rate", 44100)) {
230 if (gst_caps_structure_fixate_field_nearest_int (structure, "depth", 16)) {
233 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 16)) {
236 if (gst_caps_structure_fixate_field_nearest_int (structure, "channels", 2)) {
240 gst_caps_free (newcaps);
245 static GstPadLinkReturn
246 gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps)
248 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
250 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
251 return GST_PAD_LINK_REFUSED;
253 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
254 return GST_PAD_LINK_REFUSED;
257 return GST_PAD_LINK_OK;
261 gst_osssink_get_delay (GstOssSink *osssink)
266 if (GST_OSSELEMENT (osssink)->fd == -1)
269 #ifdef SNDCTL_DSP_GETODELAY
270 ret = ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay);
276 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
280 delay = (info.fragstotal * info.fragsize) - info.bytes;
288 gst_osssink_get_time (GstClock *clock, gpointer data)
290 GstOssSink *osssink = GST_OSSSINK (data);
294 if (!GST_OSSELEMENT (osssink)->bps)
297 delay = gst_osssink_get_delay (osssink);
299 /* sometimes delay is bigger than the number of bytes sent to the device,
300 * which screws up this calculation, we assume that everything is still
302 * thomas: with proper handling of the return value, this doesn't seem to
303 * happen anymore, so remove the second code path after april 2004 */
306 } else if ((guint64) delay > osssink->handled) {
307 g_warning ("Delay %d > osssink->handled %" G_GUINT64_FORMAT
308 ", setting to osssink->handled",
309 delay, osssink->handled);
310 delay = osssink->handled;
312 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
318 gst_osssink_get_clock (GstElement *element)
322 osssink = GST_OSSSINK (element);
324 return GST_CLOCK (osssink->provided_clock);
328 gst_osssink_set_clock (GstElement *element, GstClock *clock)
332 osssink = GST_OSSSINK (element);
334 osssink->clock = clock;
338 gst_osssink_chain (GstPad *pad, GstData *_data)
340 GstBuffer *buf = GST_BUFFER (_data);
342 GstClockTime buftime, elementtime, soundtime;
347 /* this has to be an audio buffer */
348 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
350 if (GST_IS_EVENT (buf)) {
351 GstEvent *event = GST_EVENT (buf);
353 switch (GST_EVENT_TYPE (event)) {
355 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC, 0);
356 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
357 gst_pad_event_default (pad, event);
360 gst_pad_event_default (pad, event);
363 g_assert_not_reached ();
366 if (!GST_OSSELEMENT (osssink)->bps) {
367 gst_buffer_unref (buf);
368 GST_ELEMENT_ERROR (osssink, CORE, NEGOTIATION, (NULL), ("format wasn't negotiated before chain function"));
372 data = GST_BUFFER_DATA (buf);
373 to_write = GST_BUFFER_SIZE (buf);
374 /* sync audio with buffers timestamp */
375 elementtime = gst_element_get_time (GST_ELEMENT (osssink));
376 delay = gst_osssink_get_delay (osssink);
379 soundtime = elementtime + delay * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
380 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
381 buftime = GST_BUFFER_TIMESTAMP (buf);
385 GST_LOG_OBJECT (osssink, "time: element %"G_GUINT64_FORMAT", real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
386 elementtime, soundtime, buftime);
387 if (MAX (buftime, soundtime) - MIN (buftime, soundtime) > GST_SECOND / 10) {
388 /* we need to adjust to the buffers here */
389 GST_INFO_OBJECT (osssink, "need sync: element %"G_GUINT64_FORMAT", real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
390 elementtime, soundtime, buftime);
391 if (soundtime > buftime) {
393 guint throw_away = (soundtime - buftime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND;
394 throw_away &= ~(GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels);
395 osssink->handled += throw_away;
396 if (throw_away >= GST_BUFFER_SIZE (buf)) {
397 gst_data_unref (_data);
400 to_write -= throw_away;
403 guint64 to_handle = (buftime - soundtime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND
404 / GST_OSSELEMENT (osssink)->width / GST_OSSELEMENT (osssink)->channels
405 * GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels;
406 /* FIXME: we really should output silence here */
407 /* round to full frames */
408 g_usleep (to_handle * G_USEC_PER_SEC / GST_OSSELEMENT (osssink)->bps);
409 osssink->handled += (buftime - soundtime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND
410 / GST_OSSELEMENT (osssink)->width / GST_OSSELEMENT (osssink)->channels
411 * GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels;
415 if (GST_OSSELEMENT (osssink)->fd >= 0) {
416 if (!osssink->mute) {
418 while (to_write > 0) {
419 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
420 MIN (to_write, osssink->chunk_size));
429 osssink->handled += done;
433 g_warning ("muting osssinks unimplemented wrt clocks!");
437 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, gst_osssink_get_time (osssink->provided_clock, osssink));
439 gst_buffer_unref (buf);
442 static const GstFormat*
443 gst_osssink_get_formats (GstPad *pad)
445 static const GstFormat formats[] = {
455 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
456 GstFormat *dest_format, gint64 *dest_value)
460 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
462 return gst_osselement_convert (GST_OSSELEMENT (osssink),
463 src_format, src_value,
464 dest_format, dest_value);
467 static const GstQueryType*
468 gst_osssink_get_query_types (GstPad *pad)
470 static const GstQueryType query_types[] = {
479 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
484 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
487 case GST_QUERY_LATENCY:
488 if (!gst_osssink_convert (pad,
489 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
495 case GST_QUERY_POSITION:
496 if (!gst_osssink_convert (pad,
497 GST_FORMAT_TIME, gst_element_get_time (GST_ELEMENT (osssink)),
504 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
512 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
514 GstOssSink *osssink = GST_OSSSINK (element);
516 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
520 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
524 osssink = GST_OSSSINK (object);
528 osssink->mute = g_value_get_boolean (value);
529 g_object_notify (G_OBJECT (osssink), "mute");
532 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
533 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
535 case ARG_BUFFER_SIZE:
536 osssink->bufsize = g_value_get_uint (value);
537 g_object_notify (object, "buffer_size");
540 osssink->sync = g_value_get_boolean (value);
541 g_object_notify (G_OBJECT (osssink), "sync");
544 osssink->chunk_size = g_value_get_uint (value);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
557 osssink = GST_OSSSINK (object);
561 g_value_set_boolean (value, osssink->mute);
564 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
566 case ARG_BUFFER_SIZE:
567 g_value_set_uint (value, osssink->bufsize);
570 g_value_set_boolean (value, osssink->sync);
573 g_value_set_uint (value, osssink->chunk_size);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 static GstElementStateReturn
582 gst_osssink_change_state (GstElement *element)
586 osssink = GST_OSSSINK (element);
588 switch (GST_STATE_TRANSITION (element)) {
589 case GST_STATE_READY_TO_PAUSED:
591 case GST_STATE_PAUSED_TO_PLAYING:
592 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), TRUE);
594 case GST_STATE_PLAYING_TO_PAUSED:
595 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
596 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
597 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
599 case GST_STATE_PAUSED_TO_READY:
600 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
601 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
602 gst_osselement_reset (GST_OSSELEMENT (osssink));
603 osssink->handled = 0;
609 if (GST_ELEMENT_CLASS (parent_class)->change_state)
610 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
612 return GST_STATE_SUCCESS;