2 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
5 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
31 /* elementfactory information */
32 static GstElementDetails esdsink_details = {
35 "Plays audio to an esound server",
36 "Richard Boulton <richard-gst@tartarus.org>",
39 /* Signals and args */
53 GST_PAD_TEMPLATE_FACTORY (sink_factory,
54 "sink", /* the name of the pads */
55 GST_PAD_SINK, /* type of the pad */
56 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
58 "esdsink_sink", /* the name of the caps */
59 "audio/x-raw-int", /* the mime type of the caps */
60 /* Properties follow: */
61 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
62 "signed", GST_PROPS_LIST (
63 GST_PROPS_BOOLEAN (TRUE),
64 GST_PROPS_BOOLEAN (FALSE)
66 "width", GST_PROPS_LIST (
70 "depth", GST_PROPS_LIST (
74 "rate", GST_PROPS_INT_RANGE (8000, 96000),
75 "channels", GST_PROPS_LIST (
82 static void gst_esdsink_base_init (gpointer g_class);
83 static void gst_esdsink_class_init (GstEsdsinkClass *klass);
84 static void gst_esdsink_init (GstEsdsink *esdsink);
86 static gboolean gst_esdsink_open_audio (GstEsdsink *sink);
87 static void gst_esdsink_close_audio (GstEsdsink *sink);
88 static GstElementStateReturn gst_esdsink_change_state (GstElement *element);
89 static GstPadLinkReturn gst_esdsink_sinkconnect (GstPad *pad, GstCaps *caps);
91 static GstClockTime gst_esdsink_get_time (GstClock *clock, gpointer data);
92 static GstClock * gst_esdsink_get_clock (GstElement *element);
93 static void gst_esdsink_set_clock (GstElement *element, GstClock *clock);
94 static void gst_esdsink_chain (GstPad *pad, GstData *_data);
96 static void gst_esdsink_set_property (GObject *object, guint prop_id,
97 const GValue *value, GParamSpec *pspec);
98 static void gst_esdsink_get_property (GObject *object, guint prop_id,
99 GValue *value, GParamSpec *pspec);
101 static GstElementClass *parent_class = NULL;
102 /*static guint gst_esdsink_signals[LAST_SIGNAL] = { 0 }; */
105 gst_esdsink_get_type (void)
107 static GType esdsink_type = 0;
110 static const GTypeInfo esdsink_info = {
111 sizeof(GstEsdsinkClass),
112 gst_esdsink_base_init,
114 (GClassInitFunc)gst_esdsink_class_init,
119 (GInstanceInitFunc)gst_esdsink_init,
121 esdsink_type = g_type_register_static(GST_TYPE_ELEMENT, "GstEsdsink", &esdsink_info, 0);
127 gst_esdsink_base_init (gpointer g_class)
129 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
131 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sink_factory));
132 gst_element_class_set_details (element_class, &esdsink_details);
136 gst_esdsink_class_init (GstEsdsinkClass *klass)
138 GObjectClass *gobject_class;
139 GstElementClass *gstelement_class;
141 gobject_class = (GObjectClass*)klass;
142 gstelement_class = (GstElementClass*)klass;
144 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
146 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
147 g_param_spec_boolean("mute","mute","mute",
148 TRUE,G_PARAM_READWRITE)); /* CHECKME */
149 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HOST,
150 g_param_spec_string("host","host","host",
151 NULL, G_PARAM_READWRITE)); /* CHECKME */
152 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SYNC,
153 g_param_spec_boolean("sync","sync","Synchronize output to clock",
154 FALSE,G_PARAM_READWRITE));
155 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FALLBACK,
156 g_param_spec_boolean("fallback","fallback","Fall back to using OSS if Esound daemon is not present",
157 FALSE,G_PARAM_READWRITE));
159 gobject_class->set_property = gst_esdsink_set_property;
160 gobject_class->get_property = gst_esdsink_get_property;
162 gstelement_class->change_state = gst_esdsink_change_state;
163 gstelement_class->set_clock = gst_esdsink_set_clock;
164 gstelement_class->get_clock = gst_esdsink_get_clock;
168 gst_esdsink_init(GstEsdsink *esdsink)
170 esdsink->sinkpad = gst_pad_new_from_template (
171 GST_PAD_TEMPLATE_GET (sink_factory), "sink");
172 gst_element_add_pad(GST_ELEMENT(esdsink), esdsink->sinkpad);
173 gst_pad_set_chain_function(esdsink->sinkpad, GST_DEBUG_FUNCPTR(gst_esdsink_chain));
174 gst_pad_set_link_function(esdsink->sinkpad, gst_esdsink_sinkconnect);
176 GST_FLAG_SET (esdsink, GST_ELEMENT_EVENT_AWARE);
178 esdsink->mute = FALSE;
180 /* FIXME: get default from somewhere better than just putting them inline. */
181 esdsink->negotiated = FALSE;
182 esdsink->format = -1;
184 esdsink->channels = -1;
185 esdsink->frequency = -1;
186 esdsink->host = getenv ("ESPEAKER");
187 esdsink->provided_clock = gst_audio_clock_new("esdclock", gst_esdsink_get_time, esdsink);
188 gst_object_set_parent(GST_OBJECT(esdsink->provided_clock), GST_OBJECT(esdsink));
189 esdsink->sync = FALSE;
190 esdsink->fallback = FALSE;
193 static GstPadLinkReturn
194 gst_esdsink_sinkconnect (GstPad *pad, GstCaps *caps)
199 esdsink = GST_ESDSINK (gst_pad_get_parent (pad));
201 if (!GST_CAPS_IS_FIXED (caps))
202 return GST_PAD_LINK_DELAYED;
204 gst_caps_get_int (caps, "depth", &esdsink->depth);
205 gst_caps_get_int (caps, "signed", &sign);
206 gst_caps_get_int (caps, "channels", &esdsink->channels);
207 gst_caps_get_int (caps, "rate", &esdsink->frequency);
209 esdsink->bytes_per_sample = esdsink->channels * (esdsink->depth/8);
212 if ((sign == FALSE && esdsink->depth != 8) ||
213 (sign == TRUE && esdsink->depth != 16)) {
214 return GST_PAD_LINK_REFUSED;
217 gst_esdsink_close_audio (esdsink);
218 if (gst_esdsink_open_audio (esdsink)) {
219 esdsink->negotiated = TRUE;
220 return GST_PAD_LINK_OK;
223 return GST_PAD_LINK_REFUSED;
227 gst_esdsink_get_latency (GstEsdsink *esdsink)
229 /* esd_get_latency() doesn't actually work. So we return a
233 return esd_get_latency (esdsink->fd);
238 gst_esdsink_get_time (GstClock *clock, gpointer data)
240 GstEsdsink *esdsink = GST_ESDSINK(data);
243 res = (esdsink->handled - gst_esdsink_get_latency(esdsink))
244 * GST_SECOND / esdsink->frequency;
250 gst_esdsink_get_clock (GstElement *element)
254 esdsink = GST_ESDSINK (element);
256 return GST_CLOCK(esdsink->provided_clock);
260 gst_esdsink_set_clock (GstElement *element, GstClock *clock)
264 esdsink = GST_ESDSINK (element);
266 esdsink->clock = clock;
270 gst_esdsink_chain (GstPad *pad, GstData *_data)
272 GstBuffer *buf = GST_BUFFER (_data);
275 esdsink = GST_ESDSINK (gst_pad_get_parent (pad));
277 if (!esdsink->negotiated) {
278 gst_element_error (GST_ELEMENT (esdsink), "not negotiated");
282 if (GST_IS_EVENT(buf)){
283 GstEvent *event = GST_EVENT(buf);
285 switch(GST_EVENT_TYPE(event)){
287 gst_audio_clock_set_active (GST_AUDIO_CLOCK (esdsink->provided_clock),
289 gst_pad_event_default (pad, event);
291 case GST_EVENT_DISCONTINUOUS:
295 if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
296 if (!gst_clock_handle_discont (esdsink->clock, value)){
297 gst_audio_clock_set_active (GST_AUDIO_CLOCK (esdsink->provided_clock),
300 esdsink->handled = 0;
302 esdsink->resync = TRUE;
306 gst_pad_event_default(pad, event);
309 gst_event_unref(event);
313 if (GST_BUFFER_DATA (buf) != NULL) {
314 if (!esdsink->mute && esdsink->fd >= 0) {
315 guchar *data = GST_BUFFER_DATA (buf);
316 gint size = GST_BUFFER_SIZE (buf);
322 GstClockTimeDiff jitter;
324 delay = gst_esdsink_get_latency (esdsink);
325 queued = delay * GST_SECOND / esdsink->frequency;
327 if (esdsink->resync && esdsink->sync) {
328 GstClockID id = gst_clock_new_single_shot_id (esdsink->clock,
329 GST_BUFFER_TIMESTAMP (buf) - queued);
331 gst_element_clock_wait (GST_ELEMENT (esdsink), id, &jitter);
332 gst_clock_id_free (id);
335 gst_clock_handle_discont (esdsink->clock,
336 GST_BUFFER_TIMESTAMP (buf) - queued + jitter);
338 gst_audio_clock_set_active ((GstAudioClock *)esdsink->provided_clock, TRUE);
339 esdsink->resync = FALSE;
346 GST_DEBUG ("esdsink: fd=%d data=%p size=%d",
347 esdsink->fd, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
348 while (to_write > 0){
351 done = write (esdsink->fd, data, to_write);
357 g_assert_not_reached();
362 esdsink->handled += done / esdsink->bytes_per_sample;
368 gst_audio_clock_update_time ((GstAudioClock *)esdsink->provided_clock,
369 GST_BUFFER_TIMESTAMP (buf));
372 gst_buffer_unref (buf);
376 gst_esdsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
380 /* it's not null if we got it, but it might not be ours */
381 g_return_if_fail(GST_IS_ESDSINK(object));
382 esdsink = GST_ESDSINK(object);
386 esdsink->mute = g_value_get_boolean (value);
389 g_free(esdsink->host);
390 if (g_value_get_string (value) == NULL)
391 esdsink->host = NULL;
393 esdsink->host = g_strdup (g_value_get_string (value));
396 esdsink->sync = g_value_get_boolean (value);
399 esdsink->fallback = g_value_get_boolean (value);
407 gst_esdsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
411 esdsink = GST_ESDSINK(object);
415 g_value_set_boolean (value, esdsink->mute);
418 g_value_set_string (value, esdsink->host);
421 g_value_set_boolean (value, esdsink->sync);
424 g_value_set_boolean (value, esdsink->fallback);
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
433 gst_esdsink_factory_init (GstPlugin *plugin)
435 if (!gst_element_register (plugin, "esdsink", GST_RANK_NONE, GST_TYPE_ESDSINK))
442 gst_esdsink_open_audio (GstEsdsink *sink)
444 /* Name used by esound for this connection. */
445 const char * connname = "GStreamer";
447 /* Bitmap describing audio format. */
448 esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
450 g_return_val_if_fail (sink->fd == -1, FALSE);
452 if (sink->depth == 16) esdformat |= ESD_BITS16;
453 else if (sink->depth == 8) esdformat |= ESD_BITS8;
455 GST_DEBUG ("esdsink: invalid bit depth (%d)", sink->depth);
459 if (sink->channels == 2) esdformat |= ESD_STEREO;
460 else if (sink->channels == 1) esdformat |= ESD_MONO;
462 GST_DEBUG ("esdsink: invalid number of channels (%d)", sink->channels);
466 GST_DEBUG ("esdsink: attempting to open connection to esound server");
468 sink->fd = esd_play_stream_fallback(esdformat, sink->frequency, sink->host, connname);
470 sink->fd = esd_play_stream(esdformat, sink->frequency, sink->host, connname);
472 if ( sink->fd < 0 ) {
473 GST_DEBUG ("esdsink: can't open connection to esound server");
481 gst_esdsink_close_audio (GstEsdsink *sink)
489 GST_DEBUG ("esdsink: closed sound device");
492 static GstElementStateReturn
493 gst_esdsink_change_state (GstElement *element)
497 esdsink = GST_ESDSINK (element);
499 switch (GST_STATE_TRANSITION (element)) {
500 case GST_STATE_NULL_TO_READY:
502 case GST_STATE_READY_TO_PAUSED:
504 case GST_STATE_PAUSED_TO_PLAYING:
506 case GST_STATE_PLAYING_TO_PAUSED:
507 gst_audio_clock_set_active (GST_AUDIO_CLOCK (esdsink->provided_clock),
509 esdsink->resync = TRUE;
511 case GST_STATE_PAUSED_TO_READY:
512 gst_esdsink_close_audio (GST_ESDSINK (element));
513 esdsink->negotiated = FALSE;
515 case GST_STATE_READY_TO_NULL:
521 if (GST_ELEMENT_CLASS (parent_class)->change_state)
522 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
524 return GST_STATE_SUCCESS;