Blame SOURCES/alsa-delay

a3661c
#!/bin/bash
a3661c
a3661c
version=0.1.0
a3661c
tmpdir=$TMPDIR
a3661c
if test -z "$tmpdir"; then
a3661c
	tmpdir="/tmp"
a3661c
fi
a3661c
tmpdir=$tmpdir/alsa-delay-script
a3661c
a3661c
delay=
a3661c
pcard=
a3661c
pdev=
a3661c
ccard=
a3661c
cdev=
a3661c
a3661c
yes=
a3661c
quiet=
a3661c
clean=
a3661c
remove=
a3661c
a3661c
pdevice="default:%s"
a3661c
pdevice_set=
a3661c
cdevice="plughw:%s,%s,%s"
a3661c
pctl=
a3661c
cctl=
a3661c
one=
a3661c
arg=
a3661c
mix=
a3661c
pa=
a3661c
a3661c
useprocfs=yes
a3661c
a3661c
fuser_prg=fuser
a3661c
insmod_prg=insmod
a3661c
rmmod_prg=rmmod
a3661c
lsmod_prg=lsmod
a3661c
modprobe_prg=modprobe
a3661c
chkconfig_prg=chkconfig
a3661c
systemctl_prg=systemctl
a3661c
pidof_prg=pidof
a3661c
alsaloop_prg=alsaloop
a3661c
amixer_prg=amixer
a3661c
test -x /sbin/fuser && fuser_prg=/sbin/fuser
a3661c
test -x /sbin/insmod && insmod_prg=/sbin/insmod
a3661c
test -x /sbin/rmmod && rmmod_prg=/sbin/rmmod
a3661c
test -x /sbin/lsmod && lsmod_prg=/sbin/lsmod
a3661c
test -x /sbin/modprobe && modprobe_prg=/sbin/modprobe
a3661c
test -x /sbin/chkconfig && chkconfig_prg=/sbin/chkconfig
a3661c
test -x /bin/systemctl && systemctl_prg=/bin/systemctl
a3661c
test -x /sbin/pidof && pidof_prg=/sbin/pidof
a3661c
test -x /usr/bin/alsaloop && alsaloop_prg=/usr/bin/alsaloop
a3661c
test -x /usr/local/bin/alsaloop && alsaloop_prg=/usr/local/bin/alsaloop
a3661c
test -x /usr/bin/amixer && amixer_prg=/usr/bin/amixer
a3661c
a3661c
modprobeconf=/etc/modprobe.d/alsa.conf
a3661c
test -r /etc/modprobe.conf && modprobeconf=/etc/modprobe.conf && useprocfs=""
a3661c
alsaloopconf=/etc/alsaloop.conf
a3661c
a3661c
test -r modprobe.work && modprobeconf=modprobe.work
a3661c
test -r modprobe.work && alsaloopconf=alsaloop.conf
a3661c
a3661c
usage() {
a3661c
	echo "Usage: $0 [OPTION]... <requested_delay_in_ms> [<output_card>[,<device>]]"
a3661c
	cat <
a3661c
a3661c
This is a delay utility version $version. The snd-aloop ALSA driver is
a3661c
used to send all PCM streams back to the user space and the alsaloop
a3661c
utility is used to send this stream to a real hardware.
a3661c
a3661c
<output_card> is ALSA card index (number) or string card identifier
a3661c
<device> is ALSA device number
a3661c
a3661c
Use 'aplay -l' to list available cards and devices.
a3661c
a3661c
Operation modes:
a3661c
  -h, --help		print this help, then exit
a3661c
  -q, --quiet		quiet mode
a3661c
  -y, --yes		do not ask any questions - answer is always yes
a3661c
  -c, --clean           clean temporary directory and exit
a3661c
  -r, --remove          remove the alsa-delay config modifications and exit
a3661c
  --tmpdir=<DIR>        set temporary directory
a3661c
Alsaloop options:
a3661c
  --pdevice=<DEV>       force playback device
a3661c
  --cdevice=<DEV>       force capture device
a3661c
  --pctl=<DEV>          force playback ctl device
a3661c
  --cctl=<DEV>          force capture ctl device
a3661c
  --one=<ARG>           pass this argument to last thread
a3661c
  --arg=<ARG>           pass this argument to all threads
a3661c
  --mix=<ARG>           redirect ALSA mixer controls to OSS mixer
a3661c
			(default is Master)
a3661c
  --pa			Redirect PA to alsaloop
a3661c
a3661c
Note: For devices, the string %s is replaced with the card index
a3661c
      and second string %s is replaced with the device index
a3661c
      and third string %s is replaced the substream index (0-7).
a3661c
      Example: hw:%s,%s,%s    (card, device, substream)
a3661c
EOF
a3661c
}
a3661c
a3661c
while :
a3661c
do
a3661c
	case "$1" in
a3661c
	-h|--help)
a3661c
		usage
a3661c
		exit 0
a3661c
		;;
a3661c
	-q|--quiet)
a3661c
		quiet=true ;;
a3661c
	-y|--yes)
a3661c
		yes=true ;;
a3661c
	-c|--clean)
a3661c
		clean="full" ;;
a3661c
	-r|--remove)
a3661c
		remove="full" ;;
a3661c
	--tmpdir*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			tmpdir=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			tmpdir="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		tmpdir="$tmpdir/alsa-compile-script"
a3661c
		;;
a3661c
	--pdevice*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			pdevice=`expr "z$1" : 'z-[^=]*=\(.*\)'`
a3661c
			pdevice_set=yes ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			pdevice="$2"
a3661c
			pdevice_set=yes
a3661c
			shift ;;
a3661c
		esac
a3661c
		;;
a3661c
	--cdevice*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			cdevice=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			cdevice="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		;;
a3661c
	--pctl*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			pctl=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			pctl="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		;;
a3661c
	--cctl*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			cctl=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			cctl="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		;;
a3661c
	--one*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			one1=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			one1="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		one="$one $one1"
a3661c
		;;
a3661c
	--arg*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			arg1=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			arg1="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		arg="$arg $arg1"
a3661c
		;;
a3661c
	--mix*)
a3661c
		case "$#,$1" in
a3661c
		*,*=*)
a3661c
			mix=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
a3661c
		1,*)
a3661c
			usage ;;
a3661c
		*)
a3661c
			mix="$2"
a3661c
			shift ;;
a3661c
		esac
a3661c
		arg="$arg $arg1"
a3661c
		;;
a3661c
	--pa)
a3661c
		pa=true
a3661c
		;;
a3661c
	*)
a3661c
		if test -n "$1"; then
a3661c
			ok=
a3661c
			if test -z "$delay"; then
a3661c
				delay="$1"
a3661c
				if test "$delay" -lt 1000; then
a3661c
					delay=$[$delay * 1000]
a3661c
				fi
a3661c
				ok=true
a3661c
			fi
a3661c
			if test -z "$ok" -a -z "$pcard"; then
a3661c
				pcard="$1"
a3661c
				ok=true
a3661c
			fi
a3661c
			if test -z "$ok"; then
a3661c
				echo "Unknown parameter '$1'"
a3661c
				break
a3661c
			fi
a3661c
		else
a3661c
			break
a3661c
		fi
a3661c
		;;
a3661c
	esac
a3661c
	shift
a3661c
done
a3661c
a3661c
test -z "$pdevice_set" -a -n "$pa" && pdevice="plug:dmix:%s"
a3661c
test -z "$delay" && delay="50000"
a3661c
test -z "$pcard" && pcard="1"
a3661c
pdev=$(echo $pcard | cut -s -d , -f 2)
a3661c
pcard=$(echo $pcard | cut -d , -f 1)
a3661c
test -z "$pdev" && pdev="0"
a3661c
test -z "$ccard" && ccard="Loopback,1"
a3661c
cdev=$(echo $ccard | cut -s -d , -f 2)
a3661c
ccard=$(echo $ccard | cut -d , -f 1)
a3661c
test -z "$cdev" && cdev="0"
a3661c
a3661c
# Echo "true" or "false", depending on $yes and user response to prompt
a3661c
# $1 is prompt message
a3661c
question_bool() {
a3661c
	if test "$yes" = "yes"; then
a3661c
		echo "true"
a3661c
	else
a3661c
		echo >&2 -n "$1 (Y/ ) "
a3661c
		read i
a3661c
		local i=${i:0:1}
a3661c
		if test "$i" = "Y" -o "$i" = "y"; then
a3661c
			echo "true"
a3661c
		else
a3661c
			echo "false"
a3661c
		fi
a3661c
	fi
a3661c
}
a3661c
a3661c
# Safe exit
a3661c
safe_exit() {
a3661c
	if test -r /etc/pulse/client.conf ; then
a3661c
		if grep "# alsa-delay-line-to-be-removed-1234" /etc/pulse/client.conf 2> /dev/null > /dev/null ; then
a3661c
			grep -v alsa-delay-line-to-be-removed-1234 /etc/pulse/client.conf > /etc/pulse/client.conf.new
a3661c
			if test -s /etc/pulse/client.conf.new; then
a3661c
				mv /etc/pulse/client.conf.new /etc/pulse/client.conf
a3661c
			fi
a3661c
		fi
a3661c
	fi
a3661c
	exit $1
a3661c
}
a3661c
a3661c
# Log and execute $@ and check success
a3661c
do_cmd() {
a3661c
	if test -z "$quiet"; then
a3661c
		echo "> $@"
a3661c
	fi
a3661c
	$@ || safe_exit 1
a3661c
}
a3661c
a3661c
# Cache or restore $protocol and $url and $package in $tmpdir
a3661c
check_environment() {
a3661c
	if ! test -d $tmpdir ; then
a3661c
		mkdir -p $tmpdir
a3661c
		if ! test -d $tmpdir; then
a3661c
			echo >&2 "Unable to create directory $tmpdir."
a3661c
			exit 1
a3661c
		fi
a3661c
	fi
a3661c
	echo "Using temporary tree: $tmpdir"
a3661c
	test -x /bin/depmod && depmodbin=/bin/depmod
a3661c
	test -x /sbin/depmod && depmodbin=/sbin/depmod
a3661c
	if test -z "$depmodbin"; then
a3661c
		echo >&2 "Unable to find depmod utility."
a3661c
		exit 1
a3661c
	fi
a3661c
	test -x /bin/modinfo && modinfobin=/bin/modinfo
a3661c
	test -x /sbin/modinfo && modinfobin=/sbin/modinfo
a3661c
	if test -z "$modinfobin"; then
a3661c
		echo >&2 "Unable to find modinfo utility."
a3661c
		exit 1
a3661c
	fi
a3661c
}
a3661c
a3661c
# Kill processes currently accessing the audio devices
a3661c
kill_audio_apps() {
a3661c
	local pids0=$($fuser_prg /dev/snd/* 2> /dev/null)
a3661c
	local pids1=$($fuser_prg /dev/mixer* 2> /dev/null)
a3661c
	local pids2=$($fuser_prg /dev/sequencer* 2> /dev/null)
a3661c
	local pids=
a3661c
	for pid in $pids0 $pids1 $pids2; do
a3661c
		local pids="$pids $pid"
a3661c
	done
a3661c
	if ! test -z "$pids"; then
a3661c
		echo
a3661c
		echo "WARNING! An audio application uses ALSA driver:"
a3661c
		echo
a3661c
		for pid in $pids; do
a3661c
			ps --no-headers -p $pids || safe_exit 1
a3661c
		done
a3661c
		echo
a3661c
		if test $(question_bool "Would you like to kill these apps?") = "true"; then
a3661c
			if test -w /etc/pulse/client.conf ; then
a3661c
				echo "autospawn = no # alsa-delay-line-to-be-removed-1234" >> /etc/pulse/client.conf
a3661c
			fi
a3661c
			for pid in $pids; do
a3661c
				do_cmd kill $pid
a3661c
			done
a3661c
			sleep 2
a3661c
			local killed=
a3661c
			for pid in $pids; do
a3661c
				local a=$(ps --no-headers -p $pids)
a3661c
				if test -n "$a"; then
a3661c
					do_cmd kill -9 $pid
a3661c
					local killed="true"
a3661c
				fi
a3661c
			done
a3661c
			if test "$killed" = "true"; then
a3661c
				sleep 2
a3661c
				for pid in $pids; do
a3661c
					local a=$(ps --no-headers -p $pids)
a3661c
					if test -n "$a"; then
a3661c
						echo >&2 "Unable to kill application:"
a3661c
						echo >&2 "  $a"
a3661c
						safe_exit 1
a3661c
					fi
a3661c
				done
a3661c
			fi
a3661c
		else
a3661c
			echo >&2 "Cannot continue with running audio applications."
a3661c
			safe_exit 1
a3661c
		fi
a3661c
	fi
a3661c
}
a3661c
a3661c
# Echo the list of configured sound modules
a3661c
configured_modules() {
a3661c
	if test -z "$useprocfs"; then
a3661c
		cat $modprobeconf | grep -E "^alias snd-card-" | cut -d ' ' -f 3
a3661c
	else
a3661c
		cat /proc/asound/modules | colrm 1 3
a3661c
	fi
a3661c
}
a3661c
a3661c
# Echo the list of loaded sound modules
a3661c
current_modules() {
a3661c
	$lsmod_prg | cut -d ' ' -f 1 | grep -E "^(snd[_-])"
a3661c
}
a3661c
a3661c
# The loopback kernel driver detection
a3661c
aloop_present() {
a3661c
	if test -r /proc/asound/card0/id; then
a3661c
		local id=$(cat /proc/asound/card0/id)
a3661c
	else
a3661c
		local id=""
a3661c
	fi
a3661c
	if test "$id" = "Loopback"; then
a3661c
		echo "yes"
a3661c
	fi
a3661c
}
a3661c
a3661c
# Remove kernel modules, using two phases
a3661c
# $@ is module names
a3661c
my_rmmod() {
a3661c
	local phase2=
a3661c
	while test -n "$1"; do
a3661c
		if ! $rmmod_prg $1 2> /dev/null > /dev/null; then
a3661c
			local phase2="$phase2 $1"
a3661c
		else
a3661c
			echo "> rmmod $1"
a3661c
		fi
a3661c
		shift
a3661c
	done
a3661c
	for mod in $phase2; do
a3661c
		echo "> rmmod $mod"
a3661c
		if ! $rmmod_prg $mod ; then
a3661c
			echo >&2 "Unable to remove kernel module $mod."
a3661c
			safe_exit 1
a3661c
		fi
a3661c
	done
a3661c
}
a3661c
a3661c
# Reload kernel modules
a3661c
kernel_modules() {
a3661c
	kill_audio_apps
a3661c
	if test "$1" = "unload"; then
a3661c
		local present=$(aloop_present)
a3661c
		if test "$present" = "yes"; then
a3661c
			if ! $rmmod_prg snd-aloop; then
a3661c
				echo >&2 "Unable to remove kernel module snd-aloop."
a3661c
				safe_exit 1
a3661c
			fi
a3661c
		fi
a3661c
	fi
a3661c
	local curmods=$(current_modules)
a3661c
	local usermods=$(configured_modules)
a3661c
	my_rmmod $curmods
a3661c
	$modprobe_prg soundcore || safe_exit 1
a3661c
	if test "$1" = "load"; then
a3661c
		if ! $modprobe_prg snd-aloop; then
a3661c
			echo >&2 "Unable to install kernel module snd-aloop."
a3661c
			safe_exit 1
a3661c
		fi
a3661c
	fi
a3661c
	for mod in $usermods; do
a3661c
		if ! $modprobe_prg $mod; then
a3661c
			echo >&2 "Unable to install kernel module $mod."
a3661c
			safe_exit 1
a3661c
		fi
a3661c
	done
a3661c
	echo "Kernel modules ready:"
a3661c
	cat /proc/asound/cards
a3661c
	sleep 0.5
a3661c
}
a3661c
a3661c
# If $package is alsa-driver then remove current modules
a3661c
kernel_modules_remove() {
a3661c
	local curmods=$(current_modules)
a3661c
	if test -z "$curmods"; then
a3661c
		echo "No ALSA kernel modules to remove."
a3661c
		safe_exit 0
a3661c
	fi
a3661c
	kill_audio_apps
a3661c
	my_rmmod $curmods
a3661c
	echo "ALSA kernel modules removed."
a3661c
}
a3661c
a3661c
function clean() {
a3661c
	echo -n "Removing tree $tmpdir:"
a3661c
	if test -d "$tmpdir"; then
a3661c
		if ! rm -rf "$tmpdir"; then
a3661c
			echo " failed"
a3661c
			safe_exit 1
a3661c
		fi
a3661c
	fi
a3661c
	echo " success"
a3661c
}
a3661c
a3661c
function reindex_modprobe_conf() {
a3661c
	if test -z "$useprocfs"; then
a3661c
		cat > $tmpdir/run.awk <
a3661c
function rewrite_line(line,   res, c, i, j) {
a3661c
	split(line, l, " ")
a3661c
	if (l[1] == "alias") {
a3661c
		printf(l[1])
a3661c
		c = int(substr(l[2], 9)) + 1
a3661c
		printf(" snd-card-%i %s\n", c, l[3])
a3661c
	} else if (l[1] == "options") {
a3661c
		printf(l[1])
a3661c
		for (i = 2; i <= length(l); i++) {
a3661c
			c = substr(l[i], 0, 9)
a3661c
			if (c == "snd-card-") {
a3661c
				c = int(substr(l[i], 9)) + 1
a3661c
				printf(" snd-card-%i", c)
a3661c
				continue
a3661c
			}
a3661c
			c = substr(l[i], 0, 6)
a3661c
			if (c == "index=") {
a3661c
				printf(" index=")
a3661c
				c = substr(l[i], 6)
a3661c
				split(c, y, ",")
a3661c
				for (j = 1; j <= length(y); j++) {
a3661c
					c = int(y[j]) + 1
a3661c
					if (j > 1)
a3661c
						printf(",")
a3661c
					printf("%i", c)
a3661c
				}
a3661c
				continue
a3661c
			}
a3661c
			printf(" %s", l[i])
a3661c
		}
a3661c
		printf("\n")
a3661c
	} else if (l[1] == "remove") {
a3661c
		printf(l[1])
a3661c
		flag = 0
a3661c
		for (i = 2; i <= length(l); i++) {
a3661c
			if (l[i] == "/usr/sbin/alsactl" || l[i] == "store") {
a3661c
				flag++;
a3661c
				printf(" %s", l[i]);
a3661c
				continue;
a3661c
			}
a3661c
			if (flag == 2) {
a3661c
				c = int(l[i]) + 1;
a3661c
				printf(" %i", c);
a3661c
				flag = 0;
a3661c
				continue;
a3661c
			}
a3661c
			flag = 0;
a3661c
			printf(" %s", l[i]);
a3661c
		}
a3661c
		printf("\n")
a3661c
	} else {
a3661c
		print line
a3661c
	}
a3661c
}
a3661c
a3661c
BEGIN			{ aloop=0; }
a3661c
/alias snd-card-0 snd-aloop/ { aloop=1; next; }
a3661c
/options snd-card-0 index=0/ { if (aloop) next; }
a3661c
/options snd-aloop/	{ next; }
a3661c
/alias snd-card-/	{ rewrite_line(\$0); next; }
a3661c
/options snd-/		{ rewrite_line(\$0); next; }
a3661c
/remove snd-/		{ rewrite_line(\$0); next; }
a3661c
			{ print \$0; }
a3661c
EOF
a3661c
		cat $modprobeconf | awk -f $tmpdir/run.awk > $modprobeconf.new
a3661c
		rm $tmpdir/run.awk || safe_exit 1
a3661c
	else
a3661c
		local index=1
a3661c
		echo -n "" > $modprobeconf.new
a3661c
		declare -a modules printed
a3661c
		for mod in $(cat /proc/asound/modules | grep -Ev "^2[6789]" | colrm 1 3); do
a3661c
			if test "$mod" != "snd_aloop" -a "$mod" != "snd-aloop"; then
a3661c
				if test -z ${modules[$mod]}; then
a3661c
					modules[$mod]=$index
a3661c
				else
a3661c
					modules[$mod]="${modules[mod]},$index"
a3661c
				fi
a3661c
				echo "alias snd-card-$index $mod" >> $modprobeconf.new
a3661c
				echo "options snd-card-$index index=$index" >> $modprobeconf.new
a3661c
			fi
a3661c
			index=$(expr $index + 1)
a3661c
		done
a3661c
		for mod in $(cat /proc/asound/modules | grep -Ev "^2[6789]" | colrm 1 3); do
a3661c
			if test "$mod" != "snd_aloop" -a "$mod" != "snd-aloop"; then
a3661c
				if test -z ${printed[$mod]}; then
a3661c
					echo "options $mod index=${modules[$mod]}" >> $modprobeconf.new
a3661c
					printed[$mod]=yes
a3661c
				fi
a3661c
			fi
a3661c
		done
a3661c
	fi
a3661c
cat >> $modprobeconf.new <
a3661c
alias snd-card-0 snd-aloop
a3661c
options snd-card-0 index=0
a3661c
options snd-aloop index=0
a3661c
EOF
a3661c
	if ! test -f $modprobeconf.alsa-delay.save ; then
a3661c
		if ! test -f $modprobeconf ; then
a3661c
			touch $modprobeconf.alsa-delay.save
a3661c
		else
a3661c
			cp $modprobeconf $modprobeconf.alsa-delay.save
a3661c
		fi
a3661c
	fi
a3661c
	mv $modprobeconf.new $modprobeconf || safe_exit 1
a3661c
}
a3661c
a3661c
function modify_modprobe_conf() {
a3661c
	local check=$(cat $modprobeconf 2> /dev/null | grep -E "^alias snd-card-0 snd-aloop")
a3661c
	local present=$(aloop_present)
a3661c
	if test -n "$check"; then
a3661c
		echo "Module snd-aloop is already installed."
a3661c
		if ! test "$present" = "yes"; then
a3661c
			kernel_modules load
a3661c
		fi
a3661c
	else
a3661c
		reindex_modprobe_conf
a3661c
		if ! test "$present" = "yes"; then
a3661c
			kernel_modules load
a3661c
		else
a3661c
			kernel_modules
a3661c
		fi
a3661c
	fi
a3661c
}
a3661c
a3661c
function myprintf() {
a3661c
	local cnt=$(echo "$1" | grep -o "%" | wc -l)
a3661c
	if test $cnt -eq 1; then
a3661c
		printf "$1" "$2"
a3661c
	else if test $cnt -eq 2; then
a3661c
		printf "$1" "$2" "$3"
a3661c
	else
a3661c
		printf "$1" "$2" "$3" "$4"
a3661c
	fi
a3661c
	fi
a3661c
}
a3661c
a3661c
function check_ctl_name() {
a3661c
	$amixer_prg -D "$1" contents | grep "name='$2'"
a3661c
}
a3661c
a3661c
function check_oss_mixer() {
a3661c
	grep ": mixer" /proc/asound/oss/devices
a3661c
}
a3661c
a3661c
function get_card_id() {
a3661c
	$amixer_prg -D "$1" info | head -1 | cut -d '/' -f 1 | cut -d ' ' -f 3- | awk '{ print substr($0, 2, length($0)-2) }'
a3661c
}
a3661c
a3661c
function generate_alsaloop_line() {
a3661c
	local file="$1"
a3661c
	local idx="$2"
a3661c
	local carg=$(myprintf "$cdevice" $ccard $cdev $idx)
a3661c
	local parg=$(myprintf "$pdevice" $pcard $pdev $idx)
a3661c
	local res="-C $carg -P $parg -T $idx -t $delay"
a3661c
	if test -n "$cctl"; then
a3661c
		local carg=$(myprintf "$cctl" $pcard $cdev $idx)
a3661c
		local res="$res -Y $carg"
a3661c
	fi
a3661c
	if test -n "$pctl"; then
a3661c
		local parg=$(myprintf "$pctl" $pcard $pdev $idx)
a3661c
		local res="$res -X $parg"
a3661c
	fi
a3661c
	if test -n "$arg"; then
a3661c
		local res="$res $arg"
a3661c
	fi
a3661c
	if test $idx -eq 7; then
a3661c
		if test -n "$one"; then
a3661c
			local res="$res $one"
a3661c
		fi
a3661c
		if test -z "$pctl"; then
a3661c
			local res="$res -X hw:$pcard"
a3661c
		fi
a3661c
		local mymix="$mix"
a3661c
		if test -z "$mymix"; then
a3661c
			local mymix="Master"
a3661c
			for mymix in Master "Master Mono" Headphone Headphone2 PCM Speaker \
a3661c
				     "Desktop Speaker" Beep Front Rear Center LFE Side \
a3661c
				     Surround ; do
a3661c
				local check=$(check_ctl_name hw:$pcard "$mymix Playback Switch")
a3661c
				if test -n "$check"; then
a3661c
					local res="$res -m \"name='$mymix Playback Switch'\""
a3661c
				fi
a3661c
				local check=$(check_ctl_name hw:$pcard "$mymix Playback Volume")
a3661c
				if test -n "$check"; then
a3661c
					local res="$res -m \"name='$mymix Playback Volume'\""
a3661c
				fi
a3661c
			done
a3661c
		else
a3661c
			local check=$(check_ctl_name hw:$pcard "$mymix Playback Switch")
a3661c
			if test -n "$check"; then
a3661c
				local res="$res -m \"name='$mymix Playback Switch'\""
a3661c
			fi
a3661c
			local check=$(check_ctl_name hw:$pcard "$mymix Playback Volume")
a3661c
			if test -n "$check"; then
a3661c
				local res="$res -m \"name='$mymix Playback Volume'\""
a3661c
			fi
a3661c
		fi
a3661c
		local check=$(check_oss_mixer)
a3661c
		if test -n "$check"; then
a3661c
			local res="$res -O \"$mymix@VOLUME\""
a3661c
		fi
a3661c
	fi
a3661c
	echo $res >> $file
a3661c
}
a3661c
a3661c
function generate_alsaloop_conf() {
a3661c
	local idx=0
a3661c
	rm -f $alsaloopconf.new
a3661c
	while test $idx -lt 8; do
a3661c
		generate_alsaloop_line $alsaloopconf.new $idx
a3661c
		idx=$(expr $idx + 1)
a3661c
	done
a3661c
	mv $alsaloopconf.new $alsaloopconf || safe_exit 1
a3661c
}
a3661c
a3661c
function kill_alsaloop() {
a3661c
	local pid=$($pidof_prg alsaloop)
a3661c
	if test -n "$pid"; then
a3661c
		echo "Killing alsaloop..."
a3661c
	fi
a3661c
	while test -n "$pid"; do
a3661c
		kill $pid
a3661c
		sleep 0.01
a3661c
		pid=$($pidof_prg alsaloop)
a3661c
	done
a3661c
}
a3661c
a3661c
function restart_alsaloop() {
a3661c
	echo "Restarting alsaloop: delay $delay us."
a3661c
	if test -d /etc/systemd/system; then
a3661c
		systemctl start alsaloop.service
a3661c
	else
a3661c
		/etc/init.d/alsaloop start
a3661c
	fi
a3661c
	sleep 0.4
a3661c
	local pid=$($pidof_prg alsaloop)
a3661c
	if test -z "$pid"; then
a3661c
		echo "ERROR: Not started, check /var/log/messages for details"
a3661c
	else
a3661c
		if test -d /etc/systemd/system; then
a3661c
			systemctl status alsaloop.service
a3661c
		else
a3661c
			ps -p $pid
a3661c
		fi
a3661c
	fi
a3661c
}
a3661c
a3661c
function create_systemd_service() {
a3661c
	if test -r /etc/systemd/system/alsaloop.service; then
a3661c
		echo "/etc/systemd/system/alsaloop.service exists"
a3661c
	else
a3661c
		cat >> /etc/systemd/system/alsaloop.service <
a3661c
# This file was created by the alsa-delay utility
a3661c
a3661c
[Unit]
a3661c
Description=The alsaloop daemon service
a3661c
After=alsa-restore.service
a3661c
Before=shutdown.target
a3661c
Conflicts=shutdown.target
a3661c
a3661c
[Service]
a3661c
Type=simple
a3661c
ExecPre=-/sbin/modprobe snd-aloop
a3661c
ExecStart=-/usr/bin/alsaloop --syslog --config $alsaloopconf
a3661c
EOF
a3661c
		ln -sf ../alsaloop.service /etc/systemd/system/basic.target.wants/alsaloop.service
a3661c
	fi
a3661c
}
a3661c
a3661c
function create_initd() {
a3661c
	if test -x /etc/init.d/alsaloop; then
a3661c
		echo "/etc/init.d/alsaloop exists"
a3661c
	else
a3661c
		cat >> /etc/init.d/alsaloop <
a3661c
#! /bin/sh
a3661c
a3661c
# alsaloop init file
a3661c
# Copyright (C) 2010 Jaroslav Kysela <jkysela@redhat.com>
a3661c
a3661c
# For RedHat and cousins:
a3661c
# chkconfig: 2345 99 01
a3661c
# description: Start alsaloop daemon
a3661c
# processname: alsaloop
a3661c
a3661c
### BEGIN INIT INFO
a3661c
# Provides: alsaloop
a3661c
# Required-Start: \$local_fs
a3661c
# Required-Stop: \$local_fs
a3661c
# Should-Start:
a3661c
# Short-Description: ALSALOOP
a3661c
# Description: ALSALOOP
a3661c
### END INIT INFO
a3661c
a3661c
# This program is free software; you can redistribute it and/or modify it
a3661c
# under the terms of the GNU General Public License as published by the Free
a3661c
# Software Foundation; either version 2, or (at your option) any later
a3661c
# version.
a3661c
# You should have received a copy of the GNU General Public License (for
a3661c
# example COPYING); if not, write to the Free Software Foundation, Inc., 675
a3661c
# Mass Ave, Cambridge, MA 02139, USA.
a3661c
# This code was originally developed as a Senior Thesis by Michael Cornwell
a3661c
# at the Concurrent Systems Laboratory (now part of the Storage Systems
a3661c
# Research Center), Jack Baskin School of Engineering, University of
a3661c
# California, Santa Cruz. http://ssrc.soe.ucsc.edu/.
a3661c
a3661c
alsaloop_opts="--daemonize --workaround serialopen --config $alsaloopconf"
a3661c
a3661c
ALSALOOP_BIN=/usr/bin/alsaloop
a3661c
test -x /usr/local/bin/alsaloop && ALSALOOP_BIN=/usr/local/bin/alsaloop
a3661c
a3661c
# Source function library
a3661c
. /etc/rc.d/init.d/functions
a3661c
a3661c
RETVAL=0
a3661c
prog=alsaloop
a3661c
pidfile=/var/lock/subsys/alsaloop
a3661c
a3661c
start()
a3661c
{
a3661c
	modprobe snd-aloop
a3661c
	echo -n \$"Starting \$prog: "
a3661c
	daemon \$ALSALOOP_BIN \$alsaloop_opts
a3661c
	RETVAL=\$?
a3661c
	echo
a3661c
	[ \$RETVAL = 0 ] && touch \$pidfile
a3661c
	return \$RETVAL
a3661c
}
a3661c
a3661c
stop()
a3661c
{
a3661c
	echo -n \$"Shutting down \$prog: "
a3661c
	killproc \$ALSALOOP_BIN
a3661c
	RETVAL=\$?
a3661c
	echo
a3661c
	rm -f \$pidfile
a3661c
	return \$RETVAL
a3661c
}
a3661c
a3661c
case "\$1" in
a3661c
	start)
a3661c
		start
a3661c
		;;
a3661c
	stop)
a3661c
		stop
a3661c
		;;
a3661c
	restart)
a3661c
		stop
a3661c
		start
a3661c
		;;
a3661c
	try-restart)
a3661c
		if [ -f \$pidfile ]; then
a3661c
			stop
a3661c
			start
a3661c
		fi
a3661c
		;;
a3661c
	status)
a3661c
		status $prog
a3661c
		RETVAL=$?
a3661c
		;;
a3661c
	*)
a3661c
		echo \$"Usage: \$0 {start|stop|restart|try-restart}"
a3661c
		RETVAL=3
a3661c
esac
a3661c
a3661c
exit \$RETVAL
a3661c
EOF
a3661c
		chmod 755 /etc/init.d/alsaloop || safe_exit 1
a3661c
		$chkconfig_prg --add alsaloop
a3661c
		$chkconfig_prg alsaloop on
a3661c
	fi
a3661c
}
a3661c
a3661c
function modify_pa_conf() {
a3661c
a3661c
	if grep Loopback /etc/pulse/default.pa > /dev/null; then
a3661c
		echo "/etc/pulse/default.pa already modified"
a3661c
	else
a3661c
		if test -f /etc/pulse/default.pa; then
a3661c
			local pcardid=$(get_card_id "hw:$pcard")
a3661c
			if ! test -f /etc/pulse/default.pa.alsa-delay.save ; then
a3661c
				cp /etc/pulse/default.pa /etc/pulse/default.pa.alsa-delay.save
a3661c
			fi
a3661c
			cat /etc/pulse/default.pa | \
a3661c
				sed -e 's/^load-module module-udev-detect/#load-module module-udev-detect # commented by alsa-delay/' \
a3661c
				    -e 's/^load-module module-detect/#load-module module-detect # commented by alsa-delay/' \
a3661c
				    -e "s/^### Automatically load driver modules for Bluetooth hardware/load-module module-alsa-card device_id=Loopback\nload-module module-alsa-card device_id=$pcardid\n\n### Automatically load driver modules for Bluetooth hardware/" > /etc/pulse/default.pa.new
a3661c
			if test -s /etc/pulse/default.pa.new; then
a3661c
				mv /etc/pulse/default.pa.new /etc/pulse/default.pa
a3661c
				echo "/etc/pulse/default.pa changed"
a3661c
			fi
a3661c
		fi
a3661c
	fi
a3661c
a3661c
	local file=
a3661c
	local asoundconf=
a3661c
	if test -d /etc/alsa -a -f /etc/alsa/pulse-default.conf; then
a3661c
		file=/etc/alsa/alsaloop-default.conf
a3661c
		asoundconf=yes
a3661c
	fi
a3661c
	if test -f /usr/share/alsa/alsa.conf.d/99-pulseaudio-default.conf ; then
a3661c
		file=/usr/share/alsa/alsa.conf.d/A0-alsaloop-default.conf
a3661c
	fi
a3661c
	if test -z "$file"; then
a3661c
		echo "unknown PulseAudio setup (asound.conf)"
a3661c
	elif test -f $file; then
a3661c
		echo "$file exists"
a3661c
	else
a3661c
		cat >> $file <
a3661c
pcm.!default {
a3661c
	@args [ CARD ]
a3661c
	@args.CARD {
a3661c
		type string
a3661c
	}
a3661c
	type asym
a3661c
	playback.pcm "plughw:Loopback"
a3661c
	capture.pcm {
a3661c
		type pulse
a3661c
	}
a3661c
}
a3661c
EOF
a3661c
		if test -n "$asoundconf"; then
a3661c
			cat /etc/asound.conf | sed 's/pulse-default.conf/alsaloop-default.conf/g' > /etc/asound.conf.new
a3661c
			if test -s /etc/asound.conf.new; then
a3661c
				mv /etc/asound.conf.new /etc/asound.conf
a3661c
			fi
a3661c
		fi
a3661c
	fi
a3661c
}
a3661c
a3661c
function remove() {
a3661c
	if test -f /etc/pulse/default.pa.alsa-delay.save ; then
a3661c
		echo "Restoring /etc/pulse/default.pa"
a3661c
		mv /etc/pulse/default.pa.alsa-delay.save /etc/pulse/default.pa
a3661c
	fi
a3661c
	if test -f /etc/init.d/alsaloop ; then
a3661c
		echo "Removing /etc/init.d/alsaloop service"
a3661c
		$chkconfig_prg alsaloop off
a3661c
		$chkconfig_prg --del alsaloop
a3661c
		rm /etc/init.d/alsaloop
a3661c
	fi
a3661c
	if test -f /etc/systemd/system/alsaloop.service ; then
a3661c
		echo "Removing /etc/systemd/system/alsaloop.service"
a3661c
		$systemctl_prg disable alsaloop.service
a3661c
		rm /etc/systemd/system/alsaloop.service
a3661c
	fi
a3661c
	if test -f $alsaloopconf ; then
a3661c
		echo "Removing $alsaloopconf"
a3661c
		rm $alsaloopconf
a3661c
	fi
a3661c
	if test -f /etc/alsa/alsaloop-default.conf ; then
a3661c
		echo "Removing /etc/alsa/alsaloop-default.conf"
a3661c
		rm /etc/alsa/alsaloop-default.conf
a3661c
	fi
a3661c
	if test -f /usr/share/alsa/alsa.conf.d/A0-alsaloop-default.conf ; then
a3661c
		echo "Removing /usr/share/alsa/alsa.conf.d/A0-alsaloop-default.conf"
a3661c
		rm /usr/share/alsa/alsa.conf.d/A0-alsaloop-default.conf
a3661c
	fi
a3661c
	if grep "alsaloop-default.conf" /etc/asound.conf > /dev/null 2> /dev/null ; then
a3661c
		echo "Modifying /etc/asound.conf"
a3661c
		cat /etc/asound.conf | sed 's/alsaloop-default.conf/pulse-default.conf/g' > /etc/asound.conf.new
a3661c
		if test -s /etc/asound.conf.new; then
a3661c
			mv /etc/asound.conf.new /etc/asound.conf
a3661c
		fi
a3661c
	fi
a3661c
	if test -f $modprobeconf.alsa-delay.save ; then
a3661c
		echo "Restoring $modprobeconf"
a3661c
		if test -s $modprobeconf.alsa-delay.save ; then
a3661c
			mv $modprobeconf.alsa-delay.save $modprobeconf
a3661c
		else
a3661c
			rm $modprobeconf.alsa-delay.save
a3661c
			rm $modprobeconf
a3661c
		fi
a3661c
	fi
a3661c
	if test $(aloop_present) = "yes"; then
a3661c
		kernel_modules unload
a3661c
	fi
a3661c
}
a3661c
a3661c
rundir=$(pwd)
a3661c
export LC_ALL=C
a3661c
export LANGUAGE=C
a3661c
a3661c
check_environment
a3661c
a3661c
if test -n "$clean"; then
a3661c
	clean
a3661c
	safe_exit 0
a3661c
fi
a3661c
a3661c
if test -n "$remove"; then
a3661c
	kill_alsaloop
a3661c
	remove
a3661c
	clean
a3661c
	safe_exit 0
a3661c
fi
a3661c
a3661c
kill_alsaloop
a3661c
if test -d /etc/systemd/system ; then
a3661c
  create_systemd_service
a3661c
else
a3661c
  create_initd
a3661c
fi
a3661c
modify_modprobe_conf
a3661c
generate_alsaloop_conf
a3661c
if test -n "$pa"; then
a3661c
  modify_pa_conf
a3661c
fi
a3661c
restart_alsaloop
a3661c
a3661c
clean
a3661c
a3661c
safe_exit 0