]> code.delx.au - pulseaudio/blobdiff - polyp/module-alsa-sink.c
Merge Pierre's changes
[pulseaudio] / polyp / module-alsa-sink.c
index 0c9d77b809358444347e19d53e329e29d02c0503..dde5d8b6996d78b70d7679dbae0cde86f95dcf62 100644 (file)
@@ -4,7 +4,7 @@
   This file is part of polypaudio.
  
   polypaudio is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
+  it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
@@ -13,7 +13,7 @@
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
 
 #include <assert.h>
 #include <stdio.h>
+
+#ifdef HAVE_SYS_POLL_H
 #include <sys/poll.h>
+#else
+#include "poll.h"
+#endif
 
 #include <asoundlib.h>
 
 #include "sample-util.h"
 #include "alsa-util.h"
 #include "xmalloc.h"
+#include "log.h"
+#include "module-alsa-sink-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering")
+PA_MODULE_DESCRIPTION("ALSA Sink")
+PA_MODULE_VERSION(PACKAGE_VERSION)
+PA_MODULE_USAGE("sink_name=<name for the sink> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
+
+#define PA_TYPEID_ALSA PA_TYPEID_MAKE('A', 'L', 'S', 'A')
 
 struct userdata {
     snd_pcm_t *pcm_handle;
@@ -62,7 +76,7 @@ static const char* const valid_modargs[] = {
 };
 
 #define DEFAULT_SINK_NAME "alsa_output"
-#define DEFAULT_DEVICE "plughw:0,0"
+#define DEFAULT_DEVICE "default"
 
 static void update_usage(struct userdata *u) {
    pa_module_set_used(u->module,
@@ -73,10 +87,10 @@ static void update_usage(struct userdata *u) {
 static void xrun_recovery(struct userdata *u) {
     assert(u);
 
-    fprintf(stderr, "*** ALSA-XRUN (playback) ***\n");
+    pa_log(__FILE__": *** ALSA-XRUN (playback) ***\n");
     
     if (snd_pcm_prepare(u->pcm_handle) < 0)
-        fprintf(stderr, "snd_pcm_prepare() failed\n");
+        pa_log(__FILE__": snd_pcm_prepare() failed\n");
 }
 
 static void do_write(struct userdata *u) {
@@ -108,7 +122,7 @@ static void do_write(struct userdata *u) {
                 continue;
             }
 
-            fprintf(stderr, "snd_pcm_writei() failed\n");
+            pa_log(__FILE__": snd_pcm_writei() failed\n");
             return;
         }
 
@@ -138,13 +152,14 @@ static void io_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd,
     do_write(u);
 }
 
-static uint32_t sink_get_latency_cb(struct pa_sink *s) {
+static pa_usec_t sink_get_latency_cb(struct pa_sink *s) {
+    pa_usec_t r = 0;
     struct userdata *u = s->userdata;
     snd_pcm_sframes_t frames;
     assert(s && u && u->sink);
 
     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
-        fprintf(stderr, __FILE__": failed to get delay\n");
+        pa_log(__FILE__": failed to get delay\n");
         s->get_latency = NULL;
         return 0;
     }
@@ -152,54 +167,60 @@ static uint32_t sink_get_latency_cb(struct pa_sink *s) {
     if (frames < 0)
         frames = 0;
     
-    return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
+    r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
+
+    if (u->memchunk.memblock)
+        r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
+
+    return r;
 }
 
-int pa_module_init(struct pa_core *c, struct pa_module*m) {
+int pa__init(struct pa_core *c, struct pa_module*m) {
     struct pa_modargs *ma = NULL;
     int ret = -1;
     struct userdata *u = NULL;
     const char *dev;
     struct pa_sample_spec ss;
-    unsigned periods, fragsize;
-    snd_pcm_uframes_t buffer_size;
+    uint32_t periods, fragsize;
+    snd_pcm_uframes_t period_size;
     size_t frame_size;
 
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
-        fprintf(stderr, __FILE__": failed to parse module arguments\n");
+        pa_log(__FILE__": failed to parse module arguments\n");
         goto fail;
     }
 
     ss = c->default_sample_spec;
     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
-        fprintf(stderr, __FILE__": failed to parse sample specification\n");
+        pa_log(__FILE__": failed to parse sample specification\n");
         goto fail;
     }
     frame_size = pa_frame_size(&ss);
     
-    periods = 12;
+    periods = 8;
     fragsize = 1024;
     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
-        fprintf(stderr, __FILE__": failed to parse buffer metrics\n");
+        pa_log(__FILE__": failed to parse buffer metrics\n");
         goto fail;
     }
-    buffer_size = fragsize/frame_size*periods;
+    period_size = fragsize;
     
     u = pa_xmalloc0(sizeof(struct userdata));
     m->userdata = u;
     u->module = m;
     
+    snd_config_update_free_global();
     if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) {
-        fprintf(stderr, __FILE__": Error opening PCM device %s\n", dev);
+        pa_log(__FILE__": Error opening PCM device %s\n", dev);
         goto fail;
     }
 
-    if (pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &buffer_size) < 0) {
-        fprintf(stderr, __FILE__": Failed to set hardware parameters\n");
+    if (pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size) < 0) {
+        pa_log(__FILE__": Failed to set hardware parameters\n");
         goto fail;
     }
 
-    u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
+    u->sink = pa_sink_new(c, PA_TYPEID_ALSA, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
     assert(u->sink);
 
     u->sink->get_latency = sink_get_latency_cb;
@@ -208,14 +229,14 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) {
     u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
 
     if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
-        fprintf(stderr, __FILE__": failed to obtain file descriptors\n");
+        pa_log(__FILE__": failed to obtain file descriptors\n");
         goto fail;
     }
     
     u->frame_size = frame_size;
-    u->fragment_size = buffer_size*u->frame_size/periods;
+    u->fragment_size = period_size;
 
-    fprintf(stderr, __FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
+    pa_log(__FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
 
     u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size, c->memblock_stat);
     assert(u->silence.memblock);
@@ -236,20 +257,22 @@ finish:
 fail:
     
     if (u)
-        pa_module_done(c, m);
+        pa__done(c, m);
 
     goto finish;
 }
 
-void pa_module_done(struct pa_core *c, struct pa_module*m) {
+void pa__done(struct pa_core *c, struct pa_module*m) {
     struct userdata *u;
     assert(c && m);
 
     if (!(u = m->userdata))
         return;
     
-    if (u->sink)
-        pa_sink_free(u->sink);
+    if (u->sink) {
+        pa_sink_disconnect(u->sink);
+        pa_sink_unref(u->sink);
+    }
     
     if (u->io_events)
         pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);