|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright(c) 2025 Intel Corporation. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +## |
| 7 | +## Case Name: Execute ALSA conformance tests. |
| 8 | +## |
| 9 | +## Preconditions: |
| 10 | +## - ChromeOS Audio Test package is installed |
| 11 | +## https://chromium.googlesource.com/chromiumos/platform/audiotest |
| 12 | +## |
| 13 | +## Description: |
| 14 | +## Run `alsa_conformance_test.py` with all test cases twice: for the |
| 15 | +## playback and for the capture devices given. |
| 16 | +## Compose resulting JSON reports. |
| 17 | +## |
| 18 | +## Case steps: |
| 19 | +## 0. Set ALSA parameters. |
| 20 | +## 1. Try to start `alsa_conformance_test` in device info mode. |
| 21 | +## 2. Start `alsa conformance_test.py` for playback device. |
| 22 | +## 3. Start `alsa conformance_test.py` for capture device. |
| 23 | +## 4. Compose resulting JSON reports. |
| 24 | +## |
| 25 | +## Expect result: |
| 26 | +## ALSA conformance results collected and saved in `test_result.json` file. |
| 27 | +## Exit status 0. |
| 28 | +## |
| 29 | + |
| 30 | +TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 31 | +TESTLIB="${TESTDIR}/case-lib" |
| 32 | + |
| 33 | +# shellcheck source=case-lib/lib.sh |
| 34 | +source "${TESTLIB}/lib.sh" |
| 35 | + |
| 36 | +OPT_NAME['d']='device' OPT_DESC['d']='ALSA pcm device for playback and capture. Example: hw:0' |
| 37 | +OPT_HAS_ARG['d']=1 OPT_VAL['d']='' |
| 38 | + |
| 39 | +OPT_NAME['p']='pcm_p' OPT_DESC['p']='ALSA pcm device for playback only. Example: hw:soundwire,0' |
| 40 | +OPT_HAS_ARG['p']=1 OPT_VAL['p']='' |
| 41 | + |
| 42 | +OPT_NAME['c']='pcm_c' OPT_DESC['c']='ALSA pcm device for capture only. Example: hw:soundwire,1' |
| 43 | +OPT_HAS_ARG['c']=1 OPT_VAL['c']='' |
| 44 | + |
| 45 | +OPT_NAME['r']='rates' OPT_DESC['r']='Sample ratis to try. Default: check all available rates.' |
| 46 | +OPT_HAS_ARG['r']=1 OPT_VAL['r']='' |
| 47 | + |
| 48 | +OPT_NAME['F']='formats' OPT_DESC['F']='Data formats to try. Default: check all available formats.' |
| 49 | +OPT_HAS_ARG['F']=1 OPT_VAL['F']='' |
| 50 | + |
| 51 | +OPT_NAME['s']='sof-logger' OPT_DESC['s']="Open sof-logger trace the data will store at $LOG_ROOT" |
| 52 | +OPT_HAS_ARG['s']=0 OPT_VAL['s']=1 |
| 53 | + |
| 54 | +OPT_NAME['v']='verbose' OPT_DESC['v']='Verbose logging.' |
| 55 | +OPT_HAS_ARG['v']=0 OPT_VAL['v']=0 |
| 56 | + |
| 57 | +OPT_NAME['E']='rate-diff' OPT_DESC['E']="ALSA conformance --rate-criteria-diff-pct (difference, %)." |
| 58 | +OPT_HAS_ARG['E']=1 OPT_VAL['E']='' |
| 59 | + |
| 60 | +OPT_NAME['e']='rate-err' OPT_DESC['e']="ALSA conformance --rate-err-criteria (max rate error)." |
| 61 | +OPT_HAS_ARG['e']=1 OPT_VAL['e']='' |
| 62 | + |
| 63 | +OPT_NAME['a']='avail-delay' OPT_DESC['a']="ALSA conformance --avail-delay" |
| 64 | +OPT_HAS_ARG['a']=0 OPT_VAL['a']=0 |
| 65 | + |
| 66 | +OPT_NAME['T']='test-suites' OPT_DESC['T']="ALSA conformance --test-suites (Default: all)." |
| 67 | +OPT_HAS_ARG['T']=1 OPT_VAL['T']='' |
| 68 | + |
| 69 | +func_opt_parse_option "$@" |
| 70 | + |
| 71 | +alsa_device=${OPT_VAL['d']} |
| 72 | +pcm_p=${OPT_VAL['p']} |
| 73 | +pcm_c=${OPT_VAL['c']} |
| 74 | +rates=$([ -z "${OPT_VAL['r']}" ] && echo '' || echo "--allow-rates ${OPT_VAL['r']}") |
| 75 | +formats=$([ -z "${OPT_VAL['F']}" ] && echo '' || echo "--allow-formats ${OPT_VAL['F']}") |
| 76 | +run_verbose=$([ "${OPT_VAL['v']}" -eq 1 ] && echo '--log-file /dev/stdout' || echo '') |
| 77 | +rate_diff=$([ -z "${OPT_VAL['E']}" ] && echo '' || echo "--rate-criteria-diff-pct ${OPT_VAL['E']}") |
| 78 | +rate_err=$([ -z "${OPT_VAL['e']}" ] && echo '' || echo "--rate-err-criteria ${OPT_VAL['e']}") |
| 79 | +avail_delay=$([ "${OPT_VAL['a']}" -eq 1 ] && echo '--avail-delay' || echo '') |
| 80 | +test_suites=$([ -z "${OPT_VAL['T']}" ] && echo '' || echo "--test-suites ${OPT_VAL['T']}") |
| 81 | + |
| 82 | +AUDIOTEST_OUT="${LOG_ROOT}/alsa_conformance" |
| 83 | +RESULT_JSON="${LOG_ROOT}/test_result.json" |
| 84 | + |
| 85 | +ALSA_CONFORMANCE_OPTIONS=("${formats}" "${rates}" "${rate_diff}" "${rate_err}" "${avail_delay}" "${test_suites}") |
| 86 | +PLAYBACK_DEVICE="${pcm_p}" |
| 87 | +CAPTURE_DEVICE="${pcm_c}" |
| 88 | +PLAYBACK_OPTIONS="" |
| 89 | +CAPTURE_OPTIONS="" |
| 90 | + |
| 91 | +ALSA_CONFORMANCE_PATH=$([ -n "$ALSA_CONFORMANCE_PATH" ] || realpath "${TESTDIR}/../audiotest") |
| 92 | +ALSA_CONFORMANCE_TEST="${ALSA_CONFORMANCE_PATH}/alsa_conformance_test" |
| 93 | + |
| 94 | + |
| 95 | +check_alsa_conformance_suite() |
| 96 | +{ |
| 97 | + if [ -d "${ALSA_CONFORMANCE_PATH}" ]; then |
| 98 | + if [ -x "${ALSA_CONFORMANCE_TEST}" ] && [ -x "${ALSA_CONFORMANCE_TEST}.py" ]; then |
| 99 | + dlogi "Use ALSA conformance test suite: ${ALSA_CONFORMANCE_TEST}" |
| 100 | + return |
| 101 | + fi |
| 102 | + fi |
| 103 | + skip_test "ALSA conformance test suite is missing at: ${ALSA_CONFORMANCE_PATH}" |
| 104 | +} |
| 105 | + |
| 106 | +check_options() |
| 107 | +{ |
| 108 | + if [ -n "${alsa_device}" ]; then |
| 109 | + if [ -n "${pcm_p}" ] || [ -n "${pcm_c}" ]; then |
| 110 | + skip_test "Give either ALSA device, or ALSA playback/capture pcm-s." |
| 111 | + fi |
| 112 | + PLAYBACK_DEVICE="${alsa_device}" |
| 113 | + PLAYBACK_OPTIONS+="-P ${alsa_device}" |
| 114 | + CAPTURE_DEVICE="${alsa_device}" |
| 115 | + CAPTURE_OPTIONS+="-C ${alsa_device}" |
| 116 | + elif [ -z "${pcm_p}" ] && [ -z "${pcm_c}" ]; then |
| 117 | + skip_test "Neither playback, nor capture ALSA PCM is specified." |
| 118 | + else |
| 119 | + PLAYBACK_OPTIONS+=$([ -n "${pcm_p}" ] && echo "-P ${pcm_p}" || echo "") |
| 120 | + CAPTURE_OPTIONS+=$([ -n "${pcm_c}" ] && echo "-C ${pcm_c}" || echo "") |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +set_alsa() |
| 125 | +{ |
| 126 | + reset_sof_volume |
| 127 | + |
| 128 | + # If MODEL is defined, set proper gain for the platform |
| 129 | + if [ -z "$MODEL" ]; then |
| 130 | + dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh" |
| 131 | + else |
| 132 | + set_alsa_settings "$MODEL" |
| 133 | + fi |
| 134 | +} |
| 135 | + |
| 136 | +alsa_conformance_device_info() |
| 137 | +{ |
| 138 | + run_cmd="${ALSA_CONFORMANCE_TEST} ${PLAYBACK_OPTIONS} ${CAPTURE_OPTIONS} --dev_info_only" |
| 139 | + dlogc "${run_cmd}" |
| 140 | + bash -c "${run_cmd}" || rc_=$? |
| 141 | + [[ "${rc_}" -ne 0 ]] && die "Failed to get device info, rc=${rc_}" |
| 142 | +} |
| 143 | + |
| 144 | +alsa_conformance_test_playback() |
| 145 | +{ |
| 146 | + if [ -n "${PLAYBACK_OPTIONS}" ]; then |
| 147 | + run_cmd="PATH=${ALSA_CONFORMANCE_PATH}:${PATH} ${ALSA_CONFORMANCE_TEST}.py ${ALSA_CONFORMANCE_OPTIONS[*]} ${PLAYBACK_OPTIONS} --json-file ${AUDIOTEST_OUT}_playback.json ${run_verbose}" |
| 148 | + dlogc "${run_cmd}" |
| 149 | + bash -c "${run_cmd}" || rc_=$? |
| 150 | + [[ "${rc_}" -ne 0 ]] && dlogw "Failed PLAYBACK TESTS, rc=${rc_}" |
| 151 | + fi |
| 152 | +} |
| 153 | + |
| 154 | +alsa_conformance_test_capture() |
| 155 | +{ |
| 156 | + if [ -n "${CAPTURE_OPTIONS}" ]; then |
| 157 | + run_cmd="PATH=${ALSA_CONFORMANCE_PATH}:${PATH} ${ALSA_CONFORMANCE_TEST}.py ${ALSA_CONFORMANCE_OPTIONS[*]} ${CAPTURE_OPTIONS} --json-file ${AUDIOTEST_OUT}_capture.json ${run_verbose}" |
| 158 | + dlogc "${run_cmd}" |
| 159 | + bash -c "${run_cmd}" || rc_=$? |
| 160 | + [[ "${rc_}" -ne 0 ]] && dlogw "Failed CAPTURE TESTS, rc=${rc_}" |
| 161 | + fi |
| 162 | +} |
| 163 | + |
| 164 | +report_start() |
| 165 | +{ |
| 166 | + dlogi "Compose ${RESULT_JSON}" |
| 167 | + printf '{"options":{%s}, "alsa_conformance":[' "$(options2json)" > "${RESULT_JSON}" |
| 168 | +} |
| 169 | + |
| 170 | +json_next_sep="" |
| 171 | + |
| 172 | +report_conformance() |
| 173 | +{ |
| 174 | + local report_type=$1 |
| 175 | + local report_device=$2 |
| 176 | + local report_file="${AUDIOTEST_OUT}_${report_type}.json" |
| 177 | + if [ -s "${report_file}" ]; then |
| 178 | + printf '%s{"device":"%s","%s":' \ |
| 179 | + "${json_next_sep}" "${report_device}" "${report_type}" >> "${RESULT_JSON}" |
| 180 | + jq --compact-output . "${report_file}" >> "${RESULT_JSON}" && rm "${report_file}" |
| 181 | + printf '}' >> "${RESULT_JSON}" |
| 182 | + json_next_sep="," |
| 183 | + else |
| 184 | + dlogw "No conformance report for ${report_type}" |
| 185 | + fi |
| 186 | +} |
| 187 | + |
| 188 | +report_end() |
| 189 | +{ |
| 190 | + printf ']}\n' >> "${RESULT_JSON}" |
| 191 | + [ -n "${run_verbose}" ] && cat "${RESULT_JSON}" |
| 192 | +} |
| 193 | + |
| 194 | +assert_failures() |
| 195 | +{ |
| 196 | + local report_type=$1 |
| 197 | + local report_device=$2 |
| 198 | + [ -z "${report_device}" ] && return |
| 199 | + |
| 200 | + local report_key_="alsa_conformance[].${report_type}.fail" |
| 201 | + local failures_="" |
| 202 | + |
| 203 | + failures_=$(jq ".${report_key_} // 0" "${RESULT_JSON}") |
| 204 | + if [ -z "${failures_}" ] || [ "${failures_}" -ne "${failures_}" ]; then |
| 205 | + die "Invalid ${RESULT_JSON}" |
| 206 | + fi |
| 207 | + if [ "${failures_}" -ne 0 ]; then |
| 208 | + die "${report_type} has ${failures_} failures." |
| 209 | + fi |
| 210 | +} |
| 211 | + |
| 212 | +main() |
| 213 | +{ |
| 214 | + check_alsa_conformance_suite |
| 215 | + |
| 216 | + check_options |
| 217 | + |
| 218 | + setup_kernel_check_point |
| 219 | + |
| 220 | + start_test |
| 221 | + |
| 222 | + logger_disabled || func_lib_start_log_collect |
| 223 | + |
| 224 | + set_alsa |
| 225 | + |
| 226 | + alsa_conformance_device_info |
| 227 | + |
| 228 | + report_start |
| 229 | + |
| 230 | + alsa_conformance_test_playback |
| 231 | + report_conformance 'playback' "${PLAYBACK_DEVICE}" |
| 232 | + |
| 233 | + alsa_conformance_test_capture |
| 234 | + report_conformance 'capture' "${CAPTURE_DEVICE}" |
| 235 | + |
| 236 | + report_end |
| 237 | + |
| 238 | + assert_failures 'playback' "${PLAYBACK_DEVICE}" |
| 239 | + assert_failures 'capture' "${CAPTURE_DEVICE}" |
| 240 | +} |
| 241 | + |
| 242 | +{ |
| 243 | + main "$@"; exit "$?" |
| 244 | +} |
0 commit comments