|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | +# Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 5 | + |
| 6 | +## |
| 7 | +## Case Name: latency baseline statistics collection on a signal path |
| 8 | +## |
| 9 | +## Preconditions: |
| 10 | +## - JACK Audio Connection Kit is installed. |
| 11 | +## - loopback connection to measure latency over its signal path. |
| 12 | +## |
| 13 | +## Description: |
| 14 | +## Run `jackd` audio server; execute `jack_iodelay` with its in/out ports |
| 15 | +## connected to the loopback-ed ports and give it some time ot collect |
| 16 | +## latency measurements (on each 0.5 sec.) |
| 17 | +## |
| 18 | +## Case step: |
| 19 | +## 1. Probe to start `jackd` with parameters given and read configuration. |
| 20 | +## 2. Start `jackd` again for latency measurements. |
| 21 | +## 3. Start `jack_iodelay` which awaits for its ports connected to loopback. |
| 22 | +## 4. Connect `jack_iodelay` ports to signal path ports with the loopback. |
| 23 | +## 5. Wait for the period given to collect enough latency measurements. |
| 24 | +## 6. Calculate latency statistics and save into a JSON file. |
| 25 | +## |
| 26 | +## Expect result: |
| 27 | +## Latency statistics collected and saved in `results.json`. |
| 28 | +## Exit status 0. |
| 29 | +## |
| 30 | + |
| 31 | +TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 32 | +TESTLIB="${TESTDIR}/case-lib" |
| 33 | + |
| 34 | +# shellcheck source=case-lib/lib.sh |
| 35 | +source "${TESTLIB}/lib.sh" |
| 36 | + |
| 37 | +OPT_NAME['R']='run_period' OPT_DESC['R']='Time period (in seconds) to measure latency.' |
| 38 | +OPT_HAS_ARG['R']=1 OPT_VAL['R']="30" |
| 39 | + |
| 40 | +OPT_NAME['d']='device' OPT_DESC['d']='ALSA pcm device to use by JACK' |
| 41 | +OPT_HAS_ARG['d']=1 OPT_VAL['d']="hw:0" |
| 42 | + |
| 43 | +OPT_NAME['r']='rate' OPT_DESC['r']='Sample rate to try latency with' |
| 44 | +OPT_HAS_ARG['r']=1 OPT_VAL['r']=48000 |
| 45 | + |
| 46 | +OPT_NAME['S']='shorts' OPT_DESC['S']='Try to use 16-bit samples instead of 32-bit, if possible.' |
| 47 | +OPT_HAS_ARG['S']=0 OPT_VAL['S']=0 |
| 48 | + |
| 49 | +OPT_NAME['p']='port_p' OPT_DESC['p']='Jack playback port with loopback. Example: system:playback_1' |
| 50 | +OPT_HAS_ARG['p']=1 OPT_VAL['p']='' |
| 51 | + |
| 52 | +OPT_NAME['c']='port_c' OPT_DESC['c']='Jack capture port with loopback. Example: system:capture_1' |
| 53 | +OPT_HAS_ARG['c']=1 OPT_VAL['c']='' |
| 54 | + |
| 55 | +OPT_NAME['s']='sof-logger' OPT_DESC['s']="Open sof-logger trace the data will store at $LOG_ROOT" |
| 56 | +OPT_HAS_ARG['s']=0 OPT_VAL['s']=1 |
| 57 | + |
| 58 | +OPT_NAME['v']='verbose' OPT_DESC['v']='Verbose logging.' |
| 59 | +OPT_HAS_ARG['v']=0 OPT_VAL['v']=0 |
| 60 | + |
| 61 | +func_opt_parse_option "$@" |
| 62 | + |
| 63 | +alsa_device=${OPT_VAL['d']} |
| 64 | +alsa_shorts=$([ "${OPT_VAL['S']}" -eq 1 ] && echo '--shorts' || echo '') |
| 65 | +port_playback=${OPT_VAL['p']} |
| 66 | +port_capture=${OPT_VAL['c']} |
| 67 | +rate=${OPT_VAL['r']} |
| 68 | +run_period=${OPT_VAL['R']} |
| 69 | +run_verbose=$([ "${OPT_VAL['v']}" -eq 1 ] && echo '--verbose' || echo '') |
| 70 | + |
| 71 | +RUN_PERIOD_MAX="$((run_period + 30))s" |
| 72 | +JACKD_TIMEOUT="$((run_period + 15))s" |
| 73 | +JACKD_OPTIONS="${run_verbose} --realtime --temporary" |
| 74 | +JACKD_BACKEND="alsa" |
| 75 | +JACKD_BACKEND_OPTIONS="-d ${alsa_device} -r ${rate} ${alsa_shorts}" |
| 76 | +WAIT_JACKD="2s" |
| 77 | +IODELAY_TIMEOUT="${run_period}s" |
| 78 | +WAIT_IODELAY="2s" |
| 79 | + |
| 80 | +if [ "$port_playback" == "" ] || [ "$port_capture" == "" ]; |
| 81 | +then |
| 82 | + skip_test "No playback or capture Jack port is specified. Skip the test." |
| 83 | +fi |
| 84 | + |
| 85 | +setup_kernel_check_point |
| 86 | + |
| 87 | +start_test |
| 88 | + |
| 89 | +logger_disabled || func_lib_start_log_collect |
| 90 | + |
| 91 | +check_jackd_configured() |
| 92 | +{ |
| 93 | + dlogc "jackd ${JACKD_OPTIONS} -d ${JACKD_BACKEND} ${JACKD_BACKEND_OPTIONS}" |
| 94 | + # shellcheck disable=SC2086 |
| 95 | + JACK_NO_AUDIO_RESERVATION=1 timeout --kill-after ${RUN_PERIOD_MAX} ${run_verbose} ${JACKD_TIMEOUT} \ |
| 96 | + jackd ${JACKD_OPTIONS} -d ${JACKD_BACKEND} ${JACKD_BACKEND_OPTIONS} & jackdPID=$! |
| 97 | + |
| 98 | + sleep ${WAIT_JACKD} |
| 99 | + |
| 100 | + dlogc "jack_lsp" |
| 101 | + jack_lsp -AclLpt |
| 102 | + |
| 103 | + dlogi "Waiting Jackd to stop without a client" |
| 104 | + wait ${jackdPID} |
| 105 | +} |
| 106 | + |
| 107 | +collect_latency_data() |
| 108 | +{ |
| 109 | + dlogc "jackd ${JACKD_OPTIONS} -d ${JACKD_BACKEND} ${JACKD_BACKEND_OPTIONS}" |
| 110 | + # shellcheck disable=SC2086 |
| 111 | + JACK_NO_AUDIO_RESERVATION=1 timeout --kill-after ${RUN_PERIOD_MAX} ${run_verbose} ${JACKD_TIMEOUT} \ |
| 112 | + jackd ${JACKD_OPTIONS} -d ${JACKD_BACKEND} ${JACKD_BACKEND_OPTIONS} & jackdPID=$! |
| 113 | + |
| 114 | + sleep ${WAIT_JACKD} |
| 115 | + dlogc "jack_iodelay" |
| 116 | + # shellcheck disable=SC2086 |
| 117 | + timeout --kill-after ${RUN_PERIOD_MAX} ${run_verbose} ${IODELAY_TIMEOUT} \ |
| 118 | + stdbuf -oL -eL jack_iodelay | \ |
| 119 | + tee >(AWKPATH="${TESTLIB}:${AWKPATH}" \ |
| 120 | + gawk -f "${TESTLIB}/jack_iodelay_metrics.awk" > "${LOG_ROOT}/metrics.json") & iodelayPID="$!" |
| 121 | + |
| 122 | + sleep ${WAIT_IODELAY} |
| 123 | + dlogi "jack_connect: ${port_capture} ==>[jack_delay]==> ${port_playback}" |
| 124 | + jack_connect jack_delay:out "${port_playback}" && jack_connect jack_delay:in "${port_capture}" |
| 125 | + |
| 126 | + dlogi "Latency data collection" |
| 127 | + wait ${jackdPID} ${iodelayPID} |
| 128 | + dlogi "Latency data collection completed." |
| 129 | +} |
| 130 | + |
| 131 | +main() |
| 132 | +{ |
| 133 | + # TODO: should we set volume to some pre-defined level (parameterized) |
| 134 | + # reset_sof_volume |
| 135 | + # set_alsa_settings |
| 136 | + |
| 137 | + dlogi "Check Jack server can be started" |
| 138 | + check_jackd_configured # "$@" |
| 139 | + |
| 140 | + dlogi "Start collecting latency data" |
| 141 | + collect_latency_data # "$@" |
| 142 | + |
| 143 | + if [ ! -f "${LOG_ROOT}/metrics.json" ] || [ "$(grep -ce 'metrics' "${LOG_ROOT}/metrics.json")" -lt 1 ]; then |
| 144 | + skip_test "No metrics collected" |
| 145 | + fi |
| 146 | + |
| 147 | + dlogi "Compose results.json" |
| 148 | + echo -n "{\"options\":{$(options2json)}," > "${LOG_ROOT}/results.json" |
| 149 | + cat "${LOG_ROOT}/metrics.json" >> "${LOG_ROOT}/results.json" && rm "${LOG_ROOT}/metrics.json" |
| 150 | + echo "}" >> "${LOG_ROOT}/results.json" |
| 151 | + cat "${LOG_ROOT}/results.json" |
| 152 | +} |
| 153 | + |
| 154 | +{ |
| 155 | + main "$@"; exit "$?" |
| 156 | +} |
0 commit comments