forked from JJFourie/HomeAssistant-PulseAudio-Disable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pa-suspend.sh
executable file
·120 lines (102 loc) · 5.12 KB
/
pa-suspend.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
###############################################################################
# Name:
# pa-suspend (PulseAudio Suspend)
#
# Puspose:
# Change the PulseAudio run command, to replace verbose logging with error only logging. Then restart PulseAudio.
# Load the PulseAudio "module-suspend-on-idle" module in the Home Assistant "hassio_audio" container.
# This is done either when the script is started and the container is already running, or when the container is (re)started.
# This is done to prevent audio issues and reduce CPU usage in certain environments, caused by system-wide PulseAudio running in the hassio_audio container.
#
# Description:
# - When
# a) the script starts, but the "hassio_audio" container is already running.
# b) the script is already running, and then the "hassio_audio" container is (re)started.
# - What
# 1) update the PulseAudio run parameters in /run/s6/services/pulseaudio/run
# replace "-vvv" (verbose logging) with "--log-level=0" (log errors only).
# 2) Stop PulseAudio. It will automatically be restared using the new parameters.
# 3) load the PulseAudio "module-suspend-on-idle" module inside "hassio_audio".
# Continue to monitor Docker Events for "hassio_audio" container start events.
# The script start- and module load events are reported to rsyslog as User events.
#
# Execution
# - Shell script: ./pa-suspend.sh
#
# Version
# v1.1 - Changed hassio_audio logging from verbose to error-only.
# v1.2 - Fixed bug in Docker exec commands, expanded error checking.
# v1.3 - Added boolean settings to enable/disable (1) change run params (2) load PA module, (3) set null devices as defaults (sink and input)
#
###############################################################################
me=`basename "$0"`
RETVAL=0
event_filter="container=hassio_audio"
event_format="Container={{.Actor.Attributes.name}} Status={{.Status}}"
DO_CHANGE_PARAMS=true
DO_LOAD_MODULE=true
DO_SET_NULL_AS_DEFAULT=true
#------------------------------------------------------------------------------
# Function to change parameters and load PulseAudio module
#------------------------------------------------------------------------------
function change_pulseaudio () {
set -x
if [[ $DO_CHANGE_PARAMS = true ]]; then
# Change the PulseAudio run command: replace verbose logging with only logging errors. Then restart PulseAudio.
res=$(docker exec -i hassio_audio sed -i 's/-vvv/--log-level=0 --log-time=true/' /run/s6/services/pulseaudio/run 2>&1)
if [[ "${?}" -ne "0" ]]; then
logger -p user.err "${1}: Failed to change PA parameters in hassio_audio ($res)"
fi
# Restart PulseAudio.
res=$(docker exec -i hassio_audio pkill pulseaudio 2>&1)
if [[ "${?}" -ne "0" ]]; then
logger -p user.err "${1}: Failed to kill PulseAudio in hassio_audio ($res)"
fi
sleep 2 # Wait for pulse audio to restart
fi
if [[ $DO_LOAD_MODULE = true ]]; then
# Load the PulseAudio suspend module
res=$(docker exec -i hassio_audio pactl load-module module-suspend-on-idle 2>&1)
if [[ "${?}" == "0" ]]; then
logger -p user.notice "${1}: PulseAudio module-suspend-on-idle loaded ok ($res)"
else
logger -p user.err "${1}: PulseAudio module-suspend-on-idle failed to load! ($res)"
fi
else
logger -p user.notice "${1}: Not loading PulseAudio module-suspend-on-idle."
fi
if [[ $DO_SET_NULL_AS_DEFAULT = true ]]; then
logger -p user.notice "${1}: PulseAudio setting default sink and source to null"
docker exec -i hassio_audio pactl unload-module module-switch-on-connect
docker exec -i hassio_audio pactl unload-module module-switch-on-port-available
docker exec -i hassio_audio pactl load-module module-null-sink
#docker exec -i hassio_audio pactl set-default-sink alsa_output.pci-0000_07_00.1.hdmi-stereo-extra2
docker exec -i hassio_audio pactl set-default-sink null
docker exec -i hassio_audio pactl set-default-source null.monitor
set +x
fi
}
#------------------------------------------------------------------------------
# Function to wait forever and update container if hassio_audio is (re)started
#------------------------------------------------------------------------------
function event_loop () {
while read line; do
if [[ ${line} == *"Status=start" ]]; then
# Container started. Wait to allow container to initialize (else may get "connection refused" error).
sleep 5
change_pulseaudio "${me} (Container Start)"
fi
done
}
logger -p user.notice "${me}: Started"
# Check if hass_audio is already running, and addressable.
tmp=$(docker exec hassio_audio date 2>&1)
if [[ "${?}" == "0" ]]; then
change_pulseaudio "${me} (Script Start)"
else
logger -p user.warning "${me}: Script started - container hassio_audio not running."
fi
# Read the Container Events and pass to function loop to process.
docker events --filter ${event_filter} --format "${event_format}" | event_loop
exit $RETVAL