/* $Id$ */ /*** This file is part of gst-polyp. gst-polyp is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. gst-polyp 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with gst-polyp; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include "polypsink.h" GST_DEBUG_CATEGORY_EXTERN(polyp_debug); #define GST_CAT_DEFAULT polyp_debug enum { ARG_0, ARG_SERVER, ARG_SINK, }; static GstAudioSinkClass *parent_class = NULL; static void gst_polypsink_destroy_stream(GstPolypSink *polypsink); static void gst_polypsink_destroy_context(GstPolypSink *polypsink); static void gst_polypsink_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); static void gst_polypsink_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); static void gst_polypsink_finalize(GObject *object); static gboolean gst_polypsink_open(GstAudioSink *asink); static gboolean gst_polypsink_close(GstAudioSink *asink); static gboolean gst_polypsink_prepare(GstAudioSink *asink, GstRingBufferSpec *spec); static gboolean gst_polypsink_unprepare(GstAudioSink *asink); static guint gst_polypsink_write(GstAudioSink *asink, gpointer data, guint length); static guint gst_polypsink_delay(GstAudioSink *asink); static void gst_polypsink_reset(GstAudioSink *asink); #if (G_BYTE_ORDER == G_LITTLE_ENDIAN) # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN" #else # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN" #endif static void gst_polypsink_base_init(gpointer g_class) { static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE( "sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS( "audio/x-raw-int, " "endianness = (int) { " ENDIANNESS " }, " "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16, " "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 16 ];" "audio/x-raw-int, " "signed = (boolean) FALSE, " "width = (int) 8, " "depth = (int) 8, " "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 16 ];" "audio/x-raw-float, " "endianness = (int) { " ENDIANNESS " }, " "width = (int) 32, " "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 16 ];" "audio/x-alaw, " "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 16 ];" "audio/x-mulaw, " "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 16 ]" ) ); static const GstElementDetails details = GST_ELEMENT_DETAILS( "Polypaudio audio sink", "Sink/Audio", "Plays audio to a Polypaudio server", "Lennart Poettering"); GstElementClass *element_class = GST_ELEMENT_CLASS(g_class); gst_element_class_set_details(element_class, &details); gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&pad_template)); } static void gst_polypsink_class_init( gpointer g_class, gpointer class_data) { GObjectClass *gobject_class = G_OBJECT_CLASS(g_class); // GstElementClass *gstelement_class = GST_ELEMENT_CLASS(g_class); GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS(g_class); parent_class = g_type_class_peek_parent(g_class); gobject_class->set_property = gst_polypsink_set_property; gobject_class->get_property = gst_polypsink_get_property; gobject_class->finalize = gst_polypsink_finalize; gstaudiosink_class->open = GST_DEBUG_FUNCPTR(gst_polypsink_open); gstaudiosink_class->close = GST_DEBUG_FUNCPTR(gst_polypsink_close); gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR(gst_polypsink_prepare); gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR(gst_polypsink_unprepare); gstaudiosink_class->write = GST_DEBUG_FUNCPTR(gst_polypsink_write); gstaudiosink_class->delay = GST_DEBUG_FUNCPTR(gst_polypsink_delay); gstaudiosink_class->reset = GST_DEBUG_FUNCPTR(gst_polypsink_reset); /* Overwrite GObject fields */ g_object_class_install_property( gobject_class, ARG_SERVER, g_param_spec_string("server", "Server", "The Polypaudio server to connect to", NULL, G_PARAM_READWRITE)); g_object_class_install_property( gobject_class, ARG_SINK, g_param_spec_string("sink", "Sink", "The Polypaudio sink device to connect to", NULL, G_PARAM_READWRITE)); } static void gst_polypsink_init( GTypeInstance * instance, gpointer g_class) { GstPolypSink *polypsink = GST_POLYPSINK(instance); int e; polypsink->server = polypsink->device = NULL; polypsink->context = NULL; polypsink->stream = NULL; polypsink->mainloop = pa_threaded_mainloop_new(); g_assert(polypsink->mainloop); e = pa_threaded_mainloop_start(polypsink->mainloop); g_assert(e == 0); } static void gst_polypsink_destroy_stream(GstPolypSink* polypsink) { if (polypsink->stream) { pa_stream_disconnect(polypsink->stream); pa_stream_unref(polypsink->stream); polypsink->stream = NULL; } } static void gst_polypsink_destroy_context(GstPolypSink* polypsink) { gst_polypsink_destroy_stream(polypsink); if (polypsink->context) { pa_context_disconnect(polypsink->context); pa_context_unref(polypsink->context); polypsink->context = NULL; } } static void gst_polypsink_finalize(GObject * object) { GstPolypSink *polypsink = GST_POLYPSINK(object); pa_threaded_mainloop_stop(polypsink->mainloop); gst_polypsink_destroy_context(polypsink); g_free(polypsink->server); g_free(polypsink->device); pa_threaded_mainloop_free(polypsink->mainloop); G_OBJECT_CLASS(parent_class)->finalize(object); } static void gst_polypsink_set_property( GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstPolypSink *polypsink = GST_POLYPSINK(object); switch (prop_id) { case ARG_SERVER: g_free(polypsink->server); polypsink->server = g_value_dup_string(value); break; case ARG_SINK: g_free(polypsink->device); polypsink->device = g_value_dup_string(value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; } } static void gst_polypsink_get_property( GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstPolypSink *polypsink = GST_POLYPSINK(object); switch(prop_id) { case ARG_SERVER: g_value_set_string(value, polypsink->server); break; case ARG_SINK: g_value_set_string(value, polypsink->device); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; } } static void gst_polypsink_context_state_cb(pa_context *c, void *userdata) { GstPolypSink *polypsink = GST_POLYPSINK(userdata); switch (pa_context_get_state(c)) { case PA_CONTEXT_READY: case PA_CONTEXT_TERMINATED: case PA_CONTEXT_FAILED: pa_threaded_mainloop_signal(polypsink->mainloop, 0); break; case PA_CONTEXT_UNCONNECTED: case PA_CONTEXT_CONNECTING: case PA_CONTEXT_AUTHORIZING: case PA_CONTEXT_SETTING_NAME: break; } } static void gst_polypsink_stream_state_cb(pa_stream *s, void * userdata) { GstPolypSink *polypsink = GST_POLYPSINK(userdata); switch (pa_stream_get_state(s)) { case PA_STREAM_READY: case PA_STREAM_FAILED: case PA_STREAM_TERMINATED: pa_threaded_mainloop_signal(polypsink->mainloop, 0); break; case PA_STREAM_UNCONNECTED: case PA_STREAM_CREATING: break; } } static void gst_polypsink_stream_request_cb(pa_stream *s, size_t length, void *userdata) { GstPolypSink *polypsink = GST_POLYPSINK(userdata); pa_threaded_mainloop_signal(polypsink->mainloop, 0); } static gboolean gst_polypsink_open(GstAudioSink *asink) { GstPolypSink *polypsink = GST_POLYPSINK(asink); pa_threaded_mainloop_lock(polypsink->mainloop); if (!(polypsink->context = pa_context_new(pa_threaded_mainloop_get_api(polypsink->mainloop), "gstreamer"))) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to create context"), (NULL)); goto unlock_and_fail; } pa_context_set_state_callback(polypsink->context, gst_polypsink_context_state_cb, polypsink); if (pa_context_connect(polypsink->context, polypsink->server, 0, NULL) < 0) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to connect: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } /* Wait until the context is ready */ pa_threaded_mainloop_wait(polypsink->mainloop); if (pa_context_get_state(polypsink->context) != PA_CONTEXT_READY) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to connect: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } pa_threaded_mainloop_unlock(polypsink->mainloop); return TRUE; unlock_and_fail: pa_threaded_mainloop_unlock(polypsink->mainloop); return FALSE; } static gboolean gst_polypsink_close(GstAudioSink *asink) { GstPolypSink *polypsink = GST_POLYPSINK(asink); pa_threaded_mainloop_lock(polypsink->mainloop); gst_polypsink_destroy_context(polypsink); pa_threaded_mainloop_unlock(polypsink->mainloop); return TRUE; } static gboolean gst_polypsink_fill_sample_spec(GstRingBufferSpec *spec, pa_sample_spec *ss) { if (spec->format == GST_MU_LAW && spec->width == 8) ss->format = PA_SAMPLE_ULAW; else if (spec->format == GST_A_LAW && spec->width == 8) ss->format = PA_SAMPLE_ALAW; else if (spec->format == GST_U8 && spec->width == 8) ss->format = PA_SAMPLE_U8; else if (spec->format == GST_S16_LE && spec->width == 16) ss->format = PA_SAMPLE_S16LE; else if (spec->format == GST_S16_BE && spec->width == 16) ss->format = PA_SAMPLE_S16BE; else if (spec->format == GST_FLOAT32_LE && spec->width == 32) ss->format = PA_SAMPLE_FLOAT32LE; else if (spec->format == GST_FLOAT32_BE && spec->width == 32) ss->format = PA_SAMPLE_FLOAT32BE; else return FALSE; ss->channels = spec->channels; ss->rate = spec->rate; if (!pa_sample_spec_valid(ss)) return FALSE; return TRUE; } static gboolean gst_polypsink_prepare(GstAudioSink *asink, GstRingBufferSpec *spec) { pa_buffer_attr buf_attr; GstPolypSink *polypsink = GST_POLYPSINK(asink); if (!gst_polypsink_fill_sample_spec(spec, &polypsink->sample_spec)) { GST_ELEMENT_ERROR(polypsink, RESOURCE, SETTINGS, ("Invalid sample specification."), (NULL)); goto unlock_and_fail; } pa_threaded_mainloop_lock(polypsink->mainloop); if (!polypsink->context || pa_context_get_state(polypsink->context) != PA_CONTEXT_READY) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Bad context state: %s", polypsink->context ? pa_strerror(pa_context_errno(polypsink->context)) : NULL), (NULL)); goto unlock_and_fail; } if (!(polypsink->stream = pa_stream_new(polypsink->context, "the stream", &polypsink->sample_spec, NULL))) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to create stream: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } pa_stream_set_state_callback(polypsink->stream, gst_polypsink_stream_state_cb, polypsink); pa_stream_set_write_callback(polypsink->stream, gst_polypsink_stream_request_cb, polypsink); memset(&buf_attr, 0, sizeof(buf_attr)); buf_attr.tlength = spec->segtotal*spec->segsize; buf_attr.maxlength = buf_attr.tlength*2; buf_attr.prebuf = buf_attr.minreq = spec->segsize; if (pa_stream_connect_playback(polypsink->stream, polypsink->device, &buf_attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE|PA_STREAM_NOT_MONOTONOUS, NULL, NULL) < 0) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to connext stream: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } /* Wait until the stream is ready */ pa_threaded_mainloop_wait(polypsink->mainloop); if (pa_stream_get_state(polypsink->stream) != PA_STREAM_READY) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Failed to connext stream: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } pa_threaded_mainloop_unlock(polypsink->mainloop); spec->bytes_per_sample = pa_frame_size(&polypsink->sample_spec); memset(spec->silence_sample, 0, spec->bytes_per_sample); return TRUE; unlock_and_fail: pa_threaded_mainloop_unlock(polypsink->mainloop); return FALSE; } static gboolean gst_polypsink_unprepare(GstAudioSink * asink) { GstPolypSink *polypsink = GST_POLYPSINK(asink); pa_threaded_mainloop_lock(polypsink->mainloop); gst_polypsink_destroy_stream(polypsink); pa_threaded_mainloop_unlock(polypsink->mainloop); return TRUE; } #define CHECK_DEAD_GOTO(polypsink, label) \ if (!(polypsink)->context || pa_context_get_state((polypsink)->context) != PA_CONTEXT_READY || \ !(polypsink)->stream || pa_stream_get_state((polypsink)->stream) != PA_STREAM_READY) { \ GST_ELEMENT_ERROR((polypsink), RESOURCE, FAILED, ("Disconnected: %s", (polypsink)->context ? pa_strerror(pa_context_errno((polypsink)->context)) : NULL), (NULL)); \ goto label; \ } static guint gst_polypsink_write(GstAudioSink *asink, gpointer data, guint length) { GstPolypSink *polypsink = GST_POLYPSINK(asink); size_t l; pa_threaded_mainloop_lock(polypsink->mainloop); for (;;) { CHECK_DEAD_GOTO(polypsink, unlock_and_fail); if ((l = pa_stream_writable_size(polypsink->stream))) break; pa_threaded_mainloop_wait(polypsink->mainloop); } if (l == (size_t) -1) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("pa_stream_writable_size() failed: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } if (l > length) l = length; if (pa_stream_write(polypsink->stream, data, l, NULL, 0, PA_SEEK_RELATIVE) < 0) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("pa_stream_write() failed: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } pa_threaded_mainloop_unlock(polypsink->mainloop); GST_WARNING("Write"); return l; unlock_and_fail: pa_threaded_mainloop_unlock(polypsink->mainloop); return 0; } static guint gst_polypsink_delay(GstAudioSink *asink) { GstPolypSink *polypsink = GST_POLYPSINK(asink); pa_usec_t t; pa_threaded_mainloop_lock(polypsink->mainloop); CHECK_DEAD_GOTO(polypsink, unlock_and_fail); if (pa_stream_get_latency(polypsink->stream, &t, NULL) < 0) { if (pa_context_errno(polypsink->context) != PA_ERR_NODATA) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("pa_stream_get_latency() failed: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } GST_WARNING("Not data while querying latency"); t = 0; } GST_WARNING("Latency %0.3f ms", (double) t / 1000); pa_threaded_mainloop_unlock(polypsink->mainloop); return (guint) ((t * polypsink->sample_spec.rate) / 1000000LL); unlock_and_fail: pa_threaded_mainloop_unlock(polypsink->mainloop); return 0; } static void gst_polypsink_success_cb(pa_stream *s, int success, void *userdata) { GstPolypSink *polypsink = GST_POLYPSINK(userdata); polypsink->operation_success = success; pa_threaded_mainloop_signal(polypsink->mainloop, 0); } static void gst_polypsink_reset(GstAudioSink *asink) { GstPolypSink *polypsink = GST_POLYPSINK(asink); pa_operation *o = NULL; pa_threaded_mainloop_lock(polypsink->mainloop); CHECK_DEAD_GOTO(polypsink, unlock_and_fail); if (!(o = pa_stream_flush(polypsink->stream, gst_polypsink_success_cb, polypsink))) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("pa_stream_flush() failed: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } polypsink->operation_success = 0; while (pa_operation_get_state(o) != PA_OPERATION_DONE) { CHECK_DEAD_GOTO(polypsink, unlock_and_fail); pa_threaded_mainloop_wait(polypsink->mainloop); } GST_WARNING("Reset"); if (!polypsink->operation_success) { GST_ELEMENT_ERROR(polypsink, RESOURCE, FAILED, ("Flush failed: %s", pa_strerror(pa_context_errno(polypsink->context))), (NULL)); goto unlock_and_fail; } unlock_and_fail: if (o) { pa_operation_cancel(o); pa_operation_unref(o); } pa_threaded_mainloop_unlock(polypsink->mainloop); } GType gst_polypsink_get_type(void) { static GType polypsink_type = 0; if (!polypsink_type) { static const GTypeInfo polypsink_info = { sizeof(GstPolypSinkClass), gst_polypsink_base_init, NULL, gst_polypsink_class_init, NULL, NULL, sizeof(GstPolypSink), 0, gst_polypsink_init, }; polypsink_type = g_type_register_static( GST_TYPE_AUDIO_SINK, "GstPolypSink", &polypsink_info, 0); } return polypsink_type; }