From 00b7536f5edfd3febd63e48278146f9b93809b77 Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Sun, 21 Nov 2021 16:58:56 +0100 Subject: [PATCH] Setup pulse audio for remote audio --- tasks/mpd.yml | 68 +++---------------- tasks/pipewire.yml | 33 +++++++-- templates/desktop/nftables.j2 | 6 +- templates/desktop/pulse-script.j2 | 5 ++ templates/laptop/pulse-script.j2 | 5 ++ templates/laptop/pulse.j2 | 8 +++ templates/mpd/mpd.j2 | 44 ------------ templates/mpd/music_mount.j2 | 5 -- templates/mpd/service.j2 | 14 ---- templates/mpd/socket.j2 | 11 --- .../music_umount.j2 => pipewire-pulse.j2} | 6 +- templates/sudoers.j2 | 4 -- 12 files changed, 62 insertions(+), 147 deletions(-) create mode 100644 templates/desktop/pulse-script.j2 create mode 100644 templates/laptop/pulse-script.j2 create mode 100644 templates/laptop/pulse.j2 delete mode 100644 templates/mpd/mpd.j2 delete mode 100644 templates/mpd/music_mount.j2 delete mode 100644 templates/mpd/service.j2 delete mode 100644 templates/mpd/socket.j2 rename templates/{mpd/music_umount.j2 => pipewire-pulse.j2} (58%) delete mode 100644 templates/sudoers.j2 diff --git a/tasks/mpd.yml b/tasks/mpd.yml index 9a24f3e..53217f3 100644 --- a/tasks/mpd.yml +++ b/tasks/mpd.yml @@ -1,64 +1,14 @@ -- name: set up sudoers configuration - become: yes - template: - src: 'templates/sudoers.j2' - dest: '/etc/sudoers.d/10-sonny' - owner: root - group: root - mode: '0644' +# remote mpd server is used now -- name: copy systemd configuration files - template: - src: '{{ item.src }}' - dest: '{{ item.dest }}' - loop: - - { - src: 'templates/mpd/service.j2', - dest: '{{ xdg_config_dir }}/systemd/user/mpd.service', - } - - { - src: 'templates/mpd/socket.j2', - dest: '{{ xdg_config_dir }}/systemd/user/mpd.socket', - } +- name: check for mpd socket + stat: + path: '{{ xdg_config_dir }}/systemd/user/mpd.socket' + register: mpd_socket -- name: create mpd files - file: - path: '{{ item.path }}' - state: '{{ item.state }}' - loop: - - { path: '{{ mpd_configuration_dir }}', state: 'directory' } - - { path: '{{ mpd_configuration_dir }}/playlists', state: 'directory' } - - { path: '{{ mpd_configuration_dir }}/log', state: 'touch' } - - { path: '{{ mpd_configuration_dir }}/database', state: 'touch' } - - { path: '{{ mpd_configuration_dir }}/sticker.sql', state: 'touch' } - - { path: '{{ mpd_configuration_dir }}/state', state: 'touch' } - -- name: copy configuration file - template: - src: 'templates/mpd/mpd.j2' - dest: '{{ mpd_configuration_dir }}/mpd.conf' - -- name: copy nfs connection scripts - template: - src: '{{ item.src }}' - dest: '{{ item.dest }}' - mode: '0755' - loop: - - { src: 'templates/mpd/music_mount.j2', dest: '{{ xdg_script_dir }}/music_mount' } - - { src: 'templates/mpd/music_umount.j2', dest: '{{ xdg_script_dir }}/music_umount' } - -- name: restart mpd service - systemd: - name: mpd.service - state: restarted - enabled: no - scope: user - when: platform == "desktop" - -- name: enable mpd socket +- name: disable mpd socket systemd: name: mpd.socket - state: started - enabled: yes + state: stopped + enabled: no scope: user - when: platform == "desktop" + when: mpd_socket.stat.exists diff --git a/tasks/pipewire.yml b/tasks/pipewire.yml index 8f4cc53..1a7e7f7 100644 --- a/tasks/pipewire.yml +++ b/tasks/pipewire.yml @@ -1,4 +1,23 @@ -# Note that pulsaudio should be removed by install pipewire-pulse manually +# Note that pulsaudio should be removed by installing pipewire-pulse manually + +- name: create pipewire-pulse override directory + file: + path: '{{ xdg_config_dir }}/systemd/user/pipewire-pulse.service.d/' + state: directory + +# sets up an post activation script to load the module-native-protocol-tcp +# with given parameters. This is not yet supported through pipewire-pulse's configuration, +# see https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Migrate-PulseAudio#module-native-protocol-tcp +- name: copy pipewire-pulse service override + template: + src: 'templates/pipewire-pulse.j2' + dest: '{{ xdg_config_dir }}/systemd/user/pipewire-pulse.service.d/override.conf' + +- name: copy pipewire-pulse script + template: + src: 'templates/{{ platform }}/pulse-script.j2' + dest: '{{ xdg_script_dir }}/pulse-script' + mode: 0755 - name: start pipewire socket systemd: @@ -7,9 +26,13 @@ enabled: true scope: user -- name: start pipewire-pulse socket +- name: restart pipewire-pulse systemd: - name: pipewire-pulse.socket - state: started - enabled: true + name: '{{ item.name }}' + state: restarted + enabled: '{{ item.enabled }}' scope: user + daemon-reload: true + loop: + - { name: 'pipewire-pulse.socket', enabled: true } + - { name: 'pipewire-pulse.service', enabled: false } diff --git a/templates/desktop/nftables.j2 b/templates/desktop/nftables.j2 index 5d3e23f..3adbb58 100644 --- a/templates/desktop/nftables.j2 +++ b/templates/desktop/nftables.j2 @@ -1,8 +1,9 @@ # {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} # -#!/usr/bin/nft -f # vim:set ts=2 sw=2 et: +flush ruleset + table inet filter { chain input { type filter hook input priority 0; @@ -26,6 +27,9 @@ table inet filter { # syncthing ip saddr 10.8.1.1 tcp dport 22000 accept + # allow remote pulse audio + ip saddr 10.8.1.1 tcp dport 4713 accept + # allow dhcp requests for bridget connections iifname "vmbr0" udp dport { 53, 67 } accept diff --git a/templates/desktop/pulse-script.j2 b/templates/desktop/pulse-script.j2 new file mode 100644 index 0000000..56cd152 --- /dev/null +++ b/templates/desktop/pulse-script.j2 @@ -0,0 +1,5 @@ +#!/usr/bin/sh +# +# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} + +/usr/bin/pactl load-module module-native-protocol-tcp auth-anonymous=1 listen=10.8.1.10 diff --git a/templates/laptop/pulse-script.j2 b/templates/laptop/pulse-script.j2 new file mode 100644 index 0000000..5e3b3aa --- /dev/null +++ b/templates/laptop/pulse-script.j2 @@ -0,0 +1,5 @@ +#!/usr/bin/sh +# +# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} + +/usr/bin/pactl load-module module-native-protocol-tcp auth-anonymous=1 listen=10.8.1.6 diff --git a/templates/laptop/pulse.j2 b/templates/laptop/pulse.j2 new file mode 100644 index 0000000..3ae1387 --- /dev/null +++ b/templates/laptop/pulse.j2 @@ -0,0 +1,8 @@ +# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} +# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} +# +# see /usr/share/pipewire/pipewire-pulse.conf for reference + +context.exec = [ + { path = "pactl" args = "module-native-protocol-tcp auth-anonymous=1 listen=10.8.1.6" } +] diff --git a/templates/mpd/mpd.j2 b/templates/mpd/mpd.j2 deleted file mode 100644 index 24ec414..0000000 --- a/templates/mpd/mpd.j2 +++ /dev/null @@ -1,44 +0,0 @@ -# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} - -music_directory "{{ mpd_music_dir }}" -playlist_directory "{{ mpd_playlist_dir }}" -state_file "{{ mpd_state_path }}" -sticker_file "{{ mpd_sticker_path }}" -log_level "secure" - -bind_to_address "{{ mpd_listen_address }}" -port "{{ mpd_listen_port }}" - -auto_update "yes" -filesystem_charset "UTF-8" - -samplerate_converter "1" - -database { - plugin "proxy" - host "{{ mpd_database_address }}" - port "{{ mpd_database_port }}" -} - -audio_output { - type "pulse" - name "mpd" - replay_gain_handler "software" - mixer_type "hardware" - format "96000:24:1" -} - -input { - enabled "no" - plugin "tidal" -} - -input { - enabled "no" - plugin "qobuz" -} - -decoder { - plugin "wildmidi" - enabled "no" -} diff --git a/templates/mpd/music_mount.j2 b/templates/mpd/music_mount.j2 deleted file mode 100644 index 615712b..0000000 --- a/templates/mpd/music_mount.j2 +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# -# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} - -sudo mount -t nfs -o vers=4,soft,async,proto=tcp,port=2049 10.8.0.1:/srv/nfs4/music {{ mpd_music_dir }} diff --git a/templates/mpd/service.j2 b/templates/mpd/service.j2 deleted file mode 100644 index dd79222..0000000 --- a/templates/mpd/service.j2 +++ /dev/null @@ -1,14 +0,0 @@ -# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} - -[Unit] -Description=Music Player Daemon -Documentation=man:mpd(1) man:mpd.conf(5) - -[Service] -Type=notify -ExecStartPre={{ xdg_script_dir }}/music_mount -ExecStart=/usr/bin/mpd --no-daemon {{ mpd_configuration_dir }}/mpd.conf -ExecStopPost={{ xdg_script_dir }}/music_umount -Restart=on-failure -RestartSec=15s -TimeoutStopSec=3 diff --git a/templates/mpd/socket.j2 b/templates/mpd/socket.j2 deleted file mode 100644 index 7188f2c..0000000 --- a/templates/mpd/socket.j2 +++ /dev/null @@ -1,11 +0,0 @@ -# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} - -[Socket] -ListenStream=/run/user/1000/mpd.socket -ListenStream={{ mpd_listen_port }} -Backlog=5 -KeepAlive=true -PassCredentials=true - -[Install] -WantedBy=sockets.target diff --git a/templates/mpd/music_umount.j2 b/templates/pipewire-pulse.j2 similarity index 58% rename from templates/mpd/music_umount.j2 rename to templates/pipewire-pulse.j2 index 9bc55a2..a0aa782 100644 --- a/templates/mpd/music_umount.j2 +++ b/templates/pipewire-pulse.j2 @@ -1,6 +1,4 @@ -#!/bin/bash -# # {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} -pkill cantata -sudo umount {{ mpd_music_dir }} +[Service] +ExecStartPost={{ xdg_script_dir }}/pulse-script diff --git a/templates/sudoers.j2 b/templates/sudoers.j2 deleted file mode 100644 index 5d41d4c..0000000 --- a/templates/sudoers.j2 +++ /dev/null @@ -1,4 +0,0 @@ -# {{ ansible_managed }} {{ ansible_date_time.time }} {{ ansible_date_time.date }} - -sonny ALL=(ALL) NOPASSWD: /usr/bin/mount -sonny ALL=(ALL) NOPASSWD: /usr/bin/umount