Blame SOURCES/alsa-delay

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