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
|
|
|
{
|
|
|
|
switch (sink->behaviour[i]) {
|
2004-03-14 22:34:34 +00:00
|
|
|
case 0:
|
|
|
|
if (sink->buf[i])
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_data_unref (GST_DATA (sink->buf[i]));
|
2004-03-14 22:34:34 +00:00
|
|
|
sink->buf[i] = NULL;
|
|
|
|
sink->data[i] = NULL;
|
|
|
|
sink->behaviour[i] = 0;
|
|
|
|
sink->size[i] = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
g_free (sink->data[i]);
|
|
|
|
sink->data[i] = NULL;
|
|
|
|
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;
|
|
|
|
GstEvent *event = GST_EVENT (sink->buf[pad_nr]);
|
|
|
|
GstAlsa *this = GST_ALSA (sink);
|
|
|
|
|
|
|
|
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-03-15 19:32:28 +00:00
|
|
|
GstClockTime value;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &value)) {
|
|
|
|
gst_element_set_time (GST_ELEMENT (this), value);
|
2004-05-09 00:02:26 +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,
|
|
|
|
&value))) {
|
2004-03-15 19:32:28 +00:00
|
|
|
value = gst_alsa_samples_to_timestamp (this, value);
|
|
|
|
gst_element_set_time (GST_ELEMENT (this), value);
|
|
|
|
} 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
|
|
|
}
|
|
|
|
gst_event_unref (event);
|
|
|
|
sink->buf[pad_nr] = NULL;
|
|
|
|
} 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++) {
|
|
|
|
src[i].addr = sink->data[0];
|
|
|
|
src[i].first = i * width;
|
|
|
|
src[i].step = this->format->channels * width;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* noninterleaved */
|
|
|
|
for (i = 0; i < this->format->channels; i++) {
|
|
|
|
src[i].addr = sink->data[i];
|
|
|
|
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 */
|
|
|
|
err = snd_pcm_writei (this->handle, sink->data[0], *avail);
|
|
|
|
} else {
|
|
|
|
/* noninterleaved */
|
|
|
|
for (i = 0; i < this->format->channels; i++) {
|
|
|
|
channels[i] = sink->data[i];
|
|
|
|
}
|
|
|
|
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-01-15 02:14:54 +00:00
|
|
|
snd_pcm_uframes_t samplestamp, time_sample;
|
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 */
|
|
|
|
/* 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++) {
|
|
|
|
g_assert (this->pad[i] != NULL);
|
|
|
|
while (sink->size[i] == 0) {
|
2004-03-15 19:32:28 +00:00
|
|
|
if (!sink->buf[i])
|
|
|
|
sink->buf[i] = GST_BUFFER (gst_pad_pull (this->pad[i]));
|
|
|
|
if (GST_IS_EVENT (sink->buf[i])) {
|
|
|
|
if (gst_alsa_sink_check_event (sink, i))
|
|
|
|
continue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* caps nego failed somewhere */
|
|
|
|
if (this->format == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (this, CORE, NEGOTIATION, (NULL),
|
|
|
|
("ALSA format not negotiated"));
|
|
|
|
}
|
|
|
|
samplestamp =
|
|
|
|
gst_alsa_timestamp_to_samples (this,
|
|
|
|
GST_BUFFER_TIMESTAMP (sink->buf[i]));
|
|
|
|
max_discont = gst_alsa_timestamp_to_samples (this, this->max_discont);
|
|
|
|
time_sample =
|
|
|
|
gst_alsa_timestamp_to_samples (this,
|
|
|
|
gst_element_get_time (GST_ELEMENT (this)));
|
|
|
|
snd_pcm_delay (this->handle, &sample_diff);
|
|
|
|
/* actual diff = buffer samplestamp - played - to_play */
|
|
|
|
sample_diff = samplestamp - time_sample - sample_diff;
|
|
|
|
|
|
|
|
if ((!GST_BUFFER_TIMESTAMP_IS_VALID (sink->buf[i])) ||
|
|
|
|
(-max_discont <= sample_diff && sample_diff <= max_discont)) {
|
|
|
|
|
|
|
|
/* difference between expected and current is < GST_ALSA_DEVIATION */
|
|
|
|
no_difference:
|
|
|
|
sink->size[i] = sink->buf[i]->size;
|
|
|
|
sink->data[i] = sink->buf[i]->data;
|
|
|
|
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,
|
|
|
|
"Allocating %d bytes (%ld samples) now to resync: sample %ld expected, but got %ld",
|
|
|
|
size, MIN (bytes, sample_diff), time_sample, samplestamp);
|
|
|
|
sink->data[i] = g_try_malloc (size);
|
|
|
|
if (!sink->data[i]) {
|
|
|
|
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,
|
|
|
|
sink->data[i], samples)) {
|
|
|
|
GST_WARNING_OBJECT (this,
|
|
|
|
"error silencing buffer, enjoy the noise.");
|
|
|
|
}
|
|
|
|
sink->behaviour[i] = 1;
|
|
|
|
} else if (gst_alsa_samples_to_bytes (this,
|
|
|
|
-sample_diff) >= sink->buf[i]->size) {
|
|
|
|
GST_INFO_OBJECT (this,
|
|
|
|
"Skipping %lu samples to resync (complete buffer): sample %ld expected, but got %ld",
|
|
|
|
gst_alsa_bytes_to_samples (this, sink->buf[i]->size), time_sample,
|
|
|
|
samplestamp);
|
|
|
|
/* this buffer is way behind */
|
|
|
|
gst_buffer_unref (sink->buf[i]);
|
|
|
|
sink->buf[i] = NULL;
|
|
|
|
continue;
|
|
|
|
} else if (sample_diff < 0) {
|
|
|
|
gint difference = gst_alsa_samples_to_bytes (this, -samplestamp);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (this,
|
|
|
|
"Skipping %lu samples to resync: sample %ld expected, but got %ld",
|
|
|
|
(gulong) - sample_diff, time_sample, samplestamp);
|
|
|
|
/* this buffer is only a bit behind */
|
|
|
|
sink->size[i] = sink->buf[i]->size - difference;
|
|
|
|
sink->data[i] = sink->buf[i]->data + difference;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
this->transmitted += copied;
|
|
|
|
/* 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-03-15 19:32:28 +00:00
|
|
|
sink->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:
|
|
|
|
g_assert_not_reached ();
|
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-01-31 17:19:21 +00:00
|
|
|
if (snd_pcm_delay (this->handle, &delay) == 0 && this->format) {
|
2004-03-14 22:34:34 +00:00
|
|
|
return GST_SECOND * (GstClockTime) (this->transmitted >
|
2004-03-15 19:32:28 +00:00
|
|
|
delay ? this->transmitted - delay : 0) / this->format->rate;
|
2003-11-16 00:40:01 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|