Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit a0b3b85890ae336134a6785c670ccfa9994b2c76 @Themaister committed Aug 12, 2010
Showing with 82 additions and 0 deletions.
  1. +27 −0 Makefile
  2. +51 −0 alsa-reroute.c
  3. +4 −0 alsa_reroute
@@ -0,0 +1,27 @@
+TARGET = libalsa-reroute.so
+TARGET_SH = alsa_reroute
+
+PREFIX = /usr/local
+
+OBJ = alsa-reroute.o
+
+all: $(TARGET)
+
+$(TARGET): $(OBJ)
+ $(CC) -o $@ $< -ldl -shared
+
+%.o: %.c
+ $(CC) $(CFLAGS) -fPIC -c $< -o $@
+
+clean:
+ rm -rf $(TARGET)
+ rm -rf $(OBJ)
+
+install:
+ install -m755 $(TARGET) $(PREFIX)/lib/
+ install -m755 $(TARGET_SH) $(PREFIX)/bin/
+
+uninstall:
+ rm -rf $(PREFIX)$(TARGET)
+ rm -rf $(PREFIX)$(TARGET_SH)
+
@@ -0,0 +1,51 @@
+#include <dlfcn.h>
+#include <alsa/asoundlib.h>
+#include <stdlib.h>
+
+#ifndef RTLD_NOW
+//#define RTLD_NOW ((void*)2)
+#error "RTLD_NOW not defined. :v"
+#endif
+
+int (*_snd_pcm_open)(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
+
+static const char* correct_device = NULL;
+
+static void init_lib(void)
+{
+ static int libinit = 0;
+
+ correct_device = getenv("ALSA_DEVICE");
+ if ( correct_device && strlen(correct_device) == 0 )
+ {
+ correct_device = NULL;
+ }
+
+ if (!libinit)
+ {
+ void *handle = dlopen("libasound.so", RTLD_NOW);
+ if ( handle )
+ {
+ _snd_pcm_open = dlsym(handle, "snd_pcm_open");
+ if ( !_snd_pcm_open )
+ fprintf(stderr, "ERROR: ALSA_REROUTE. Could not load libasound, all calls to snd_pcm_open() will fail!\n");
+ }
+ }
+ libinit++;
+}
+
+
+int snd_pcm_open(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode)
+{
+ init_lib();
+
+ const char *new_dev = (correct_device) ? correct_device : name;
+
+ fprintf(stderr, "ALSA_REROUTE: Got device: \"%s\" Using device: \"%s\"\n", name, new_dev);
+
+ if ( _snd_pcm_open == NULL )
+ return -1;
+
+ return _snd_pcm_open(pcm, new_dev, stream, mode);
+}
+
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+LD_PRELOAD="libalsa-reroute.so" exec "$@"
+

0 comments on commit a0b3b85

Please sign in to comment.