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.') |
Edward Lemur | ed7b4ff | 2018-02-01 17:23:58 +0100 | [diff] [blame] | 60 | parser.add_argument('--isolated-script-test-perf-output', |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 61 | help='Where to store perf results in chartjson format.', default=None) |
Edward Lemur | 7e3b569 | 2017-10-04 17:03:16 +0200 | [diff] [blame] | 62 | |
| 63 | # Ignore Chromium-specific flags |
| 64 | parser.add_argument('--isolated-script-test-output', |
| 65 | type=str, default=None) |
Edward Lemur | d8b041c | 2018-01-16 14:30:28 +0100 | [diff] [blame] | 66 | parser.add_argument('--test-launcher-summary-output', |
| 67 | type=str, default=None) |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 68 | args = parser.parse_args() |
Edward Lemur | 7e3b569 | 2017-10-04 17:03:16 +0200 | [diff] [blame] | 69 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 70 | return args |
| 71 | |
| 72 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 73 | def _GetPlatform(): |
| 74 | if sys.platform == 'win32': |
| 75 | return 'win' |
| 76 | elif sys.platform == 'darwin': |
| 77 | return 'mac' |
| 78 | elif sys.platform.startswith('linux'): |
| 79 | return 'linux' |
| 80 | |
| 81 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 82 | def _GetExtension(): |
| 83 | return '.exe' if sys.platform == 'win32' else '' |
| 84 | |
| 85 | |
| 86 | def _GetPathToTools(): |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 87 | tools_dir = os.path.join(SRC_DIR, 'tools_webrtc') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 88 | toolchain_dir = os.path.join(tools_dir, 'audio_quality') |
| 89 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 90 | platform = _GetPlatform() |
| 91 | ext = _GetExtension() |
Edward Lemur | bb1222f | 2017-10-03 12:33:38 +0000 | [diff] [blame] | 92 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 93 | pesq_path = os.path.join(toolchain_dir, platform, 'pesq' + ext) |
| 94 | if not os.path.isfile(pesq_path): |
| 95 | pesq_path = None |
| 96 | |
| 97 | polqa_path = os.path.join(toolchain_dir, platform, 'PolqaOem64' + ext) |
| 98 | if not os.path.isfile(polqa_path): |
| 99 | polqa_path = None |
| 100 | |
| 101 | if (platform != 'mac' and not polqa_path) or not pesq_path: |
| 102 | logging.error(NO_TOOLS_ERROR_MESSAGE, |
| 103 | toolchain_dir, |
| 104 | os.path.join(tools_dir, 'download_tools.py'), |
| 105 | toolchain_dir) |
| 106 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 107 | return pesq_path, polqa_path |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 108 | |
| 109 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 110 | def ExtractTestRuns(lines, echo=False): |
| 111 | """Extracts information about tests from the output of a test runner. |
| 112 | |
| 113 | Produces tuples (android_device, test_name, reference_file, degraded_file). |
| 114 | """ |
| 115 | for line in lines: |
| 116 | if echo: |
| 117 | sys.stdout.write(line) |
| 118 | |
| 119 | # Output from Android has a prefix with the device name. |
| 120 | android_prefix_re = r'(?:I\b.+\brun_tests_on_device\((.+?)\)\s*)?' |
| 121 | test_re = r'^' + android_prefix_re + r'TEST (\w+) ([^ ]+?) ([^ ]+?)\s*$' |
| 122 | |
| 123 | match = re.search(test_re, line) |
| 124 | if match: |
| 125 | yield match.groups() |
| 126 | |
| 127 | |
| 128 | def _GetFile(file_path, out_dir, move=False, |
| 129 | android=False, adb_prefix=('adb',)): |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 130 | out_file_name = os.path.basename(file_path) |
| 131 | out_file_path = os.path.join(out_dir, out_file_name) |
| 132 | |
| 133 | if android: |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 134 | # Pull the file from the connected Android device. |
| 135 | adb_command = adb_prefix + ('pull', file_path, out_dir) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 136 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 137 | if move: |
| 138 | # Remove that file. |
| 139 | adb_command = adb_prefix + ('shell', 'rm', file_path) |
| 140 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 141 | elif os.path.abspath(file_path) != os.path.abspath(out_file_path): |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame] | 142 | if move: |
| 143 | shutil.move(file_path, out_file_path) |
| 144 | else: |
| 145 | shutil.copy(file_path, out_file_path) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 146 | |
| 147 | return out_file_path |
| 148 | |
| 149 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 150 | def _RunPesq(executable_path, reference_file, degraded_file, |
| 151 | sample_rate_hz=16000): |
| 152 | directory = os.path.dirname(reference_file) |
| 153 | assert os.path.dirname(degraded_file) == directory |
| 154 | |
| 155 | # Analyze audio. |
| 156 | command = [executable_path, '+%d' % sample_rate_hz, |
| 157 | os.path.basename(reference_file), |
| 158 | os.path.basename(degraded_file)] |
| 159 | # Need to provide paths in the current directory due to a bug in PESQ: |
| 160 | # On Mac, for some 'path/to/file.wav', if 'file.wav' is longer than |
| 161 | # 'path/to', PESQ crashes. |
| 162 | out = subprocess.check_output(_LogCommand(command), |
| 163 | cwd=directory, stderr=subprocess.STDOUT) |
| 164 | |
| 165 | # Find the scores in stdout of PESQ. |
| 166 | match = re.search( |
| 167 | r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', out) |
| 168 | if match: |
| 169 | raw_mos, _ = match.groups() |
| 170 | |
| 171 | return {'pesq_mos': (raw_mos, 'score')} |
| 172 | else: |
| 173 | logging.error('PESQ: %s', out.splitlines()[-1]) |
| 174 | return {} |
| 175 | |
| 176 | |
| 177 | def _RunPolqa(executable_path, reference_file, degraded_file): |
| 178 | # Analyze audio. |
| 179 | command = [executable_path, '-q', '-LC', 'NB', |
| 180 | '-Ref', reference_file, '-Test', degraded_file] |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 181 | process = subprocess.Popen(_LogCommand(command), |
| 182 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 183 | out, err = process.communicate() |
| 184 | |
| 185 | # Find the scores in stdout of POLQA. |
| 186 | match = re.search(r'\bMOS-LQO:\s+([\d.]+)', out) |
| 187 | |
| 188 | if process.returncode != 0 or not match: |
| 189 | if process.returncode == 2: |
| 190 | logging.warning('%s (2)', err.strip()) |
| 191 | logging.warning('POLQA license error, skipping test.') |
| 192 | else: |
| 193 | logging.error('%s (%d)', err.strip(), process.returncode) |
| 194 | return {} |
| 195 | |
| 196 | mos_lqo, = match.groups() |
| 197 | return {'polqa_mos_lqo': (mos_lqo, 'score')} |
| 198 | |
| 199 | |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 200 | def _AddChart(charts, metric, test_name, value, units): |
| 201 | chart = charts.setdefault(metric, {}) |
| 202 | chart[test_name] = { |
| 203 | "type": "scalar", |
| 204 | "value": value, |
| 205 | "units": units, |
| 206 | } |
| 207 | |
| 208 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 209 | Analyzer = collections.namedtuple('Analyzer', ['func', 'executable', |
| 210 | 'sample_rate_hz']) |
| 211 | |
| 212 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 213 | def main(): |
| 214 | # pylint: disable=W0101 |
| 215 | logging.basicConfig(level=logging.INFO) |
| 216 | |
| 217 | args = _ParseArgs() |
| 218 | |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 219 | pesq_path, polqa_path = _GetPathToTools() |
| 220 | if pesq_path is None: |
| 221 | return 1 |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 222 | |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 223 | out_dir = os.path.join(args.build_dir, '..') |
| 224 | if args.android: |
| 225 | test_command = [os.path.join(args.build_dir, 'bin', |
Edward Lesmes | 5b9c684 | 2018-03-09 13:07:22 -0500 | [diff] [blame] | 226 | 'run_low_bandwidth_audio_test'), |
| 227 | '-v', '--num-retries', args.num_retries] |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 228 | else: |
| 229 | test_command = [os.path.join(args.build_dir, 'low_bandwidth_audio_test')] |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 230 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 231 | analyzers = [Analyzer(_RunPesq, pesq_path, 16000)] |
| 232 | # Check if POLQA can run at all, or skip the 48 kHz tests entirely. |
| 233 | example_path = os.path.join(SRC_DIR, 'resources', |
| 234 | 'voice_engine', 'audio_tiny48.wav') |
Edward Lemur | b0250f0 | 2017-10-04 14:41:17 +0200 | [diff] [blame] | 235 | if polqa_path and _RunPolqa(polqa_path, example_path, example_path): |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 236 | analyzers.append(Analyzer(_RunPolqa, polqa_path, 48000)) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 237 | |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 238 | charts = {} |
| 239 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 240 | for analyzer in analyzers: |
| 241 | # Start the test executable that produces audio files. |
| 242 | test_process = subprocess.Popen( |
| 243 | _LogCommand(test_command + ['--sample_rate_hz=%d' % |
| 244 | analyzer.sample_rate_hz]), |
oprypin | 4f1f458 | 2017-06-14 09:35:11 -0700 | [diff] [blame] | 245 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 246 | try: |
| 247 | lines = iter(test_process.stdout.readline, '') |
| 248 | for result in ExtractTestRuns(lines, echo=True): |
| 249 | (android_device, test_name, reference_file, degraded_file) = result |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 250 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 251 | adb_prefix = (args.adb_path,) |
| 252 | if android_device: |
| 253 | adb_prefix += ('-s', android_device) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 254 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 255 | reference_file = _GetFile(reference_file, out_dir, |
| 256 | android=args.android, adb_prefix=adb_prefix) |
| 257 | degraded_file = _GetFile(degraded_file, out_dir, move=True, |
| 258 | android=args.android, adb_prefix=adb_prefix) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 259 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 260 | analyzer_results = analyzer.func(analyzer.executable, |
| 261 | reference_file, degraded_file) |
| 262 | for metric, (value, units) in analyzer_results.items(): |
| 263 | # Output a result for the perf dashboard. |
| 264 | print 'RESULT %s: %s= %s %s' % (metric, test_name, value, units) |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 265 | _AddChart(charts, metric, test_name, value, units) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 266 | |
oprypin | f250100 | 2017-04-12 05:00:56 -0700 | [diff] [blame] | 267 | if args.remove: |
| 268 | os.remove(reference_file) |
| 269 | os.remove(degraded_file) |
| 270 | finally: |
| 271 | test_process.terminate() |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 272 | |
Edward Lemur | ed7b4ff | 2018-02-01 17:23:58 +0100 | [diff] [blame] | 273 | if args.isolated_script_test_perf_output: |
| 274 | with open(args.isolated_script_test_perf_output, 'w') as f: |
Edward Lemur | b401771 | 2018-01-15 14:21:09 +0100 | [diff] [blame] | 275 | json.dump({"format_version": "1.0", "charts": charts}, f) |
| 276 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 277 | return test_process.wait() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 278 | |
| 279 | |
| 280 | if __name__ == '__main__': |
| 281 | sys.exit(main()) |