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)
265 if (GST_OSSELEMENT (osssink)->fd == -1)
268 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETODELAY, &delay) < 0) {
270 if (ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
274 delay = (info.fragstotal * info.fragsize) - info.bytes;
281 gst_osssink_get_time (GstClock *clock, gpointer data)
283 GstOssSink *osssink = GST_OSSSINK (data);
287 if (!GST_OSSELEMENT (osssink)->bps)
290 delay = gst_osssink_get_delay (osssink);
292 /* sometimes delay is bigger than the number of bytes sent to the device,
293 * which screws up this calculation, we assume that everything is still
294 * in the device then */
295 if (((guint64)delay) > osssink->handled) {
296 delay = osssink->handled;
298 res = (osssink->handled - delay) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
304 gst_osssink_get_clock (GstElement *element)
308 osssink = GST_OSSSINK (element);
310 return GST_CLOCK (osssink->provided_clock);
314 gst_osssink_set_clock (GstElement *element, GstClock *clock)
318 osssink = GST_OSSSINK (element);
320 osssink->clock = clock;
324 gst_osssink_chain (GstPad *pad, GstData *_data)
326 GstBuffer *buf = GST_BUFFER (_data);
328 GstClockTime buftime, elementtime, soundtime;
332 /* this has to be an audio buffer */
333 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
335 if (GST_IS_EVENT (buf)) {
336 GstEvent *event = GST_EVENT (buf);
338 switch (GST_EVENT_TYPE (event)) {
340 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_SYNC);
341 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
342 gst_pad_event_default (pad, event);
345 gst_pad_event_default (pad, event);
348 g_assert_not_reached ();
351 if (!GST_OSSELEMENT (osssink)->bps) {
352 gst_buffer_unref (buf);
353 gst_element_error (osssink, CORE, NEGOTIATION, NULL, ("format wasn't negotiated before chain function"));
357 data = GST_BUFFER_DATA (buf);
358 to_write = GST_BUFFER_SIZE (buf);
359 /* sync audio with buffers timestamp */
360 elementtime = gst_element_get_time (GST_ELEMENT (osssink));
361 soundtime = elementtime + gst_osssink_get_delay (osssink) * GST_SECOND / GST_OSSELEMENT (osssink)->bps;
362 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
363 buftime = GST_BUFFER_TIMESTAMP (buf);
367 GST_LOG_OBJECT (osssink, "time: element %"G_GUINT64_FORMAT", real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
368 elementtime, soundtime, buftime);
369 if (MAX (buftime, soundtime) - MIN (buftime, soundtime) > GST_SECOND / 10) {
370 /* we need to adjust to the buffers here */
371 GST_INFO_OBJECT (osssink, "need sync: element %"G_GUINT64_FORMAT", real %"G_GUINT64_FORMAT", buffer: %"G_GUINT64_FORMAT,
372 elementtime, soundtime, buftime);
373 if (soundtime > buftime) {
375 guint throw_away = (soundtime - buftime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND
376 / GST_OSSELEMENT (osssink)->width / GST_OSSELEMENT (osssink)->channels
377 * GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels;
378 if (throw_away >= GST_BUFFER_SIZE (buf)) {
379 gst_data_unref (_data);
382 to_write -= throw_away;
385 guint64 to_handle = (buftime - soundtime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND
386 / GST_OSSELEMENT (osssink)->width / GST_OSSELEMENT (osssink)->channels
387 * GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels;
388 /* FIXME: we really should output silence here */
389 /* round to full frames */
390 g_usleep (to_handle * G_USEC_PER_SEC / GST_OSSELEMENT (osssink)->bps);
391 osssink->handled += (buftime - soundtime) * GST_OSSELEMENT (osssink)->bps / GST_SECOND
392 / GST_OSSELEMENT (osssink)->width / GST_OSSELEMENT (osssink)->channels
393 * GST_OSSELEMENT (osssink)->width * GST_OSSELEMENT (osssink)->channels;
397 if (GST_OSSELEMENT (osssink)->fd >= 0) {
398 if (!osssink->mute) {
400 while (to_write > 0) {
401 gint done = write (GST_OSSELEMENT (osssink)->fd, data,
402 MIN (to_write, osssink->chunk_size));
411 osssink->handled += done;
415 g_warning ("muting osssinks unimplemented wrt clocks!");
419 gst_audio_clock_update_time ((GstAudioClock*)osssink->provided_clock, gst_osssink_get_time (osssink->provided_clock, osssink));
421 gst_buffer_unref (buf);
424 static const GstFormat*
425 gst_osssink_get_formats (GstPad *pad)
427 static const GstFormat formats[] = {
437 gst_osssink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
438 GstFormat *dest_format, gint64 *dest_value)
442 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
444 return gst_osselement_convert (GST_OSSELEMENT (osssink),
445 src_format, src_value,
446 dest_format, dest_value);
449 static const GstQueryType*
450 gst_osssink_get_query_types (GstPad *pad)
452 static const GstQueryType query_types[] = {
461 gst_osssink_sink_query (GstPad *pad, GstQueryType type, GstFormat *format, gint64 *value)
466 osssink = GST_OSSSINK (gst_pad_get_parent (pad));
469 case GST_QUERY_LATENCY:
470 if (!gst_osssink_convert (pad,
471 GST_FORMAT_BYTES, gst_osssink_get_delay (osssink),
477 case GST_QUERY_POSITION:
478 if (!gst_osssink_convert (pad,
479 GST_FORMAT_TIME, gst_element_get_time (GST_ELEMENT (osssink)),
486 res = gst_pad_query (gst_pad_get_peer (osssink->sinkpad), type, format, value);
494 gst_osssink_query (GstElement *element, GstQueryType type, GstFormat *format, gint64 *value)
496 GstOssSink *osssink = GST_OSSSINK (element);
498 return gst_osssink_sink_query (osssink->sinkpad, type, format, value);
502 gst_osssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
506 osssink = GST_OSSSINK (object);
510 osssink->mute = g_value_get_boolean (value);
511 g_object_notify (G_OBJECT (osssink), "mute");
514 GST_OSSELEMENT (osssink)->fragment = g_value_get_int (value);
515 gst_osselement_sync_parms (GST_OSSELEMENT (osssink));
517 case ARG_BUFFER_SIZE:
518 osssink->bufsize = g_value_get_uint (value);
519 g_object_notify (object, "buffer_size");
522 osssink->sync = g_value_get_boolean (value);
523 g_object_notify (G_OBJECT (osssink), "sync");
526 osssink->chunk_size = g_value_get_uint (value);
529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535 gst_osssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
539 osssink = GST_OSSSINK (object);
543 g_value_set_boolean (value, osssink->mute);
546 g_value_set_int (value, GST_OSSELEMENT (osssink)->fragment);
548 case ARG_BUFFER_SIZE:
549 g_value_set_uint (value, osssink->bufsize);
552 g_value_set_boolean (value, osssink->sync);
555 g_value_set_uint (value, osssink->chunk_size);
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563 static GstElementStateReturn
564 gst_osssink_change_state (GstElement *element)
568 osssink = GST_OSSSINK (element);
570 switch (GST_STATE_TRANSITION (element)) {
571 case GST_STATE_READY_TO_PAUSED:
573 case GST_STATE_PAUSED_TO_PLAYING:
574 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), TRUE);
576 case GST_STATE_PLAYING_TO_PAUSED:
577 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
578 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
579 gst_audio_clock_set_active (GST_AUDIO_CLOCK (osssink->provided_clock), FALSE);
581 case GST_STATE_PAUSED_TO_READY:
582 if (GST_FLAG_IS_SET (element, GST_OSSSINK_OPEN))
583 ioctl (GST_OSSELEMENT (osssink)->fd, SNDCTL_DSP_RESET, 0);
584 gst_osselement_reset (GST_OSSELEMENT (osssink));
590 if (GST_ELEMENT_CLASS (parent_class)->change_state)
591 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
593 return GST_STATE_SUCCESS;