Skip to content

Commit 7cfa5bb

Browse files
committed
check-alsabat: add option to test with pipewire
1 parent a9f04af commit 7cfa5bb

File tree

3 files changed

+157
-47
lines changed

3 files changed

+157
-47
lines changed

case-lib/lib.sh

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ func_lib_enable_pipewire()
588588
systemctl --user start wireplumber.service
589589

590590
systemctl --user daemon-reload
591+
sleep 3
591592

592593
systemctl is-active --user --quiet pipewire{,-pulse}.{socket,service} && dlogi "Pipewire started"
593594
systemctl is-active --user --quiet wireplumber.service && dlogi "Wireplumber started"
@@ -609,6 +610,7 @@ func_lib_disable_pipewire()
609610

610611
sudo systemctl --global mask wireplumber.service
611612
sudo systemctl --global mask pipewire{,-pulse}.{socket,service}
613+
sleep 1
612614

613615
if systemctl is-active --user --quiet wireplumber.service; then dlogi "Wireplumber not stopped"; else dlogi "Wireplumber stopped"; fi
614616
if systemctl is-active --user --quiet pipewire{,-pulse}.{socket,service}; then dlogi "Pipewire not stopped"; else dlogi "Pipewire stopped"; fi
@@ -951,15 +953,9 @@ aplay_opts()
951953
# shellcheck disable=SC2086
952954
tinyplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS -D "$card_nr" -d "$dev_nr" -i wav noise.wav
953955
elif [[ "$SOF_ALSA_TOOL" = "alsa" ]]; then
954-
if [[ "$SOF_TEST_PIPEWIRE" == true ]]; then
955-
dlogc "timeout -k $duration $duration aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS $*" # option -d doesn't work with pipewire so we need timeout
956-
# shellcheck disable=SC2086
957-
timeout -k "$duration" "$duration" aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS "$@"
958-
else
959-
dlogc "aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS $*"
960-
# shellcheck disable=SC2086
961-
aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS "$@"
962-
fi
956+
dlogc "aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS $*"
957+
# shellcheck disable=SC2086
958+
aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS "$@"
963959
else
964960
die "Unknown ALSA tool: ${SOF_ALSA_TOOL}"
965961
fi
@@ -976,20 +972,33 @@ arecord_opts()
976972
# shellcheck disable=SC2086
977973
tinycap $SOF_ALSA_OPTS $SOF_ARECORD_OPTS "$file" -D "$card_nr" -d "$dev_nr" -c "$channel" -t "$duration" -r "$rate" -b "$format"
978974
elif [[ "$SOF_ALSA_TOOL" = "alsa" ]]; then
979-
if [[ "$SOF_TEST_PIPEWIRE" == true ]]; then
980-
dlogc "timeout -k $duration $duration arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS $*" # option -d doesn't work with pipewire so we need timeout
981-
# shellcheck disable=SC2086
982-
timeout -k "$duration" "$duration" arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS "$@"
983-
else
984-
dlogc "arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS $*"
985-
# shellcheck disable=SC2086
986-
arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS "$@"
987-
fi
975+
dlogc "arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS $*"
976+
# shellcheck disable=SC2086
977+
arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS "$@"
988978
else
989979
die "Unknown ALSA tool: ${SOF_ALSA_TOOL}"
990980
fi
991981
}
992982

983+
# Get the ID of the first source of a given type, e.g. "Microphone" or "Audio codec". Print an empty line if ID not found.
984+
get_id_of_pipewire_source()
985+
{
986+
# $ wpctl status returns list of all endpoints managed by wireplumber. We use grep to get only lines after "Sources".
987+
# Then we filter by given sink/source type, which returns something like this:
988+
# │ * 48. sof-soundwire Microphone [vol: 0.40] (or without the * when it's not the current default)
989+
# We filter out everything but ID, and only take the first line of the output (if there's more that one object of that type we ignore the rest)
990+
991+
local object_name="$1"
992+
object_id=$(wpctl status | grep "Sources" -A 10 | awk -v name="$object_name" 'tolower($0) ~ tolower(name) { sub(/\*/,""); sub(/\./,"",$2); print $2; exit }')
993+
994+
# Check if object_id is a number
995+
re='^[0-9]+$'
996+
if [[ "$object_id" =~ $re ]] ; then
997+
printf '%s' "$object_id"
998+
fi
999+
1000+
}
1001+
9931002
# Get the ID of the first sink/source of a given type, e.g. "Speaker" or "Headphones". Print an empty line if ID not found.
9941003
get_id_of_pipewire_endpoint()
9951004
{
@@ -1008,6 +1017,42 @@ get_id_of_pipewire_endpoint()
10081017

10091018
}
10101019

1020+
# Get pipewire sink name for given alsa pcm
1021+
# Arguments: 1-alsa pcm, ex. hw:0,0
1022+
set_default_pipewire_sink_for_alsa_pcm()
1023+
{
1024+
card_id=$(echo "$1" | awk -F'[:,]' '{print $2}')
1025+
dev_id=$(echo "$1" | awk -F'[:,]' '{print $3}')
1026+
dlogi "card: $card_id, device: $dev_id"
1027+
1028+
sink_name=$(pactl list sinks \
1029+
| grep -B 20 "alsa.device = \"$dev_id\"" \
1030+
| grep -B 16 "alsa.card = \"$card_id\"" \
1031+
| grep "Name: alsa_output" \
1032+
| cut -d' ' -f2-)
1033+
1034+
dlogi "Setting default sink set to $sink_name"
1035+
pactl set-default-sink "$sink_name"
1036+
}
1037+
1038+
# Get pipewire source name for given alsa pcm
1039+
# Arguments: 1-alsa pcm, ex. hw:0,0
1040+
set_default_pipewire_source_for_alsa_pcm()
1041+
{
1042+
card_id=$(echo "$1" | awk -F'[:,]' '{print $2}')
1043+
dev_id=$(echo "$1" | awk -F'[:,]' '{print $3}')
1044+
dlogi "card: $card_id, device: $dev_id"
1045+
1046+
sink_name=$(pactl list sources \
1047+
| grep -B 20 "alsa.device = \"$dev_id\"" \
1048+
| grep -B 16 "alsa.card = \"$card_id\"" \
1049+
| grep "Name: alsa_input" \
1050+
| cut -d' ' -f2-)
1051+
1052+
dlogi "Setting default source set to $sink_name"
1053+
pactl set-default-source "$sink_name"
1054+
}
1055+
10111056
die()
10121057
{
10131058
dloge "$@"

test-case/check-alsabat.sh

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,97 @@ function __upload_wav_file
100100
done
101101
}
102102

103-
# check the PCMs before alsabat test
104-
dlogi "check the PCMs before alsabat test"
105-
aplay "-Dplug${pcm_p}" -d 1 /dev/zero -q || die "Failed to play on PCM: ${pcm_p}"
106-
arecord "-Dplug${pcm_c}" -d 1 /dev/null -q || die "Failed to capture on PCM: ${pcm_c}"
107-
108-
# alsabat test
109-
# BT offload PCMs also support mono playback.
110-
dlogc "alsabat -P$pcm_p --standalone -n $frames -r $rate -c $channel_p -f $format -F $frequency -k $sigmak"
111-
alsabat "-P${pcm_p}" --standalone -n "${frames}" -c "${channel_p}" -r "${rate}" -f "${format}" -F "${frequency}" -k "${sigmak}" & playPID=$!
112-
113-
# playback may have low latency, add one second delay to aviod recording zero at beginning.
114-
sleep 1
115-
116-
# Select the first card
117-
first_card_name=$(aplay -l | awk '/^card ([0-9]+)/ {print $3; exit}')
118-
# dump amixer contents always.
119-
# Good case amixer settings is for reference, bad case for debugging.
120-
amixer -c "${first_card_name}" contents > "$LOG_ROOT"/amixer_settings.txt
121-
122-
# We use different USB sound cards in CI, part of them only support 1 channel for capture,
123-
# so make the channel as an option and config it in alsabat-playback.csv
124-
dlogc "alsabat -C$pcm_c -c $channel_c -r $rate -f $format -F $frequency -k $sigmak"
125-
alsabat "-C${pcm_c}" -c "${channel_c}" -r "${rate}" -f "${format}" -F "${frequency}" -k "${sigmak}" || {
126-
# upload failed wav file
127-
__upload_wav_file
128-
exit 1
103+
# Set default pipewire sink and source
104+
set_pcms_in_pipewire()
105+
{
106+
set_default_pipewire_sink_for_alsa_pcm "$pcm_p"
107+
set_default_pipewire_source_for_alsa_pcm "$pcm_c"
108+
}
109+
110+
check_the_pcms()
111+
{
112+
aplay "-Dplug${pcm_p}" -d 1 /dev/zero -q || die "Failed to play on PCM: ${pcm_p}"
113+
arecord "-Dplug${pcm_c}" -d 1 /dev/null -q || die "Failed to capture on PCM: ${pcm_c}"
114+
}
115+
116+
check_the_pcms_with_pipewire()
117+
{
118+
aplay -D pipewire -d 1 /dev/zero -q || die "Failed to play on pipewire"
119+
arecord -D pipewire -d 1 /dev/null -q || die "Failed to capture on pipewire"
120+
}
121+
122+
run_test_on_pipewire()
123+
{
124+
# Set correct sink and source in pipewire
125+
set_pcms_in_pipewire
126+
127+
# check the PCMs before alsabat test
128+
check_the_pcms_with_pipewire
129+
130+
# alsabat test
131+
# when ran without specified PCM, alsabat does playback and capture in one command
132+
dlogc "alsabat -n $frames -r $rate -c $channel_p -f $format -F $frequency -k $sigmak"
133+
alsabat -n "${frames}" -c "${channel_p}" -r "${rate}" -f "${format}" -F "${frequency}" -k "${sigmak}" || {
134+
# upload failed wav file
135+
__upload_wav_file
136+
exit 1
137+
}
129138
}
130139

131-
wait $playPID
140+
run_test_on_alsa_direct_mode()
141+
{
142+
# check the PCMs before alsabat test
143+
check_the_pcms
144+
145+
# alsabat test
146+
# BT offload PCMs also support mono playback.
147+
dlogc "alsabat -P$pcm_p --standalone -n $frames -r $rate -c $channel_p -f $format -F $frequency -k $sigmak"
148+
alsabat "-P${pcm_p}" --standalone -n "${frames}" -c "${channel_p}" -r "${rate}" -f "${format}" -F "${frequency}" -k "${sigmak}" & playPID=$!
149+
150+
# playback may have low latency, add one second delay to aviod recording zero at beginning.
151+
sleep 1
152+
153+
# Select the first card
154+
first_card_name=$(aplay -l | awk '/^card ([0-9]+)/ {print $3; exit}')
155+
# dump amixer contents always.
156+
# Good case amixer settings is for reference, bad case for debugging.
157+
amixer -c "${first_card_name}" contents > "$LOG_ROOT"/amixer_settings.txt
158+
159+
# We use different USB sound cards in CI, part of them only support 1 channel for capture,
160+
# so make the channel as an option and config it in alsabat-playback.csv
161+
dlogc "alsabat -C$pcm_c -c $channel_c -r $rate -f $format -F $frequency -k $sigmak"
162+
alsabat "-C${pcm_c}" -c "${channel_c}" -r "${rate}" -f "${format}" -F "${frequency}" -k "${sigmak}" || {
163+
# upload failed wav file
164+
__upload_wav_file
165+
exit 1
166+
}
167+
168+
wait $playPID
169+
}
170+
171+
main()
172+
{
173+
start_test
174+
175+
if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ];
176+
then
177+
dloge "No playback or capture PCM is specified. Skip the alsabat test"
178+
exit 2
179+
fi
180+
181+
check_locale_for_alsabat
182+
183+
logger_disabled || func_lib_start_log_collect
184+
185+
set_alsa
186+
187+
if [ "$SOF_TEST_PIPEWIRE" == true ]; then
188+
run_test_on_pipewire
189+
else
190+
run_test_on_alsa_direct_mode
191+
fi
192+
}
193+
194+
{
195+
main "$@"; exit "$?"
196+
}

test-case/check-performance.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ run_aplays()
5959
fi
6060
dlogi "Setting default sink to $sink_id: $sink_type"
6161
wpctl set-default "$sink_id"
62-
aplay_opts -D pipewire /dev/zero -q &
62+
aplay_opts -D pipewire /dev/zero -d "$duration" -q &
6363
aplay_num=$((aplay_num+1))
6464
done
6565
}
@@ -78,7 +78,7 @@ run_arecords()
7878
fi
7979
dlogi "Setting default source to $source_id: $source_type"
8080
wpctl set-default "$source_id"
81-
arecord_opts -D pipewire /dev/null -q &
81+
arecord_opts -D pipewire /dev/null -d "$duration" -q &
8282
arecord_num=$((arecord_num+1))
8383
done
8484
}

0 commit comments

Comments
 (0)