2003-11-16 00:40:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 CodeFactory AB
|
|
|
|
* Copyright (C) 2001 Thomas Nyberg <thomas@codefactory.se>
|
|
|
|
* Copyright (C) 2001-2002 Andy Wingo <apwingo@eos.ncsu.edu>
|
|
|
|
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstalsasink.h"
|
|
|
|
#include "gstalsaclock.h"
|
|
|
|
|
|
|
|
/* elementfactory information */
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstElementDetails gst_alsa_sink_details =
|
|
|
|
GST_ELEMENT_DETAILS ("Alsa Sink",
|
|
|
|
"Sink/Audio",
|
|
|
|
"Output to a sound card via ALSA",
|
|
|
|
"Thomas Nyberg <thomas@codefactory.se>, "
|
|
|
|
"Andy Wingo <apwingo@eos.ncsu.edu>, "
|
|
|
|
"Benjamin Otte <in7y118@public.uni-hamburg.de>");
|
|
|
|
|
|
|
|
static GstPadTemplate *gst_alsa_sink_pad_factory (void);
|
|
|
|
static GstPadTemplate *gst_alsa_sink_request_pad_factory (void);
|
|
|
|
static void gst_alsa_sink_base_init (gpointer g_class);
|
|
|
|
static void gst_alsa_sink_class_init (gpointer g_klass, gpointer class_data);
|
|
|
|
static void gst_alsa_sink_init (GstAlsaSink * this);
|
|
|
|
static inline void gst_alsa_sink_flush_one_pad (GstAlsaSink * sink, gint i);
|
|
|
|
static void gst_alsa_sink_flush_pads (GstAlsaSink * sink);
|
|
|
|
static int gst_alsa_sink_mmap (GstAlsa * this, snd_pcm_sframes_t * avail);
|
|
|
|
static int gst_alsa_sink_write (GstAlsa * this, snd_pcm_sframes_t * avail);
|
|
|
|
static void gst_alsa_sink_loop (GstElement * element);
|
|
|
|
static gboolean gst_alsa_sink_check_event (GstAlsaSink * sink, gint pad_nr);
|
|
|
|
static GstElementStateReturn gst_alsa_sink_change_state (GstElement * element);
|
|
|
|
|
|
|
|
static GstClockTime gst_alsa_sink_get_time (GstAlsa * this);
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
static GstAlsa *sink_parent_class = NULL;
|
|
|
|
|
|
|
|
static GstPadTemplate *
|
|
|
|
gst_alsa_sink_pad_factory (void)
|
|
|
|
{
|
|
|
|
static GstPadTemplate *template = NULL;
|
|
|
|
|
|
|
|
if (!template)
|
|
|
|
template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_alsa_caps (SND_PCM_FORMAT_UNKNOWN, -1, -1));
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
static GstPadTemplate *
|
|
|
|
gst_alsa_sink_request_pad_factory (void)
|
|
|
|
{
|
|
|
|
static GstPadTemplate *template = NULL;
|
|
|
|
|
|
|
|
if (!template)
|
|
|
|
template =
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_pad_template_new ("sink%d", GST_PAD_SINK, GST_PAD_REQUEST,
|
|
|
|
gst_alsa_caps (SND_PCM_FORMAT_UNKNOWN, -1, 1));
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-11-16 00:40:01 +00:00
|
|
|
GType
|
|
|
|
gst_alsa_sink_get_type (void)
|
|
|
|
{
|
|
|
|
static GType alsa_sink_type = 0;
|
|
|
|
|
|
|
|
if (!alsa_sink_type) {
|
|
|
|
static const GTypeInfo alsa_sink_info = {
|
|
|
|
sizeof (GstAlsaSinkClass),
|
|
|
|
gst_alsa_sink_base_init,
|
|
|
|
NULL,
|
|
|
|
gst_alsa_sink_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstAlsaSink),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_alsa_sink_init,
|
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
alsa_sink_type =
|
2004-05-07 04:56:56 +00:00
|
|
|
g_type_register_static (GST_TYPE_ALSA_MIXER, "GstAlsaSink",
|
|
|
|
&alsa_sink_info, 0);
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
return alsa_sink_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_alsa_sink_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_alsa_sink_pad_factory ());
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_alsa_sink_request_pad_factory ());
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &gst_alsa_sink_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_alsa_sink_class_init (gpointer g_class, gpointer class_data)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
GstAlsaClass *alsa_class;
|
|
|
|
GstAlsaSinkClass *klass;
|
|
|
|
|
|
|
|
klass = (GstAlsaSinkClass *) g_class;
|
|
|
|
object_class = (GObjectClass *) klass;
|
|
|
|
element_class = (GstElementClass *) klass;
|
|
|
|
alsa_class = (GstAlsaClass *) klass;
|
|
|
|
|
|
|
|
if (sink_parent_class == NULL)
|
2004-05-07 04:56:56 +00:00
|
|
|
sink_parent_class = g_type_class_ref (GST_TYPE_ALSA_MIXER);
|
2003-11-16 00:40:01 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
alsa_class->stream = SND_PCM_STREAM_PLAYBACK;
|
|
|
|
alsa_class->transmit_mmap = gst_alsa_sink_mmap;
|
|
|
|
alsa_class->transmit_rw = gst_alsa_sink_write;
|
|
|
|
|
|
|
|
element_class->change_state = gst_alsa_sink_change_state;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_init (GstAlsaSink * sink)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
GstAlsa *this = GST_ALSA (sink);
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
this->pad[0] =
|
|
|
|
gst_pad_new_from_template (gst_alsa_sink_pad_factory (), "sink");
|
2003-11-16 00:40:01 +00:00
|
|
|
gst_pad_set_link_function (this->pad[0], gst_alsa_link);
|
|
|
|
gst_pad_set_getcaps_function (this->pad[0], gst_alsa_get_caps);
|
2004-03-09 21:29:56 +00:00
|
|
|
gst_pad_set_fixate_function (this->pad[0], gst_alsa_fixate);
|
2003-11-16 00:40:01 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (this), this->pad[0]);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
this->clock =
|
|
|
|
gst_alsa_clock_new ("alsasinkclock", gst_alsa_sink_get_time, this);
|
2003-11-16 00:40:01 +00:00
|
|
|
/* we hold a ref to our clock until we're disposed */
|
|
|
|
gst_object_ref (GST_OBJECT (this->clock));
|
|
|
|
gst_object_sink (GST_OBJECT (this->clock));
|
|
|
|
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (this), gst_alsa_sink_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_flush_one_pad (GstAlsaSink * sink, gint i)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
2004-07-08 13:40:37 +00:00
|
|
|
GST_DEBUG_OBJECT (sink, "flushing pad %d", i);
|
2003-11-16 00:40:01 +00:00
|
|
|
switch (sink->behaviour[i]) {
|
2004-03-14 22:34:34 +00:00
|
|
|
case 0:
|
2004-07-08 13:40:37 +00:00
|
|
|
if (sink->gst_data[i]) {
|
|
|
|
GST_DEBUG_OBJECT (sink, "unreffing gst data %p", sink->gst_data[i]);
|
|
|
|
gst_data_unref (GST_DATA (sink->gst_data[i]));
|
|
|
|
}
|
|
|
|
sink->gst_data[i] = NULL;
|
|
|
|
sink->buf_data[i] = NULL;
|
2004-03-14 22:34:34 +00:00
|
|
|
sink->behaviour[i] = 0;
|
|
|
|
sink->size[i] = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
2004-07-08 13:40:37 +00:00
|
|
|
g_free (sink->buf_data[i]);
|
|
|
|
sink->buf_data[i] = NULL;
|
2004-03-14 22:34:34 +00:00
|
|
|
sink->behaviour[i] = 0;
|
|
|
|
sink->size[i] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_flush_pads (GstAlsaSink * sink)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < GST_ELEMENT (sink)->numpads; i++) {
|
|
|
|
/* flush twice to unref buffer when behaviour == 1 */
|
|
|
|
gst_alsa_sink_flush_one_pad (sink, i);
|
|
|
|
gst_alsa_sink_flush_one_pad (sink, i);
|
|
|
|
}
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-11-16 00:40:01 +00:00
|
|
|
/* TRUE, if everything should continue */
|
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_check_event (GstAlsaSink * sink, gint pad_nr)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
gboolean cont = TRUE;
|
2004-07-08 13:40:37 +00:00
|
|
|
GstEvent *event;
|
2003-11-16 00:40:01 +00:00
|
|
|
GstAlsa *this = GST_ALSA (sink);
|
|
|
|
|
2004-07-08 13:40:37 +00:00
|
|
|
/* we get the event from our internal buffer and clear the internal one */
|
|
|
|
event = GST_EVENT (sink->gst_data[pad_nr]);
|
|
|
|
sink->gst_data[pad_nr] = 0;
|
|
|
|
GST_LOG_OBJECT (sink, "checking event %p of type %d on sink pad %d",
|
|
|
|
event, GST_EVENT_TYPE (event), pad_nr);
|
2003-11-16 00:40:01 +00:00
|
|
|
if (event) {
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_alsa_set_eos (this);
|
|
|
|
cont = FALSE;
|
|
|
|
break;
|
2003-11-16 00:40:01 +00:00
|
|
|
case GST_EVENT_INTERRUPT:
|
2004-03-15 19:32:28 +00:00
|
|
|
cont = FALSE;
|
|
|
|
break;
|
2004-03-14 22:34:34 +00:00
|
|
|
case GST_EVENT_DISCONTINUOUS:
|
|
|
|
{
|
2004-06-07 01:41:37 +00:00
|
|
|
GstClockTime value, delay;
|
2004-03-15 19:32:28 +00:00
|
|
|
|
2004-05-09 00:02:26 +00:00
|
|
|
/* only the first pad may seek */
|
2004-03-15 19:32:28 +00:00
|
|
|
if (pad_nr != 0) {
|
|
|
|
break;
|
|
|
|
}
|
2004-06-07 20:44:55 +00:00
|
|
|
delay = (this->format == NULL) ? 0 :
|
2004-06-23 18:08:26 +00:00
|
|
|
GST_SECOND * this->played / this->format->rate -
|
2004-06-07 01:41:37 +00:00
|
|
|
gst_alsa_sink_get_time (this);
|
2004-03-15 19:32:28 +00:00
|
|
|
if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
|
2004-07-03 12:48:30 +00:00
|
|
|
gst_element_set_time_delay (GST_ELEMENT (this), value,
|
|
|
|
MIN (value, delay));
|
2004-06-23 16:57:16 +00:00
|
|
|
} else if (this->format
|
|
|
|
&& (gst_event_discont_get_value (event, GST_FORMAT_DEFAULT, &value)
|
|
|
|
|| gst_event_discont_get_value (event, GST_FORMAT_BYTES,
|
2004-05-09 00:02:26 +00:00
|
|
|
&value))) {
|
2004-03-15 19:32:28 +00:00
|
|
|
value = gst_alsa_samples_to_timestamp (this, value);
|
2004-07-03 12:48:30 +00:00
|
|
|
gst_element_set_time_delay (GST_ELEMENT (this), value, MIN (value,
|
|
|
|
delay));
|
2004-03-15 19:32:28 +00:00
|
|
|
} else {
|
2004-05-09 00:02:26 +00:00
|
|
|
GST_WARNING_OBJECT (this,
|
2004-03-15 19:32:28 +00:00
|
|
|
"couldn't extract time from discont event. Bad things might happen!");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2004-03-14 22:34:34 +00:00
|
|
|
}
|
2003-11-16 00:40:01 +00:00
|
|
|
default:
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_INFO_OBJECT (this, "got an unknown event (Type: %d)",
|
|
|
|
GST_EVENT_TYPE (event));
|
|
|
|
break;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
2004-07-08 13:40:37 +00:00
|
|
|
GST_LOG_OBJECT (sink, "unreffing event %p of type %d with refcount %d",
|
|
|
|
event, GST_EVENT_TYPE (event), GST_DATA_REFCOUNT (event));
|
2003-11-16 00:40:01 +00:00
|
|
|
gst_event_unref (event);
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->gst_data[pad_nr] = NULL;
|
2003-11-16 00:40:01 +00:00
|
|
|
} else {
|
|
|
|
/* the element at the top of the chain did not emit an event. */
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
static int
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_mmap (GstAlsa * this, snd_pcm_sframes_t * avail)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
snd_pcm_uframes_t offset;
|
|
|
|
const snd_pcm_channel_area_t *dst;
|
|
|
|
snd_pcm_channel_area_t *src;
|
|
|
|
GstAlsaSink *sink = GST_ALSA_SINK (this);
|
2004-04-30 20:57:48 +00:00
|
|
|
int i;
|
|
|
|
int err = -1;
|
|
|
|
int width = snd_pcm_format_physical_width (this->format->format);
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
/* areas points to the memory areas that belong to gstreamer. */
|
2004-03-14 22:34:34 +00:00
|
|
|
src = g_malloc0 (this->format->channels * sizeof (snd_pcm_channel_area_t));
|
2003-11-16 00:40:01 +00:00
|
|
|
|
|
|
|
if (((GstElement *) this)->numpads == 1) {
|
|
|
|
/* interleaved */
|
|
|
|
for (i = 0; i < this->format->channels; i++) {
|
2004-07-08 13:40:37 +00:00
|
|
|
src[i].addr = sink->buf_data[0];
|
2003-11-16 00:40:01 +00:00
|
|
|
src[i].first = i * width;
|
|
|
|
src[i].step = this->format->channels * width;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* noninterleaved */
|
|
|
|
for (i = 0; i < this->format->channels; i++) {
|
2004-07-08 13:40:37 +00:00
|
|
|
src[i].addr = sink->buf_data[i];
|
2003-11-16 00:40:01 +00:00
|
|
|
src[i].first = 0;
|
|
|
|
src[i].step = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_mmap_begin (this->handle, &dst, &offset, avail)) < 0) {
|
|
|
|
GST_ERROR_OBJECT (this, "mmap failed: %s", snd_strerror (err));
|
2004-04-30 20:57:48 +00:00
|
|
|
goto out;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if ((err =
|
2004-03-15 19:32:28 +00:00
|
|
|
snd_pcm_areas_copy (dst, offset, src, 0, this->format->channels,
|
|
|
|
*avail, this->format->format)) < 0) {
|
2003-11-16 00:40:01 +00:00
|
|
|
snd_pcm_mmap_commit (this->handle, offset, 0);
|
|
|
|
GST_ERROR_OBJECT (this, "data copy failed: %s", snd_strerror (err));
|
2004-04-30 20:57:48 +00:00
|
|
|
goto out;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
if ((err = snd_pcm_mmap_commit (this->handle, offset, *avail)) < 0) {
|
|
|
|
GST_ERROR_OBJECT (this, "mmap commit failed: %s", snd_strerror (err));
|
2004-04-30 20:57:48 +00:00
|
|
|
goto out;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
2004-05-07 04:56:56 +00:00
|
|
|
out:
|
2004-04-30 20:57:48 +00:00
|
|
|
g_free (src);
|
2003-11-16 00:40:01 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
static int
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_write (GstAlsa * this, snd_pcm_sframes_t * avail)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
GstAlsaSink *sink = GST_ALSA_SINK (this);
|
|
|
|
void *channels[this->format->channels];
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
if (((GstElement *) this)->numpads == 1) {
|
|
|
|
/* interleaved */
|
2004-07-08 13:40:37 +00:00
|
|
|
err = snd_pcm_writei (this->handle, sink->buf_data[0], *avail);
|
2003-11-16 00:40:01 +00:00
|
|
|
} else {
|
|
|
|
/* noninterleaved */
|
|
|
|
for (i = 0; i < this->format->channels; i++) {
|
2004-07-08 13:40:37 +00:00
|
|
|
channels[i] = sink->buf_data[i];
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
err = snd_pcm_writen (this->handle, channels, *avail);
|
|
|
|
}
|
|
|
|
/* error handling */
|
|
|
|
if (err < 0) {
|
|
|
|
if (err == -EPIPE) {
|
|
|
|
gst_alsa_xrun_recovery (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
GST_ERROR_OBJECT (this, "error on data access: %s", snd_strerror (err));
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_loop (GstElement * element)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
snd_pcm_sframes_t avail, avail2, copied, sample_diff, max_discont;
|
2004-05-16 02:51:29 +00:00
|
|
|
snd_pcm_uframes_t samplestamp, expected;
|
2003-11-16 00:40:01 +00:00
|
|
|
gint i;
|
2004-03-15 19:32:28 +00:00
|
|
|
guint bytes; /* per channel */
|
2003-11-16 00:40:01 +00:00
|
|
|
GstAlsa *this = GST_ALSA (element);
|
|
|
|
GstAlsaSink *sink = GST_ALSA_SINK (element);
|
|
|
|
|
|
|
|
g_return_if_fail (sink != NULL);
|
|
|
|
|
|
|
|
sink_restart:
|
|
|
|
|
|
|
|
avail = gst_alsa_update_avail (this);
|
2004-03-14 22:34:34 +00:00
|
|
|
if (avail == -EPIPE)
|
|
|
|
goto sink_restart;
|
|
|
|
if (avail < 0)
|
|
|
|
return;
|
2003-11-16 00:40:01 +00:00
|
|
|
if (avail > 0) {
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
/* Not enough space. We grab data nonetheless and sleep afterwards */
|
2003-11-16 00:40:01 +00:00
|
|
|
if (avail < this->period_size) {
|
|
|
|
avail = this->period_size;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-11-16 00:40:01 +00:00
|
|
|
/* check how many bytes we still have in all our bytestreams */
|
2004-07-08 13:40:37 +00:00
|
|
|
/* initialize this value to a somewhat sane state, we might alloc
|
|
|
|
* this much data below (which would be a bug, but who knows)... */
|
2004-03-15 19:32:28 +00:00
|
|
|
bytes = this->period_size * this->period_count * element->numpads * 8; /* must be > max sample size in bytes */
|
2003-11-16 00:40:01 +00:00
|
|
|
for (i = 0; i < element->numpads; i++) {
|
2004-07-08 13:40:37 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
|
2003-11-16 00:40:01 +00:00
|
|
|
g_assert (this->pad[i] != NULL);
|
|
|
|
while (sink->size[i] == 0) {
|
2004-07-08 13:40:37 +00:00
|
|
|
if (!sink->gst_data[i]) {
|
|
|
|
sink->gst_data[i] = gst_pad_pull (this->pad[i]);
|
|
|
|
GST_LOG_OBJECT (sink, "pulled data %p from pad %d",
|
|
|
|
sink->gst_data[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_IS_EVENT (sink->gst_data[i])) {
|
|
|
|
GST_LOG_OBJECT (sink, "pulled data %p is an event, checking",
|
|
|
|
sink->gst_data[i]);
|
2004-03-15 19:32:28 +00:00
|
|
|
if (gst_alsa_sink_check_event (sink, i))
|
|
|
|
continue;
|
|
|
|
return;
|
|
|
|
}
|
2004-07-08 13:40:37 +00:00
|
|
|
/* it's a buffer */
|
|
|
|
g_return_if_fail (GST_IS_BUFFER (sink->gst_data[i]));
|
|
|
|
buf = GST_BUFFER (sink->gst_data[i]);
|
|
|
|
/* check if caps nego failed somewhere */
|
2004-03-15 19:32:28 +00:00
|
|
|
if (this->format == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (this, CORE, NEGOTIATION, (NULL),
|
|
|
|
("ALSA format not negotiated"));
|
|
|
|
}
|
2004-07-08 13:40:37 +00:00
|
|
|
samplestamp = gst_alsa_timestamp_to_samples (this,
|
|
|
|
GST_BUFFER_TIMESTAMP (buf));
|
2004-03-15 19:32:28 +00:00
|
|
|
max_discont = gst_alsa_timestamp_to_samples (this, this->max_discont);
|
2004-05-16 02:51:29 +00:00
|
|
|
/* optimization: check if we're using our own clock
|
2004-07-08 13:40:37 +00:00
|
|
|
* This optimization is important because if we're using our own clock
|
|
|
|
* gst_element_get_time calls snd_pcm_delay and the following code
|
|
|
|
* assumes that both calls return the same value. However they can be
|
|
|
|
* wildly different, since snd_pcm_delay goes deep into the kernel.
|
2004-05-16 02:51:29 +00:00
|
|
|
*/
|
2004-05-16 16:06:42 +00:00
|
|
|
if (gst_element_get_clock (element) == GST_CLOCK (this->clock)) {
|
2004-07-08 13:40:37 +00:00
|
|
|
/* FIXME: this is ugly because of the variables it uses but I
|
|
|
|
* don't know a better way to get this info */
|
2004-05-16 16:06:42 +00:00
|
|
|
if (element->base_time > this->clock->start_time) {
|
|
|
|
expected =
|
2004-06-23 18:08:26 +00:00
|
|
|
this->played - gst_alsa_timestamp_to_samples (this,
|
2004-05-16 16:06:42 +00:00
|
|
|
element->base_time - this->clock->start_time);
|
|
|
|
} else {
|
|
|
|
expected =
|
2004-06-23 18:08:26 +00:00
|
|
|
this->played + gst_alsa_timestamp_to_samples (this,
|
2004-05-16 16:06:42 +00:00
|
|
|
this->clock->start_time - element->base_time);
|
|
|
|
}
|
2004-05-16 02:51:29 +00:00
|
|
|
} else {
|
2004-05-16 16:06:42 +00:00
|
|
|
if (snd_pcm_delay (this->handle, &sample_diff) != 0) {
|
|
|
|
sample_diff = 0;
|
|
|
|
}
|
2004-05-16 02:51:29 +00:00
|
|
|
expected =
|
|
|
|
gst_alsa_timestamp_to_samples (this,
|
|
|
|
gst_element_get_time (GST_ELEMENT (this))) + sample_diff;
|
|
|
|
/* actual diff = buffer samplestamp - played - to_play */
|
|
|
|
}
|
|
|
|
sample_diff = samplestamp - expected;
|
2004-03-15 19:32:28 +00:00
|
|
|
|
2004-07-08 13:40:37 +00:00
|
|
|
if ((!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) ||
|
2004-03-15 19:32:28 +00:00
|
|
|
(-max_discont <= sample_diff && sample_diff <= max_discont)) {
|
|
|
|
|
|
|
|
/* difference between expected and current is < GST_ALSA_DEVIATION */
|
|
|
|
no_difference:
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->size[i] = GST_BUFFER_SIZE (buf);
|
|
|
|
sink->buf_data[i] = GST_BUFFER_DATA (buf);
|
2004-03-15 19:32:28 +00:00
|
|
|
sink->behaviour[i] = 0;
|
|
|
|
} else if (sample_diff > 0) {
|
|
|
|
/* there are empty samples in front of us, fill them with silence */
|
|
|
|
int samples = MIN (bytes, sample_diff) *
|
|
|
|
(element->numpads == 1 ? this->format->channels : 1);
|
|
|
|
int size =
|
|
|
|
samples * snd_pcm_format_physical_width (this->format->format) /
|
|
|
|
8;
|
|
|
|
GST_INFO_OBJECT (this,
|
2004-05-16 02:51:29 +00:00
|
|
|
"Allocating %d bytes (%ld samples) now to resync: sample %lu expected, but got %ld",
|
|
|
|
size, MIN (bytes, sample_diff), expected, samplestamp);
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->buf_data[i] = g_try_malloc (size);
|
|
|
|
if (!sink->buf_data[i]) {
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_WARNING_OBJECT (this,
|
|
|
|
"error allocating %d bytes, buffers unsynced now.", size);
|
|
|
|
goto no_difference;
|
|
|
|
}
|
|
|
|
sink->size[i] = size;
|
|
|
|
if (0 != snd_pcm_format_set_silence (this->format->format,
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->buf_data[i], samples)) {
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_WARNING_OBJECT (this,
|
|
|
|
"error silencing buffer, enjoy the noise.");
|
|
|
|
}
|
|
|
|
sink->behaviour[i] = 1;
|
2004-07-08 13:40:37 +00:00
|
|
|
} else if (gst_alsa_samples_to_bytes (this, -sample_diff) >=
|
|
|
|
GST_BUFFER_SIZE (buf)) {
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_INFO_OBJECT (this,
|
2004-05-16 02:51:29 +00:00
|
|
|
"Skipping %lu samples to resync (complete buffer): sample %lu expected, but got %ld",
|
2004-07-08 13:40:37 +00:00
|
|
|
gst_alsa_bytes_to_samples (this, GST_BUFFER_SIZE (buf)), expected,
|
2004-03-15 19:32:28 +00:00
|
|
|
samplestamp);
|
|
|
|
/* this buffer is way behind */
|
2004-07-08 13:40:37 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
sink->gst_data[i] = NULL;
|
2004-03-15 19:32:28 +00:00
|
|
|
continue;
|
|
|
|
} else if (sample_diff < 0) {
|
2004-05-16 23:30:00 +00:00
|
|
|
gint difference = gst_alsa_samples_to_bytes (this, -sample_diff);
|
2004-03-15 19:32:28 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (this,
|
2004-05-16 02:51:29 +00:00
|
|
|
"Skipping %lu samples to resync: sample %lu expected, but got %ld",
|
|
|
|
(gulong) - sample_diff, expected, samplestamp);
|
2004-03-15 19:32:28 +00:00
|
|
|
/* this buffer is only a bit behind */
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->size[i] = GST_BUFFER_SIZE (buf) - difference;
|
|
|
|
sink->buf_data[i] = GST_BUFFER_DATA (buf) + difference;
|
2004-03-15 19:32:28 +00:00
|
|
|
sink->behaviour[i] = 0;
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
bytes = MIN (bytes, sink->size[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
avail = MIN (avail, gst_alsa_bytes_to_samples (this, bytes));
|
|
|
|
|
|
|
|
/* wait until the hw buffer has enough space */
|
2004-03-14 22:34:34 +00:00
|
|
|
while (gst_element_get_state (element) == GST_STATE_PLAYING
|
2004-03-15 19:32:28 +00:00
|
|
|
&& (avail2 = gst_alsa_update_avail (this)) < avail) {
|
2004-03-14 22:34:34 +00:00
|
|
|
if (avail2 <= -EPIPE)
|
2004-03-15 19:32:28 +00:00
|
|
|
goto sink_restart;
|
2004-03-14 22:34:34 +00:00
|
|
|
if (avail2 < 0)
|
2004-03-15 19:32:28 +00:00
|
|
|
return;
|
2004-03-14 22:34:34 +00:00
|
|
|
if (avail2 < avail
|
2004-03-15 19:32:28 +00:00
|
|
|
&& snd_pcm_state (this->handle) != SND_PCM_STATE_RUNNING)
|
|
|
|
if (!gst_alsa_start (this))
|
|
|
|
return;
|
2003-11-16 00:40:01 +00:00
|
|
|
if (gst_alsa_pcm_wait (this) == FALSE)
|
2004-03-15 19:32:28 +00:00
|
|
|
return;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-08 13:40:37 +00:00
|
|
|
/* FIXME: lotsa stuff can have happened while fetching data.
|
|
|
|
* Do we need to check something? */
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-11-16 00:40:01 +00:00
|
|
|
/* put this data into alsa */
|
|
|
|
if ((copied = this->transmit (this, &avail)) < 0)
|
|
|
|
return;
|
|
|
|
/* update our clock */
|
2004-06-23 18:08:26 +00:00
|
|
|
this->played += copied;
|
2003-11-16 00:40:01 +00:00
|
|
|
/* flush the data */
|
|
|
|
bytes = gst_alsa_samples_to_bytes (this, copied);
|
|
|
|
for (i = 0; i < element->numpads; i++) {
|
|
|
|
if ((sink->size[i] -= bytes) == 0) {
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_alsa_sink_flush_one_pad (sink, i);
|
|
|
|
continue;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
g_assert (sink->size[i] > 0);
|
|
|
|
if (sink->behaviour[i] != 1)
|
2004-07-08 13:40:37 +00:00
|
|
|
sink->buf_data[i] += bytes;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (snd_pcm_state (this->handle) != SND_PCM_STATE_RUNNING
|
|
|
|
&& snd_pcm_avail_update (this->handle) == 0) {
|
2003-11-16 00:40:01 +00:00
|
|
|
gst_alsa_start (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_change_state (GstElement * element)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
GstAlsaSink *sink;
|
|
|
|
|
|
|
|
g_return_val_if_fail (element != NULL, FALSE);
|
|
|
|
sink = GST_ALSA_SINK (element);
|
|
|
|
|
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
2004-03-14 22:34:34 +00:00
|
|
|
case GST_STATE_NULL_TO_READY:
|
|
|
|
case GST_STATE_READY_TO_PAUSED:
|
|
|
|
case GST_STATE_PAUSED_TO_PLAYING:
|
|
|
|
case GST_STATE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
gst_alsa_sink_flush_pads (sink);
|
|
|
|
break;
|
|
|
|
case GST_STATE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
2004-07-09 10:56:51 +00:00
|
|
|
break;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (sink_parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (sink_parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_alsa_sink_get_time (GstAlsa * this)
|
2003-11-16 00:40:01 +00:00
|
|
|
{
|
|
|
|
snd_pcm_sframes_t delay;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-05-16 02:51:29 +00:00
|
|
|
if (!this->format)
|
|
|
|
return 0;
|
ext/alsa/gstalsamixer.c: Update mixer (to sync with other sessions) if we try to obtain a new value. This makes alsam...
Original commit message from CVS:
* ext/alsa/gstalsamixer.c: (gst_alsa_mixer_update),
(gst_alsa_mixer_get_volume), (gst_alsa_mixer_set_volume),
(gst_alsa_mixer_set_mute), (gst_alsa_mixer_set_record),
(gst_alsa_mixer_set_option), (gst_alsa_mixer_get_option):
Update mixer (to sync with other sessions) if we try to obtain
a new value. This makes alsamixer work accross applications.
* ext/alsa/gstalsasink.c: (gst_alsa_sink_get_time):
Only call sync functions if we're running, else alsalib asserts.
* ext/ogg/gstoggdemux.c: (gst_ogg_demux_src_query):
Sometimes fails to compile. Possibly a gcc bug.
* gst/playback/gstplaybin.c: (gen_video_element),
(gen_audio_element):
Add a reference to an application-provided object, because we lose
this same reference if we add it to the bin. If we don't do this,
we can only use this object once and thus crash if we go from
ready to playing, back to ready and back to playing again.
Also add an audioscale element because several cheap soundcards -
like mine - don't support all samplerates.
* sys/ximage/ximagesink.c: (gst_ximagesink_xcontext_get),
(gst_ximagesink_xcontext_clear), (gst_ximagesink_change_state):
Fix wrong order or PAR calls. Makes automatically obtained PAR
from the X server atually being used.
2004-09-13 09:24:00 +00:00
|
|
|
if (!GST_FLAG_IS_SET (this, GST_ALSA_RUNNING)) {
|
|
|
|
delay = 0;
|
|
|
|
} else if (snd_pcm_delay (this->handle, &delay) != 0) {
|
2004-06-23 18:08:26 +00:00
|
|
|
return this->played / this->format->rate;
|
2004-05-16 02:51:29 +00:00
|
|
|
}
|
2004-06-23 18:08:26 +00:00
|
|
|
if (this->played <= delay) {
|
2003-11-16 00:40:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-16 02:51:29 +00:00
|
|
|
|
2004-06-23 18:08:26 +00:00
|
|
|
return GST_SECOND * (this->played - delay) / this->format->rate;
|
2003-11-16 00:40:01 +00:00
|
|
|
}
|