kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
| 10 | """ |
| 11 | This script is the wrapper that runs the low-bandwidth audio test. |
| 12 | |
| 13 | After running the test, post-process steps for calculating audio quality of the |
| 14 | output files will be performed. |
| 15 | """ |
| 16 | |
| 17 | import argparse |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 18 | import collections |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 19 | import json |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 20 | import logging |
| 21 | import os |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 22 | import re |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 23 | import shutil |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
| 26 | |
| 27 | |
| 28 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
Henrik Kjellander | 5a6aa4f | 2017-09-15 09:31:54 +0200 | [diff] [blame] | 29 | SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 30 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 31 | NO_TOOLS_ERROR_MESSAGE = ( |
| 32 | 'Could not find PESQ or POLQA at %s.\n' |
| 33 | '\n' |
| 34 | 'To fix this run:\n' |
| 35 | ' python %s %s\n' |
| 36 | '\n' |
| 37 | 'Note that these tools are Google-internal due to licensing, so in order to ' |
| 38 | 'use them you will have to get your own license and manually put them in the ' |
| 39 | 'right location.\n' |
| 40 | 'See https://cs.chromium.org/chromium/src/third_party/webrtc/tools_webrtc/' |
| 41 | 'download_tools.py?rcl=bbceb76f540159e2dba0701ac03c514f01624130&l=13') |
| 42 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 43 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 44 | def _LogCommand(command): |
| 45 | logging.info('Running %r', command) |
| 46 | return command |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def _ParseArgs(): |
| 50 | parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.') |
| 51 | parser.add_argument('build_dir', |
| 52 | help='Path to the build directory (e.g. out/Release).') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 53 | parser.add_argument('--remove', action='store_true', |
| 54 | help='Remove output audio files after testing.') |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 55 | parser.add_argument('--android', action='store_true', |
| 56 | help='Perform the test on a connected Android device instead.') |
| 57 | parser.add_argument('--adb-path', help='Path to adb binary.', default='adb') |
Edward Lesmes | 9599fd4 | 2018-03-12 16:43:05 -0400 | [diff] [blame] | 58 | parser.add_argument('--num-retries', default='0', |
Edward Lesmes | 5b9c684 | 2018-03-09 13:07:22 -0500 | [diff] [blame] | 59 | help='Number of times to retry the test on Android.') |
Oleh Prypin | 637b0b5 | 2018-09-21 17:16:06 +0200 | [diff] [blame] | 60 | parser.add_argument('--isolated-script-test-perf-output', default=None, |
| 61 | help='Path to store perf results in chartjson format.') |
| 62 | parser.add_argument('--isolated-script-test-output', default=None, |
| 63 | help='Path to output an empty JSON file which Chromium infra requires.') |
Artem Titov | cbc91efa | 2019-07-23 13:23:20 +0200 | [diff] [blame] | 64 | parser.add_argument('--extra-test-args', default=[], action='append', |
| 65 | help='Extra args to path to the test binary.') |
Edward Lemur | 7e3b569 | 2017-10-04 17:03:16 +0200 | [diff] [blame] | 66 | |
| 67 | # Ignore Chromium-specific flags |
Edward Lemur | d8b041c | 2018-01-16 14:30:28 +0100 | [diff] [blame] | 68 | parser.add_argument('--test-launcher-summary-output', |
| 69 | type=str, default=None) |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 70 | args = parser.parse_args() |
Edward Lemur | 7e3b569 | 2017-10-04 17:03:16 +0200 | [diff] [blame] | 71 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 72 | return args |
| 73 | |
| 74 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 75 | def _GetPlatform(): |
| 76 | if sys.platform == 'win32': |
| 77 | return 'win' |
| 78 | elif sys.platform == 'darwin': |
| 79 | return 'mac' |
| 80 | elif sys.platform.startswith('linux'): |
| 81 | return 'linux' |
| 82 | |
| 83 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 84 | def _GetExtension(): |
| 85 | return '.exe' if sys.platform == 'win32' else '' |
| 86 | |
| 87 | |
| 88 | def _GetPathToTools(): |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 89 | tools_dir = os.path.join(SRC_DIR, 'tools_webrtc') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 90 | toolchain_dir = os.path.join(tools_dir, 'audio_quality') |
| 91 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 92 | platform = _GetPlatform() |
| 93 | ext = _GetExtension() |
Edward Lemur | bb1222f | 2017-10-03 12:33:38 +0000 | [diff] [blame] | 94 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 95 | pesq_path = os.path.join(toolchain_dir, platform, 'pesq' + ext) |
| 96 | if not os.path.isfile(pesq_path): |
| 97 | pesq_path = None |
| 98 | |
| 99 | polqa_path = os.path.join(toolchain_dir, platform, 'PolqaOem64' + ext) |
| 100 | if not os.path.isfile(polqa_path): |
| 101 | polqa_path = None |
| 102 | |
| 103 | if (platform != 'mac' and not polqa_path) or not pesq_path: |
| 104 | logging.error(NO_TOOLS_ERROR_MESSAGE, |
| 105 | toolchain_dir, |
| 106 | os.path.join(tools_dir, 'download_tools.py'), |
| 107 | toolchain_dir) |
| 108 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 109 | return pesq_path, polqa_path |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 110 | |
| 111 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 112 | def ExtractTestRuns(lines, echo=False): |
| 113 | """Extracts information about tests from the output of a test runner. |
| 114 | |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 115 | Produces tuples |
| 116 | (android_device, test_name, reference_file, degraded_file, cur_perf_results). |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 117 | """ |
| 118 | for line in lines: |
| 119 | if echo: |
| 120 | sys.stdout.write(line) |
| 121 | |
| 122 | # Output from Android has a prefix with the device name. |
| 123 | android_prefix_re = r'(?:I\b.+\brun_tests_on_device\((.+?)\)\s*)?' |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 124 | test_re = r'^' + android_prefix_re + (r'TEST (\w+) ([^ ]+?) ([^\s]+)' |
| 125 | r' ?([^\s]+)?\s*$') |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 126 | |
| 127 | match = re.search(test_re, line) |
| 128 | if match: |
| 129 | yield match.groups() |
| 130 | |
| 131 | |
| 132 | def _GetFile(file_path, out_dir, move=False, |
| 133 | android=False, adb_prefix=('adb',)): |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 134 | out_file_name = os.path.basename(file_path) |
| 135 | out_file_path = os.path.join(out_dir, out_file_name) |
| 136 | |
| 137 | if android: |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 138 | # Pull the file from the connected Android device. |
| 139 | adb_command = adb_prefix + ('pull', file_path, out_dir) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 140 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 141 | if move: |
| 142 | # Remove that file. |
| 143 | adb_command = adb_prefix + ('shell', 'rm', file_path) |
| 144 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 145 | elif os.path.abspath(file_path) != os.path.abspath(out_file_path): |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 146 | if move: |
| 147 | shutil.move(file_path, out_file_path) |
| 148 | else: |
| 149 | shutil.copy(file_path, out_file_path) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 150 | |
| 151 | return out_file_path |
| 152 | |
| 153 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 154 | def _RunPesq(executable_path, reference_file, degraded_file, |
| 155 | sample_rate_hz=16000): |
| 156 | directory = os.path.dirname(reference_file) |
| 157 | assert os.path.dirname(degraded_file) == directory |
| 158 | |
| 159 | # Analyze audio. |
| 160 | command = [executable_path, '+%d' % sample_rate_hz, |
| 161 | os.path.basename(reference_file), |
| 162 | os.path.basename(degraded_file)] |
| 163 | # Need to provide paths in the current directory due to a bug in PESQ: |
| 164 | # On Mac, for some 'path/to/file.wav', if 'file.wav' is longer than |
| 165 | # 'path/to', PESQ crashes. |
| 166 | out = subprocess.check_output(_LogCommand(command), |
| 167 | cwd=directory, stderr=subprocess.STDOUT) |
| 168 | |
| 169 | # Find the scores in stdout of PESQ. |
| 170 | match = re.search( |
| 171 | r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', out) |
| 172 | if match: |
| 173 | raw_mos, _ = match.groups() |
| 174 | |
| 175 | return {'pesq_mos': (raw_mos, 'score')} |
| 176 | else: |
| 177 | logging.error('PESQ: %s', out.splitlines()[-1]) |
| 178 | return {} |
| 179 | |
| 180 | |
| 181 | def _RunPolqa(executable_path, reference_file, degraded_file): |
| 182 | # Analyze audio. |
| 183 | command = [executable_path, '-q', '-LC', 'NB', |
| 184 | '-Ref', reference_file, '-Test', degraded_file] |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 185 | process = subprocess.Popen(_LogCommand(command), |
| 186 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 187 | out, err = process.communicate() |
| 188 | |
| 189 | # Find the scores in stdout of POLQA. |
| 190 | match = re.search(r'\bMOS-LQO:\s+([\d.]+)', out) |
| 191 | |
| 192 | if process.returncode != 0 or not match: |
| 193 | if process.returncode == 2: |
| 194 | logging.warning('%s (2)', err.strip()) |
| 195 | logging.warning('POLQA license error, skipping test.') |
| 196 | else: |
| 197 | logging.error('%s (%d)', err.strip(), process.returncode) |
| 198 | return {} |
| 199 | |
| 200 | mos_lqo, = match.groups() |
| 201 | return {'polqa_mos_lqo': (mos_lqo, 'score')} |
| 202 | |
| 203 | |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 204 | def _AddChart(charts, metric, test_name, value, units): |
| 205 | chart = charts.setdefault(metric, {}) |
| 206 | chart[test_name] = { |
| 207 | "type": "scalar", |
| 208 | "value": value, |
| 209 | "units": units, |
| 210 | } |
| 211 | |
| 212 | |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 213 | def _AddRunPerfResults(charts, run_perf_results_file): |
| 214 | with open(run_perf_results_file, 'rb') as f: |
| 215 | per_run_perf_results = json.load(f) |
| 216 | if 'charts' not in per_run_perf_results: |
| 217 | return |
| 218 | for metric, cases in per_run_perf_results['charts'].items(): |
| 219 | chart = charts.setdefault(metric, {}) |
| 220 | for case_name, case_value in cases.items(): |
| 221 | if case_name in chart: |
| 222 | logging.error('Overriding results for %s/%s', metric, case_name) |
| 223 | chart[case_name] = case_value |
| 224 | |
| 225 | |
| 226 | Analyzer = collections.namedtuple('Analyzer', ['name', 'func', 'executable', |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 227 | 'sample_rate_hz']) |
| 228 | |
| 229 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 230 | def main(): |
| 231 | # pylint: disable=W0101 |
| 232 | logging.basicConfig(level=logging.INFO) |
| 233 | |
| 234 | args = _ParseArgs() |
| 235 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 236 | pesq_path, polqa_path = _GetPathToTools() |
| 237 | if pesq_path is None: |
| 238 | return 1 |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 239 | |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 240 | out_dir = os.path.join(args.build_dir, '..') |
| 241 | if args.android: |
| 242 | test_command = [os.path.join(args.build_dir, 'bin', |
Edward Lesmes | 5b9c684 | 2018-03-09 13:07:22 -0500 | [diff] [blame] | 243 | 'run_low_bandwidth_audio_test'), |
| 244 | '-v', '--num-retries', args.num_retries] |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 245 | else: |
| 246 | test_command = [os.path.join(args.build_dir, 'low_bandwidth_audio_test')] |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 247 | |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 248 | analyzers = [Analyzer('pesq', _RunPesq, pesq_path, 16000)] |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 249 | # Check if POLQA can run at all, or skip the 48 kHz tests entirely. |
| 250 | example_path = os.path.join(SRC_DIR, 'resources', |
| 251 | 'voice_engine', 'audio_tiny48.wav') |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 252 | if polqa_path and _RunPolqa(polqa_path, example_path, example_path): |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 253 | analyzers.append(Analyzer('polqa', _RunPolqa, polqa_path, 48000)) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 254 | |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 255 | charts = {} |
| 256 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 257 | for analyzer in analyzers: |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 258 | # Start the test executable that produces audio files. |
| 259 | test_process = subprocess.Popen( |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 260 | _LogCommand(test_command + [ |
| 261 | '--sample_rate_hz=%d' % analyzer.sample_rate_hz, |
| 262 | '--test_case_prefix=%s' % analyzer.name |
Artem Titov | cbc91efa | 2019-07-23 13:23:20 +0200 | [diff] [blame] | 263 | ] + args.extra_test_args), |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 264 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 265 | perf_results_file = None |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 266 | try: |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 267 | lines = iter(test_process.stdout.readline, '') |
| 268 | for result in ExtractTestRuns(lines, echo=True): |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 269 | (android_device, test_name, reference_file, degraded_file, |
| 270 | perf_results_file) = result |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 271 | |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 272 | adb_prefix = (args.adb_path,) |
| 273 | if android_device: |
| 274 | adb_prefix += ('-s', android_device) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 275 | |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 276 | reference_file = _GetFile(reference_file, out_dir, |
| 277 | android=args.android, adb_prefix=adb_prefix) |
| 278 | degraded_file = _GetFile(degraded_file, out_dir, move=True, |
| 279 | android=args.android, adb_prefix=adb_prefix) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 280 | |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 281 | analyzer_results = analyzer.func(analyzer.executable, |
| 282 | reference_file, degraded_file) |
| 283 | for metric, (value, units) in analyzer_results.items(): |
| 284 | # Output a result for the perf dashboard. |
| 285 | print 'RESULT %s: %s= %s %s' % (metric, test_name, value, units) |
| 286 | _AddChart(charts, metric, test_name, value, units) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 287 | |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 288 | if args.remove: |
| 289 | os.remove(reference_file) |
| 290 | os.remove(degraded_file) |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 291 | finally: |
Mirko Bonadei | 4876cb2 | 2019-07-09 20:08:55 +0000 | [diff] [blame] | 292 | test_process.terminate() |
Artem Titov | b1f2d60 | 2019-07-10 14:40:58 +0200 | [diff] [blame] | 293 | if perf_results_file: |
| 294 | perf_results_file = _GetFile(perf_results_file, out_dir, move=True, |
| 295 | android=args.android, adb_prefix=adb_prefix) |
| 296 | _AddRunPerfResults(charts, perf_results_file) |
| 297 | if args.remove: |
| 298 | os.remove(perf_results_file) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 299 | |
Edward Lemur | ed7b4ff | 2018-02-01 17:23:58 +0100 | [diff] [blame] | 300 | if args.isolated_script_test_perf_output: |
| 301 | with open(args.isolated_script_test_perf_output, 'w') as f: |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 302 | json.dump({"format_version": "1.0", "charts": charts}, f) |
| 303 | |
Oleh Prypin | 637b0b5 | 2018-09-21 17:16:06 +0200 | [diff] [blame] | 304 | if args.isolated_script_test_output: |
| 305 | with open(args.isolated_script_test_output, 'w') as f: |
| 306 | json.dump({"version": 3}, f) |
| 307 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 308 | return test_process.wait() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 309 | |
| 310 | |
| 311 | if __name__ == '__main__': |
| 312 | sys.exit(main()) |