Skip to content

Commit

Permalink
qm sound sub-package: enable audio
Browse files Browse the repository at this point in the history
Enable audio in QM container and nested containers

Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Oct 7, 2024
1 parent 26cbbb5 commit f981a29
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ rpm: clean dist ## - Creates a local RPM package, useful for develop
--define="enable_qm_dropin_img_tempdir 0" \
--define="enable_qm_mount_bind_tty7 0" \
--define="enable_qm_mount_bind_ttyUSB0 0" \
--define="enable_qm_mount_bind_sound 0" \
--define="enable_qm_mount_bind_input 0" \
--define="_topdir ${RPM_TOPDIR}" \
--define="version ${VERSION}" \
Expand All @@ -85,14 +86,20 @@ qm_dropin_mount_bind_ttyUSB0: ## - Creates a QM RPM sub-package to mount bin
tools/version-update -v ${VERSION}
$(MAKE) VERSION=${VERSION} rpm

.PHONY: qm_dropin_mount_bind_sound
qm_dropin_mount_bind_sound: ## - Creates a QM RPM sub-package to mount bind /dev/snd in the nested containers
sed -i 's/%define enable_qm_mount_bind_sound 0/%define enable_qm_mount_bind_sound 1/' ${SPECFILE}
tools/version-update -v ${VERSION}
$(MAKE) VERSION=${VERSION} rpm

.PHONY: qm_dropin_mount_bind_tty7
qm_dropin_mount_bind_tty7: ## - Creates a QM RPM sub-package to mount bind /dev/tty7 in the nested containers
sed -i 's/%define enable_qm_mount_bind_tty7 0/%define enable_qm_mount_bind_tty7 1/' ${SPECFILE}
tools/version-update -v ${VERSION}
$(MAKE) VERSION=${VERSION} rpm

.PHONY: qm_dropin_mount_bind_input
qm_dropin_mount_bind_input: ## - Creates a QM RPM sub-package to mount bind input in the nested containers
qm_dropin_mount_bind_input: ## - Creates a QM RPM sub-package to mount bind /dev/input in the nested containers
sed -i 's/%define enable_qm_mount_bind_input 0/%define enable_qm_mount_bind_input 1/' ${SPECFILE}
tools/version-update -v ${VERSION}
$(MAKE) VERSION=${VERSION} rpm
Expand Down
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- [Building QM sub-packages](#building-qm-sub-packages)
- [Installing QM sub-packages](Installing-qm-sub-packages)
- [Removing QM sub-packages](Removing-qm-sub-packages)
- [Creating your own drop-in QM sub-package](Creating-your-own-drop-in-QM-sub-package)
- [QM sub-package Sound](QM-sub-package-sound)
3. [SELinux Policy](#selinux-policy)
4. [BlueChi](#bluechi)
5. [RPM building dependencies](#rpm-building-dependencies)
Expand Down Expand Up @@ -151,6 +153,138 @@ sudo podman restart qm
sudo rpm -e qm_mount_bind_input
```

## QM sub-package Sound

### Step 1: Install the QM Mount Bind Sound Package

To set up sound cards in a QM environment using Podman, follow the steps below:
Run the following commands to install the `qm_mount_bind_sound` package and restart QM (if previously in use):

```bash
# Build and install the RPM for QM sound
git clone https://github.com/containers/qm.git && cd qm
make qm_dropin_mount_bind_sound
sudo dnf install -y rpmbuild/RPMS/noarch/qm_mount_bind_sound-0.6.7-1.fc40.noarch.rpm

# To check if your system is using PulseAudio: pactl info
$ pactl info
Server String: /run/user/1000/pulse/native
Library Protocol Version: 35
Server Protocol Version: 35
Is Local: yes
Client Index: 118
Tile Size: 65472
User Name: douglas
Host Name: fedora
Server Name: PulseAudio (on PipeWire 1.0.8)
Server Version: 15.0.0
Default Sample Specification: float32le 2ch 48000Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp__sink
Default Source: alsa_input.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp_6__source
Cookie: 9108:667a

# Install PuseAudio (pactl) and alsa-utils (aplay) in the QM partition
sudo dnf --installroot /usr/lib/qm/rootfs install pulseaudio-utils alsa-utils -y

# Restart QM container (if already running)
sudo podman restart qm

# Showing /dev/snd data inside QM
sudo podman exec -it qm bash
controlC0 hwC0D0 hwC1D2 pcmC0D7p pcmC0D9p pcmC1D0p pcmC1D3p pcmC1D5p pcmC1D7c timer
controlC1 hwC1D0 pcmC0D3p pcmC0D8p pcmC1D0c pcmC1D31p pcmC1D4p pcmC1D6c seq
```

### Step 2: Identify Sound Cards

After installing the drop-in and restarting QM, you need to identify which sound card in the Linux system will be used in QM. If you're familiar with your sound card setup feel free to skip this step.

To list the sound cards available on your system (in our case, we will pick the number 1):

```bash
cat /proc/asound/cards
```

**Example Output**:

```bash
0 [NVidia ]: HDA-Intel - HDA NVidia
HDA NVidia at 0x9e000000 irq 17
1 [sofhdadsp ]: sof-hda-dsp - sof-hda-dsp
LENOVO-20Y5000QUS-ThinkPadX1ExtremeGen4i
2 [USB ]: USB-Audio - USB Audio Device
Generic USB Audio at usb-0000:00:14.0-5, full speed
```

### Detecting Channels and Sample Rates

To list the supported number of channels and samples use `pactl` command:

```bash
pactl list sinks | grep -i 48000 | uniq
Sample Specification: s24-32le 2ch 48000Hz
```

To test speaker output use the following command with sound card 1, channel 2, sample 48000

```bash
speaker-test -D hw:1,0 -c 2 -r 48000
```

This command runs a test with:

```bash
hw:1,0: sound card 1, device 0
-c 2: two channels (stereo)
-r 48000: sample rate of 48 kHz
```

If you want to test different sample rates, change the `-r` parameter to other values (e.g., 44100 for 44.1 kHz or 96000 for 96 kHz) to see which ones are supported by the hardware.

### Verify Sample Rate Support

To show the supported sample rates for a specific sound card codec, you can also inspect the codec details:

```bash
cat /proc/asound/card1/codec#0 | grep -i rates
```

This will output the supported sample rates for the codec associated with `card1`.

### Differentiating Between Cards

Accessing Card 1 (sof-hda-dsp)

```bash
cat /proc/asound/cards | grep -A 1 '^ 1 '
```

Accessing Card 2 (USB Audio Device)

```bash
cat /proc/asound/cards | grep -A 1 '^ 2 '
```

## Creating your own drop-in QM sub-package

We recommend using the existing drop-in files as a guide and adapting them to your specific needs. However, here are the step-by-step instructions:

1) Create a drop-in file in the directory: `etc/qm/containers/containers.conf.d`
2) Add it as a sub-package to `rpm/qm.spec`
3) Test it by running: `make clean && VERSION=YOURVERSIONHERE make rpm`
4) Additionally, test it with and without enabling the sub-package using (by default it should be disabled but there are cases where it will be enabled by default if QM community decide):

Example changing the spec and triggering the build via make (feel free to automate via sed, awk etc):

```bash
# Define the feature flag: 1 to enable, 0 to disable
# By default it's disabled: 0
%define enable_qm_dropin_img_tempdir 1

$ make clean && VERSION=YOURVERSIONHERE make rpm
```

## RPM building dependencies

In order to build qm package on CentOS Stream 9 you'll need Code Ready Builder
Expand Down
6 changes: 0 additions & 6 deletions docs/devel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

- [Building QM rpm manually with changes](#building-qm-rpm-manually-with-changes)
- [Building CentOS AutoSD and QM manually](#building-centos-autosd-and-qm-manually)
- [QM Drop-in sub-packages](#qm-drop-in-sub-packages)
- [Creating your own drop-in QM sub-package](#creating-your-own-drop-in-qm-sub-package)
- [Useful Commands](#useful-commands)
- [Installing software inside QM partition](#installing-software-inside-qm-partition)
- [Removing software inside QM partition](#removing-software-inside-qm-partition)
Expand Down Expand Up @@ -243,10 +241,6 @@ Error: creating runtime static files directory "/usr/share/containers/storage/li
mkdir /usr/share/containers/storage: read-only file system
```

## QM Drop-in sub-packages

QM developers are always looking for ways to help new users and engineers onboard more easily. With this in mind, they decided to break complex configurations into drop-in sub-package(s) when possible for adjusting the Podman engine configurations specifically within the QM environment. Why? This approach provides an easy way to extend or override settings without modifying the core (original) configuration files.

### Creating your own drop-in QM sub-package

We recommend using the existing drop-in files as a guide and adapting them to your specific needs. However, here are the step-by-step instructions:
Expand Down
46 changes: 46 additions & 0 deletions etc/qm/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Drop-in configuration for Podman to mount bind /dev/snd from host to container
#
# The ALSA sound architecture uses /dev/snd to represent sound hardware devices
# as special files in the filesystem. These files are typically accessed by
# user-space programs (like media players, audio recorders, or mixers) to
# interact with the sound hardware. Applications do not usually access these
# files directly. Instead, they use higher-level libraries (like PulseAudio,
# PipeWire, or ALSA's own libraries) to interface with the devices.
#
# +-------------------------------------------------------------+
# | User-Space Applications |
# |-------------------------------------------------------------|
# | Media Players | Audio Recorders | Audio Mixers |
# +-------------------------------------------------------------+
# | | |
# v v v
# +-------------------------------------------------------------+
# | High-Level Audio Libraries (User Space) |
# |-------------------------------------------------------------|
# | ALSA Libraries | PulseAudio | PipeWire |
# +-------------------------------------------------------------+
# | | |
# v v v
# +-------------------------------------------------------------+
# | /dev/snd (Special Files) |
# | ALSA Kernel Driver exposes sound devices as /dev/snd |
# +-------------------------------------------------------------+
# | | |
# v v v
# +-------------------------------------------------------------+
# | ALSA Kernel Sound Drivers (Hardware) |
# | Interfacing with Sound Cards and Audio Devices |
# +-------------------------------------------------------------+
# | | |
# v v v
# +-------------------------------------------------------------+
# | Sound Hardware (Speakers, Mics) |
# +-------------------------------------------------------------+
#
# qm_dropin_mount_bind_snd.conf
[containers]

# Devices to map for the container (sound device)
devices = [
"/dev/snd:/dev/snd"
]
40 changes: 40 additions & 0 deletions rpm/qm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
# it usually runs on this virtual console.
%define enable_qm_mount_bind_tty7 0

#######################################################################
# subpackage QM - mount bind audio device from host to container and #
# nested container enabling sound #
#######################################################################
%define enable_qm_mount_bind_sound 0

###########################################
# subpackage QM - mount bind /dev/ttyUSB0 #
Expand Down Expand Up @@ -136,6 +141,22 @@ install -d %{buildroot}%{_sysconfdir}/qm/containers/containers.conf.d
# END - qm dropin sub-package - img tempdir #
########################################################

########################################################
# START - qm dropin sub-package - mount sound #
########################################################
%if %{enable_qm_mount_bind_sound}
# first step - add drop-in file in /etc/containers/containers.d.conf/qm_dropin_mount_bind_snd.conf
# to QM container mount bind /dev/snd
install -m 644 %{_builddir}/qm-%{version}/etc/qm/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf %{buildroot}%{_sysconfdir}/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf

# second step - add drop-in file in /etc/qm/containers/containers.d.conf/qm_dropin/mount_bind_snd.conf
# to nested containers in QM env mount bind it in /dev/snd
install -m 644 %{_builddir}/qm-%{version}/etc/qm/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf %{buildroot}%{_sysconfdir}/qm/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf
%endif
########################################################
# END - qm dropin sub-package - mount sound #
########################################################

########################################################
# START - qm dropin sub-package - mount ttyUSB0 #
########################################################
Expand Down Expand Up @@ -315,6 +336,25 @@ additional drop-in configurations.
%{_sysconfdir}/qm/containers/containers.conf.d/qm_dropin_mount_bind_ttyUSB0.conf
%endif

#######################################
# sub-package QM Mount Bind /dev/snd #
#######################################
%if %{enable_qm_mount_bind_sound}
%package -n qm_mount_bind_sound
Summary: Drop-in configuration for QM containers to mount bind /dev/snd
Requires: %{name} = %{version}-%{release}
BuildArch: noarch

%description -n qm_mount_bind_sound
This sub-package installs a drop-in configurations for the QM.
It creates the `/etc/qm/containers/containers.conf.d/` directory for adding
additional drop-in configurations.

%files -n qm_mount_bind_sound
%{_sysconfdir}/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf
%{_sysconfdir}/qm/containers/containers.conf.d/qm_dropin_mount_bind_snd.conf
%endif

#######################################

%changelog
Expand Down

0 comments on commit f981a29

Please sign in to comment.