~mterry/ubuntu-touch-session/pulse-in-snap

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
description "Pulseaudio"
author "Ricardo Mendoza <ricmm@ubuntu.com>"

start on started dbus
stop on session-end

respawn
expect daemon

pre-start script
    # Use the Touch specific Pulseaudio script file
    if [ -n "$SNAP" ]; then
        # We need to fix hardcoded paths to point into our snap
        if ! grep -q $SNAP $SNAP_USER_DATA/.local/share/pulse/touch.pa; then
            mkdir -p $SNAP_USER_DATA/.local/share/pulse
            sed "s|/etc/|$SNAP/etc/|g" $SNAP/etc/pulse/touch.pa >$SNAP_USER_DATA/.local/share/pulse/touch.pa
        fi
        initctl set-env --global PULSE_SCRIPT=$SNAP_USER_DATA/.local/share/pulse/touch.pa
    else
        initctl set-env --global PULSE_SCRIPT=/etc/pulse/touch.pa
    fi
    initctl set-env --global PULSE_CLIENTCONFIG=$SNAP/etc/pulse/touch-client.conf

    # Force a default audio role (apps can overwrite this)
    initctl set-env --global PULSE_PROP='media.role=multimedia'

    # Force server-side corking when stalled (playback)
    initctl set-env --global PULSE_PLAYBACK_CORK_STALLED=1

    # Pulseaudio enforces this variable internally, but tries to re-exec itself
    # when enabling it and has difficulty finding its own binary, if in a snap.
    # So let's set it for the daemon, so it won't have to.
    initctl set-env LD_BIND_NOW=1
end script

exec pulseaudio --start --log-target=syslog

# Upstart tracks fork() calls, but what Pulse does is fork early
# and then block on a write to a socket. So Upstart thinks that
# it has started while Pulse is still initializing. This stops
# the started event from happening until the dbus-socket is ready
# for people to connect to it.
post-start script
	while true; do
		[ -S /run/user/`id -u`/pulse/dbus-socket ] && break
		sleep 0.1
	done
end script