Skip to content

Commit

Permalink
AudioVM and client configuration with separate VM audio chennels
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Sahlberg <jon.sahlberg@unikie.com>
  • Loading branch information
josa41 committed Sep 6, 2024
1 parent ec9377d commit 0534839
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 68 deletions.
8 changes: 1 addition & 7 deletions modules/common/services/audio.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ in
# Enable TCP socket for VMs pulseaudio clients
"server.address" = [
{
address = "tcp:4713";
address = "tcp:${toString cfg.pulseaudioTcpPort}";
"client.access" = "unrestricted";
}
];
Expand All @@ -60,12 +60,6 @@ in
};
};

hardware.pulseaudio.extraConfig = ''
# Set sink and source default max volume to about 75% (0-65536)
set-sink-volume @DEFAULT_SINK@ 48000
set-source-volume @DEFAULT_SOURCE@ 48000
'';

# Allow ghaf user to access pulseaudio and pipewire
users.extraUsers.ghaf.extraGroups = [
"audio"
Expand Down
70 changes: 69 additions & 1 deletion modules/microvm/virtualization/microvm/appvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,64 @@ let
runWaypipe
pkgs.tpm2-tools
pkgs.opensc
];
pkgs.pipewire
]
++ (lib.optional (
config.ghaf.profiles.debug.enable )
pkgs.pulseaudio );

security.rtkit.enable = vm.ghafAudio;
users.extraUsers.ghaf.extraGroups =
lib.optionals vm.ghafAudio ["audio" "video"];

services.pipewire = lib.mkIf vm.ghafAudio {
enable = true;
pulse.enable = true;
extraConfig = {
pipewire."10-remote-audio-tunnel-source" = {
"context.modules" = [
{
name = "libpipewire-module-pulse-tunnel";
args = {
"tunnel.mode" = "source";
"pulse.server.address" = "tcp:audio-vm:4713";
"reconnect.interval.ms" = "1000";
"stream.props" = ''
"node.name" = "${vm.name}.mic"
"node.description" = "${vm.name} Microphone"
"media.icon" = "${pkgs.icon-pack}/${vm.name}.svg"
"node.autoconnect" = "true"
'';
};
}
];
};
pipewire."11-remote-audio-tunnel-sink" = {
"context.modules" = [
{
name = "libpipewire-module-pulse-tunnel";
args = {
"tunnel.mode" = "sink";
"pulse.server.address" = "tcp:audio-vm:4713";
"reconnect.interval.ms" = "1000";
"stream.props" = ''
"node.name" = "${vm.name}.speaker"
"node.description" = "${vm.name} Speaker"
"media.icon" = "${pkgs.icon-pack}/${vm.name}.svg"
"node.autoconnect" = "true"
'';
};
}
];
};
};
};

hardware.pulseaudio.extraConfig = ''
# Set default sink and source channels per VM
set-default-source "${vm.name}.mic"
set-default-sink "${vm.name}.speaker"
'';

security.tpm2 = {
enable = true;
Expand Down Expand Up @@ -237,6 +294,11 @@ in
type = types.nullOr types.str;
default = null;
};
ghafAudio = mkOption {
description = "Enable Ghaf VM Audio support";
type = types.bool;
default = false;
};
vtpm.enable = lib.mkEnableOption "vTPM support in the virtual machine";
};
}
Expand All @@ -252,6 +314,12 @@ in
default = [ ];
};

ghafAudio = mkOption {
description = "Enable Ghaf VM Audio support";
type = types.bool;
default = false;
};

# Base VSOCK CID which is used for auto assigning CIDs for all AppVMs
# For example, when it's set to 100, AppVMs will get 100, 101, 102, etc.
# It is also possible to override the auto assinged CID using the vms.cid option
Expand Down
1 change: 1 addition & 0 deletions modules/microvm/virtualization/microvm/guivm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ let
(rmDesktopEntries [
pkgs.waypipe
pkgs.networkmanagerapplet
pkgs.myxer
])
++ [
pkgs.nm-launcher
Expand Down
23 changes: 2 additions & 21 deletions modules/reference/appvms/business.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ in
in
[
pkgs.chromium
pkgs.pulseaudio
pkgs.xdg-utils
xdgPdfItem
xdgOpenPdf
Expand All @@ -46,26 +45,7 @@ in
cores = 4;
extraModules = [
{
imports = [ ../programs/chromium.nix ];
# Enable pulseaudio for Chromium VM
security.rtkit.enable = true;
users.extraUsers.ghaf.extraGroups = [
"audio"
"video"
];

hardware.pulseaudio = {
enable = true;
extraConfig = ''
load-module module-tunnel-sink sink_name=chromium-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000
load-module module-tunnel-source source_name=chromium-mic server=audio-vm:4713 format=s16le channels=1 rate=48000
# Set sink and source default max volume to about 90% (0-65536)
set-sink-volume chromium-speaker 60000
set-source-volume chromium-mic 60000
'';
};

imports = [../programs/chromium.nix];
time.timeZone = config.time.timeZone;

microvm = {
Expand Down Expand Up @@ -279,5 +259,6 @@ in
}
];
borderColor = "#00FF00";
ghafAudio = true;
vtpm.enable = true;
}
22 changes: 3 additions & 19 deletions modules/reference/appvms/chromium.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ in
in
[
pkgs.chromium
pkgs.pulseaudio
pkgs.xdg-utils
xdgPdfItem
xdgOpenPdf
Expand All @@ -43,26 +42,10 @@ in
cores = 4;
extraModules = [
{
imports = [ ../programs/chromium.nix ];
# Enable pulseaudio for Chromium VM
security.rtkit.enable = true;
users.extraUsers.ghaf.extraGroups = [
"audio"
"video"
imports = [
../programs/chromium.nix
];

hardware.pulseaudio = {
enable = true;
extraConfig = ''
load-module module-tunnel-sink sink_name=chromium-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000
load-module module-tunnel-source source_name=chromium-mic server=audio-vm:4713 format=s16le channels=1 rate=48000
# Set sink and source default max volume to about 90% (0-65536)
set-sink-volume chromium-speaker 60000
set-source-volume chromium-mic 60000
'';
};

time.timeZone = config.time.timeZone;

microvm.qemu.extraArgs = optionals (
Expand All @@ -83,5 +66,6 @@ in
}
];
borderColor = "#630505";
ghafAudio = true;
vtpm.enable = true;
}
21 changes: 1 addition & 20 deletions modules/reference/appvms/element.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,12 @@ in
pkgs.element-gps
pkgs.gpsd
pkgs.tcpdump
pkgs.pulseaudio
] ++ pkgs.lib.optionals isDendritePineconeEnabled [ dendrite-pinecone ];
macAddress = "02:00:00:03:09:01";
ramMb = 4096;
cores = 4;
extraModules = [
{
# Enable pulseaudio for user ghaf to access mic
security.rtkit.enable = true;
users.extraUsers.ghaf.extraGroups = [
"audio"
"video"
];

hardware.pulseaudio = {
enable = true;
extraConfig = ''
load-module module-tunnel-sink sink_name=element-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000
load-module module-tunnel-source source_name=element-mic server=audio-vm:4713 format=s16le channels=1 rate=48000
# Set sink and source default max volume to about 90% (0-65536)
set-sink-volume element-speaker 60000
set-source-volume element-mic 60000
'';
};

systemd = {
services = {
element-gps = {
Expand Down Expand Up @@ -101,4 +81,5 @@ in
}
];
borderColor = "#337aff";
ghafAudio = true;
}

0 comments on commit 0534839

Please sign in to comment.