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>
32 #include "gstosssink.h"
34 /* elementfactory information */
35 static GstElementDetails gst_osssink_details = GST_ELEMENT_DETAILS (
38 "Output to a sound card via OSS",
39 "Erik Walthinsen <omega@cse.ogi.edu>, "
40 "Wim Taymans <wim.taymans@chello.be>"
43 static void gst_osssink_base_init (gpointer g_class);
44 static void gst_osssink_class_init (GstOssSinkClass *klass);
45 static void gst_osssink_init (GstOssSink *osssink);
46 static void gst_osssink_dispose (GObject *object);
48 static GstElementStateReturn gst_osssink_change_state (GstElement *element);
49 static void gst_osssink_set_clock (GstElement *element, GstClock *clock);
50 static GstClock* gst_osssink_get_clock (GstElement *element);
51 static GstClockTime gst_osssink_get_time (GstClock *clock, gpointer data);
53 static const GstFormat* gst_osssink_get_formats (GstPad *pad);
54 static gboolean gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
55 GstFormat *dest_format, gint64 *dest_value);
56 static const GstQueryType* gst_osssink_get_query_types (GstPad *pad);
57 static gboolean gst_osssink_query (GstElement *element, GstQueryType type,
58 GstFormat *format, gint64 *value);
59 static gboolean gst_osssink_sink_query (GstPad *pad, GstQueryType type,
60 GstFormat *format, gint64 *value);
62 static GstCaps * gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps);
63 static GstPadLinkReturn gst_osssink_sinkconnect (GstPad *pad, const 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,GstData *_data);
72 /* OssSink signals and args */
88 static GstStaticPadTemplate osssink_sink_factory =
89 GST_STATIC_PAD_TEMPLATE (
93 GST_STATIC_CAPS ("audio/x-raw-int, "
94 "endianness = (int) BYTE_ORDER, "
95 "signed = (boolean) { TRUE, FALSE }, "
96 "width = (int) { 8, 16 }, "
97 "depth = (int) { 8, 16 }, "
98 "rate = (int) [ 1000, 48000 ], "
99 "channels = (int) [ 1, 2 ]"
103 static GstElementClass *parent_class = NULL;
104 static guint gst_osssink_signals[LAST_SIGNAL] = { 0 };
107 gst_osssink_get_type (void)
109 static GType osssink_type = 0;
112 static const GTypeInfo osssink_info = {
113 sizeof(GstOssSinkClass),
114 gst_osssink_base_init,
116 (GClassInitFunc)gst_osssink_class_init,
121 (GInstanceInitFunc)gst_osssink_init,
123 osssink_type = g_type_register_static (GST_TYPE_OSSELEMENT, "GstOssSink", &osssink_info, 0);
130 gst_osssink_dispose (GObject *object)
132 GstOssSink *osssink = (GstOssSink *) object;
134 gst_object_unparent (GST_OBJECT (osssink->provided_clock));
136 G_OBJECT_CLASS (parent_class)->dispose (object);
140 gst_osssink_base_init (gpointer g_class)
142 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
144 gst_element_class_set_details (element_class, &gst_osssink_details);
145 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&osssink_sink_factory));
148 gst_osssink_class_init (GstOssSinkClass *klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
153 gobject_class = (GObjectClass*)klass;
154 gstelement_class = (GstElementClass*)klass;
156 parent_class = g_type_class_ref(GST_TYPE_OSSELEMENT);
158 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MUTE,
159 g_param_spec_boolean ("mute", "Mute", "Mute the audio",
160 FALSE, G_PARAM_READWRITE));
161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
162 g_param_spec_boolean ("sync", "Sync", "If syncing on timestamps should be enabled",
163 TRUE, G_PARAM_READWRITE));
164 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAGMENT,
165 g_param_spec_int ("fragment", "Fragment",
166 "The fragment as 0xMMMMSSSS (MMMM = total fragments, 2^SSSS = fragment size)",
167 0, G_MAXINT, 6, G_PARAM_READWRITE));
168 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_SIZE,
169 g_param_spec_uint ("buffer_size", "Buffer size", "Size of buffers in osssink's bufferpool (bytes)",
170 0, G_MAXINT, 4096, G_PARAM_READWRITE));
171 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHUNK_SIZE,
172 g_param_spec_uint ("chunk_size", "Chunk size", "Write data in chunk sized buffers",
173 0, G_MAXUINT, 4096, G_PARAM_READWRITE));
175 gst_osssink_signals[SIGNAL_HANDOFF] =
176 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
177 G_STRUCT_OFFSET (GstOssSinkClass, handoff), NULL, NULL,
178 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
180 gobject_class->set_property = gst_osssink_set_property;
181 gobject_class->get_property = gst_osssink_get_property;
182 gobject_class->dispose = gst_osssink_dispose;
184 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_osssink_change_state);
185 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_osssink_query);
186 gstelement_class->set_clock = gst_osssink_set_clock;
187 gstelement_class->get_clock = gst_osssink_get_clock;
192 gst_osssink_init (GstOssSink *osssink)
194 osssink->sinkpad = gst_pad_new_from_template (
195 gst_static_pad_template_get (&osssink_sink_factory), "sink");
196 gst_element_add_pad (GST_ELEMENT (osssink), osssink->sinkpad);
197 gst_pad_set_link_function (osssink->sinkpad, gst_osssink_sinkconnect);
198 gst_pad_set_fixate_function (osssink->sinkpad, gst_osssink_sink_fixate);
199 gst_pad_set_convert_function (osssink->sinkpad, gst_osssink_convert);
200 gst_pad_set_query_function (osssink->sinkpad, gst_osssink_sink_query);
201 gst_pad_set_query_type_function (osssink->sinkpad, gst_osssink_get_query_types);
202 gst_pad_set_formats_function (osssink->sinkpad, gst_osssink_get_formats);
204 gst_pad_set_chain_function (osssink->sinkpad, gst_osssink_chain);
206 GST_DEBUG ("initializing osssink");
207 osssink->bufsize = 4096;
208 osssink->chunk_size = 4096;
209 osssink->mute = FALSE;
210 osssink->sync = TRUE;
211 osssink->resync = TRUE;
212 osssink->provided_clock = gst_audio_clock_new ("ossclock", gst_osssink_get_time, osssink);
213 gst_object_set_parent (GST_OBJECT (osssink->provided_clock), GST_OBJECT (osssink));
214 osssink->handled = 0;
216 GST_FLAG_SET (osssink, GST_ELEMENT_THREAD_SUGGESTED);
217 GST_FLAG_SET (osssink, GST_ELEMENT_EVENT_AWARE);
221 gst_osssink_sink_fixate (GstPad *pad, const GstCaps *caps)
224 GstStructure *structure;
226 newcaps = gst_caps_new_full (gst_structure_copy(gst_caps_get_structure (caps, 0)), NULL);
227 structure = gst_caps_get_structure (newcaps, 0);
229 if (gst_caps_structure_fixate_field_nearest_int (structure, "rate", 44100)) {
232 if (gst_caps_structure_fixate_field_nearest_int (structure, "depth", 16)) {
235 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 16)) {
238 if (gst_caps_structure_fixate_field_nearest_int (structure, "channels", 2)) {
242 gst_caps_free (newcaps);
247 static GstPadLinkReturn
248 gst_osssink_sinkconnect (GstPad *pad, const GstCaps *caps)
250 GstOssSink *osssink = GST_OSSSINK (gst_pad_get_parent (pad));
252 if (!gst_osselement_parse_caps (GST_OSSELEMENT (osssink), caps))
253 return GST_PAD_LINK_REFUSED;
255 if (!gst_osselement_sync_parms (GST_OSSELEMENT (osssink))) {
256 return GST_PAD_LINK_REFUSED;
259 return GST_PAD_LINK_OK;
263 gst_osssink_get_delay (GstOssSink *osssink)
268 if (GST_OSSELEMENT (osssink)->fd == -1)
271 #ifdef SNDCTL_DSP_GETODELAY
272 ret = ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay);
278 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
282 delay = (info.fragstotal * info.fragsize) - info.bytes;
290 gst_osssink_get_time (GstClock *clock, gpointer data)
292 GstOssSink *osssink = GST_OSSSINK (data);
294 GstClockTimeDiff res;
296 if (!GST_OSSELEMENT (osssink)->bps)
299 delay = gst_osssink_get_delay (osssink);
301 /* sometimes delay is bigger than the number of bytes sent to the device,
302 * which screws up this calculation, we assume that everything is still
304 * thomas: with proper handling of the return value, this doesn't seem to
305 * happen anymore, so remove the second code path after april 2004 */
306 if (delay > (gint64) 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 = ((gint64) osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
320 gst_osssink_get_clock (GstElement *element)
324 osssink = GST_OSSSINK (element);
326 return GST_CLOCK (osssink->provided_clock);
330 gst_osssink_set_clock (GstElement *element, GstClock *clock)
334 osssink = GST_OSSSINK (element);
336 osssink->clock = clock;
340 gst_osssink_chain (GstPad *pad, GstData *_data)
342 GstBuffer *buf = GST_BUFFER (_data);
344 GstClockTimeDiff buftime, soundtime, elementtime;
349 /* this has to be an audio buffer */
350 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
352 if (GST_IS_EVENT (buf)) {
353 GstEvent *event = GST_EVENT (buf);
355 switch (GST_EVENT_TYPE (event)) {
357 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC, 0);
358 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
359 gst_pad_event_default (pad, event);
361 case GST_EVENT_DISCONTINUOUS:
362 osssink->resync = TRUE;
365 gst_pad_event_default (pad, event);
368 g_assert_not_reached ();
371 if (!GST_OSSELEMENT (osssink)->bps) {
372 gst_buffer_unref (buf);
373 GST_ELEMENT_ERROR (osssink, CORE, NEGOTIATION, (NULL), ("format wasn't negotiated before chain function"));
377 data = GST_BUFFER_DATA (buf);
378 to_write = GST_BUFFER_SIZE (buf);
379 /* sync audio with buffers timestamp. elementtime is the *current* time.
380 * soundtime is the time if the soundcard has processed all queued data. */
381 elementtime = gst_element_get_time (GST_ELEMENT (osssink));
382 delay = gst_osssink_get_delay (osssink);
385 soundtime = elementtime + delay * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
386 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
387 buftime = GST_BUFFER_TIMESTAMP (buf);
391 GST_LOG_OBJECT (osssink, "time: real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
393 if (MAX (buftime, soundtime) - MIN (buftime, soundtime) > (GST_SECOND / 10)) {
394 /* we need to adjust to the buffers here */
395 GST_INFO_OBJECT (osssink, "need sync: real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
397 if (soundtime > buftime) {
398 /* do *not* throw frames out. It's useless. The next frame will come in
399 * too late. And the next one. And so on. We don't want to lose sound.
400 * This is a placeholder for what - some day - should become QoS, i.e.
401 * sending events upstream to drop buffers. */
403 guint64 to_handle = (((buftime - soundtime) * GST_OSSELEMENT(osssink)->bps / GST_SECOND) /
404 ((GST_OSSELEMENT (osssink)->width / 8) * GST_OSSELEMENT (osssink)->channels)) *
405 (GST_OSSELEMENT (osssink)->width / 8) * GST_OSSELEMENT (osssink)->channels;
407 if (!osssink->resync) {
408 guint8 *buf = g_new (guint8, to_handle);
410 memset (buf, (GST_OSSELEMENT (osssink)->width == 8) ? 0 : 128, to_handle);
411 while (to_handle > 0) {
412 gint done = write (GST_OSSELEMENT (osssink)->fd, buf,
413 MIN (to_handle, osssink->chunk_size));
414 if (done == -1 && errno != EINTR) {
418 osssink->handled += done;
423 /* Timestamps at start-of-stream (MPEG) or after seek (hey,
424 * again MPEG!) can be borken, therefore this hacklet. */
425 osssink->handled += to_handle;
430 if (GST_OSSELEMENT (osssink)->fd >= 0 && to_write > 0) {
431 if (!osssink->mute) {
433 while (to_write > 0) {
434 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
435 MIN (to_write, osssink->chunk_size));
443 osssink->handled += done;
447 g_warning ("muting osssinks unimplemented wrt clocks!");
451 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, gst_osssink_get_time (osssink->provided_clock, osssink));
453 gst_buffer_unref (buf);
456 static const GstFormat*
457 gst_osssink_get_formats (GstPad *pad)
459 static const GstFormat formats[] = {
469 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
470 GstFormat *dest_format, gint64 *dest_value)
474 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
476 return gst_osselement_convert (GST_OSSELEMENT (osssink),
477 src_format, src_value,
478 dest_format, dest_value);
481 static const GstQueryType*
482 gst_osssink_get_query_types (GstPad *pad)
484 static const GstQueryType query_types[] = {
493 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
498 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
501 case GST_QUERY_LATENCY:
502 if (!gst_osssink_convert (pad,
503 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
509 case GST_QUERY_POSITION:
510 if (!gst_osssink_convert (pad,
511 GST_FORMAT_TIME, gst_element_get_time (GST_ELEMENT (osssink)),
518 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
526 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
528 GstOssSink *osssink = GST_OSSSINK (element);
530 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
534 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
538 osssink = GST_OSSSINK (object);
542 osssink->mute = g_value_get_boolean (value);
543 g_object_notify (G_OBJECT (osssink), "mute");
546 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
547 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
549 case ARG_BUFFER_SIZE:
550 osssink->bufsize = g_value_get_uint (value);
551 g_object_notify (object, "buffer_size");
554 osssink->sync = g_value_get_boolean (value);
555 g_object_notify (G_OBJECT (osssink), "sync");
558 osssink->chunk_size = g_value_get_uint (value);
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
571 osssink = GST_OSSSINK (object);
575 g_value_set_boolean (value, osssink->mute);
578 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
580 case ARG_BUFFER_SIZE:
581 g_value_set_uint (value, osssink->bufsize);
584 g_value_set_boolean (value, osssink->sync);
587 g_value_set_uint (value, osssink->chunk_size);
590 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595 static GstElementStateReturn
596 gst_osssink_change_state (GstElement *element)
600 osssink = GST_OSSSINK (element);
602 switch (GST_STATE_TRANSITION (element)) {
603 case GST_STATE_READY_TO_PAUSED:
605 case GST_STATE_PAUSED_TO_PLAYING:
606 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), TRUE);
608 case GST_STATE_PLAYING_TO_PAUSED:
609 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
610 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
611 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
613 case GST_STATE_PAUSED_TO_READY:
614 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
615 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
616 gst_osselement_reset (GST_OSSELEMENT (osssink));
617 osssink->handled = 0;
618 osssink->resync = TRUE;
624 if (GST_ELEMENT_CLASS (parent_class)->change_state)
625 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
627 return GST_STATE_SUCCESS;