blob: 07065e2c8d265ae1525a4120d329b3b14f8c6a78 [file] [log] [blame]
Jeremy Leconte1d4e9822022-01-28 16:19:39 +01001#!/usr/bin/env vpython3
kjellander8f8d1a02017-03-06 04:01:16 -08002# 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.
kjellander8f8d1a02017-03-06 04:01:16 -08009"""
10This script is the wrapper that runs the low-bandwidth audio test.
11
12After running the test, post-process steps for calculating audio quality of the
13output files will be performed.
14"""
15
16import argparse
oprypinf2501002017-04-12 05:00:56 -070017import collections
Jeremy Leconte994bf452022-01-12 10:51:16 +010018import json
kjellander8f8d1a02017-03-06 04:01:16 -080019import logging
20import os
oprypin92220ff2017-03-23 03:40:03 -070021import re
oprypin6d305ba2017-03-30 04:01:30 -070022import shutil
kjellander8f8d1a02017-03-06 04:01:16 -080023import subprocess
24import sys
25
kjellander8f8d1a02017-03-06 04:01:16 -080026SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
Henrik Kjellander5a6aa4f2017-09-15 09:31:54 +020027SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
kjellander8f8d1a02017-03-06 04:01:16 -080028
Edward Lemurb0250f02017-10-04 14:41:17 +020029NO_TOOLS_ERROR_MESSAGE = (
Mirko Bonadei8cc66952020-10-30 10:13:45 +010030 'Could not find PESQ or POLQA at %s.\n'
31 '\n'
32 'To fix this run:\n'
33 ' python %s %s\n'
34 '\n'
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010035 'Note that these tools are Google-internal due to licensing, so in order '
36 'to use them you will have to get your own license and manually put them '
37 'in the right location.\n'
Mirko Bonadei8cc66952020-10-30 10:13:45 +010038 'See https://cs.chromium.org/chromium/src/third_party/webrtc/tools_webrtc/'
39 'download_tools.py?rcl=bbceb76f540159e2dba0701ac03c514f01624130&l=13')
Edward Lemurb0250f02017-10-04 14:41:17 +020040
kjellander8f8d1a02017-03-06 04:01:16 -080041
oprypin92220ff2017-03-23 03:40:03 -070042def _LogCommand(command):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010043 logging.info('Running %r', command)
44 return command
kjellander8f8d1a02017-03-06 04:01:16 -080045
46
47def _ParseArgs():
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010048 parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.')
49 parser.add_argument('build_dir',
50 help='Path to the build directory (e.g. out/Release).')
51 parser.add_argument('--remove',
52 action='store_true',
53 help='Remove output audio files after testing.')
54 parser.add_argument(
55 '--android',
56 action='store_true',
57 help='Perform the test on a connected Android device instead.')
58 parser.add_argument('--adb-path', help='Path to adb binary.', default='adb')
59 parser.add_argument('--num-retries',
60 default='0',
61 help='Number of times to retry the test on Android.')
62 parser.add_argument(
63 '--isolated-script-test-perf-output',
64 default=None,
65 help='Path to store perf results in histogram proto format.')
Jeremy Leconte994bf452022-01-12 10:51:16 +010066 parser.add_argument(
67 '--isolated-script-test-output',
68 default=None,
69 help='Path to output an empty JSON file which Chromium infra requires.')
Edward Lemur7e3b5692017-10-04 17:03:16 +020070
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +010071 return parser.parse_known_args()
kjellander8f8d1a02017-03-06 04:01:16 -080072
73
oprypin92220ff2017-03-23 03:40:03 -070074def _GetPlatform():
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010075 if sys.platform == 'win32':
76 return 'win'
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +010077 if sys.platform == 'darwin':
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010078 return 'mac'
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +010079 if sys.platform.startswith('linux'):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010080 return 'linux'
81 raise AssertionError('Unknown platform %s' % sys.platform)
oprypin92220ff2017-03-23 03:40:03 -070082
83
Edward Lemurb0250f02017-10-04 14:41:17 +020084def _GetExtension():
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010085 return '.exe' if sys.platform == 'win32' else ''
Edward Lemurb0250f02017-10-04 14:41:17 +020086
87
88def _GetPathToTools():
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010089 tools_dir = os.path.join(SRC_DIR, 'tools_webrtc')
90 toolchain_dir = os.path.join(tools_dir, 'audio_quality')
oprypin92220ff2017-03-23 03:40:03 -070091
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010092 platform = _GetPlatform()
93 ext = _GetExtension()
Edward Lemurbb1222f2017-10-03 12:33:38 +000094
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010095 pesq_path = os.path.join(toolchain_dir, platform, 'pesq' + ext)
96 if not os.path.isfile(pesq_path):
97 pesq_path = None
Edward Lemurb0250f02017-10-04 14:41:17 +020098
Jeremy Lecontef22c78b2021-12-07 19:49:48 +010099 polqa_path = os.path.join(toolchain_dir, platform, 'PolqaOem64' + ext)
100 if not os.path.isfile(polqa_path):
101 polqa_path = None
Edward Lemurb0250f02017-10-04 14:41:17 +0200102
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100103 if (platform != 'mac' and not polqa_path) or not pesq_path:
104 logging.error(NO_TOOLS_ERROR_MESSAGE, toolchain_dir,
105 os.path.join(tools_dir, 'download_tools.py'), toolchain_dir)
Edward Lemurb0250f02017-10-04 14:41:17 +0200106
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100107 return pesq_path, polqa_path
oprypin92220ff2017-03-23 03:40:03 -0700108
109
oprypinabd101b2017-04-06 23:21:30 -0700110def ExtractTestRuns(lines, echo=False):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100111 """Extracts information about tests from the output of a test runner.
oprypinabd101b2017-04-06 23:21:30 -0700112
Artem Titovb1f2d602019-07-10 14:40:58 +0200113 Produces tuples
114 (android_device, test_name, reference_file, degraded_file, cur_perf_results).
oprypinabd101b2017-04-06 23:21:30 -0700115 """
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100116 for line in lines:
117 if echo:
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100118 sys.stdout.write(line)
oprypinabd101b2017-04-06 23:21:30 -0700119
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100120 # Output from Android has a prefix with the device name.
121 android_prefix_re = r'(?:I\b.+\brun_tests_on_device\((.+?)\)\s*)?'
122 test_re = r'^' + android_prefix_re + (r'TEST (\w+) ([^ ]+?) ([^\s]+)'
123 r' ?([^\s]+)?\s*$')
oprypinabd101b2017-04-06 23:21:30 -0700124
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100125 match = re.search(test_re, line)
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100126 if match:
127 yield match.groups()
oprypinabd101b2017-04-06 23:21:30 -0700128
129
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100130def _GetFile(file_path,
131 out_dir,
132 move=False,
133 android=False,
134 adb_prefix=('adb', )):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100135 out_file_name = os.path.basename(file_path)
136 out_file_path = os.path.join(out_dir, out_file_name)
oprypin6d305ba2017-03-30 04:01:30 -0700137
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100138 if android:
139 # Pull the file from the connected Android device.
140 adb_command = adb_prefix + ('pull', file_path, out_dir)
141 subprocess.check_call(_LogCommand(adb_command))
142 if move:
143 # Remove that file.
144 adb_command = adb_prefix + ('shell', 'rm', file_path)
145 subprocess.check_call(_LogCommand(adb_command))
146 elif os.path.abspath(file_path) != os.path.abspath(out_file_path):
147 if move:
148 shutil.move(file_path, out_file_path)
149 else:
150 shutil.copy(file_path, out_file_path)
oprypin6d305ba2017-03-30 04:01:30 -0700151
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100152 return out_file_path
oprypin6d305ba2017-03-30 04:01:30 -0700153
154
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100155def _RunPesq(executable_path,
156 reference_file,
157 degraded_file,
oprypinf2501002017-04-12 05:00:56 -0700158 sample_rate_hz=16000):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100159 directory = os.path.dirname(reference_file)
160 assert os.path.dirname(degraded_file) == directory
oprypinf2501002017-04-12 05:00:56 -0700161
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100162 # Analyze audio.
163 command = [
164 executable_path,
165 '+%d' % sample_rate_hz,
166 os.path.basename(reference_file),
167 os.path.basename(degraded_file)
168 ]
169 # Need to provide paths in the current directory due to a bug in PESQ:
170 # On Mac, for some 'path/to/file.wav', if 'file.wav' is longer than
171 # 'path/to', PESQ crashes.
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100172 out = subprocess.check_output(_LogCommand(command),
173 cwd=directory,
174 universal_newlines=True,
175 stderr=subprocess.STDOUT)
oprypinf2501002017-04-12 05:00:56 -0700176
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100177 # Find the scores in stdout of PESQ.
178 match = re.search(
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100179 r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', out)
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100180 if match:
181 raw_mos, _ = match.groups()
182 return {'pesq_mos': (raw_mos, 'unitless')}
183 logging.error('PESQ: %s', out.splitlines()[-1])
184 return {}
oprypinf2501002017-04-12 05:00:56 -0700185
186
187def _RunPolqa(executable_path, reference_file, degraded_file):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100188 # Analyze audio.
189 command = [
190 executable_path, '-q', '-LC', 'NB', '-Ref', reference_file, '-Test',
191 degraded_file
192 ]
193 process = subprocess.Popen(_LogCommand(command),
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100194 universal_newlines=True,
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100195 stdout=subprocess.PIPE,
196 stderr=subprocess.PIPE)
197 out, err = process.communicate()
oprypinf2501002017-04-12 05:00:56 -0700198
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100199 # Find the scores in stdout of POLQA.
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100200 match = re.search(r'\bMOS-LQO:\s+([\d.]+)', out)
oprypinf2501002017-04-12 05:00:56 -0700201
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100202 if process.returncode != 0 or not match:
203 if process.returncode == 2:
204 logging.warning('%s (2)', err.strip())
205 logging.warning('POLQA license error, skipping test.')
206 else:
207 logging.error('%s (%d)', err.strip(), process.returncode)
208 return {}
oprypinf2501002017-04-12 05:00:56 -0700209
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100210 mos_lqo, = match.groups()
211 return {'polqa_mos_lqo': (mos_lqo, 'unitless')}
oprypinf2501002017-04-12 05:00:56 -0700212
213
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100214def _MergeInPerfResultsFromCcTests(histograms, run_perf_results_file):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100215 from tracing.value import histogram_set
Edward Lemurb4017712018-01-15 14:21:09 +0100216
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100217 cc_histograms = histogram_set.HistogramSet()
218 with open(run_perf_results_file, 'rb') as f:
219 contents = f.read()
220 if not contents:
221 return
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100222
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100223 cc_histograms.ImportProto(contents)
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100224
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100225 histograms.Merge(cc_histograms)
Artem Titovb1f2d602019-07-10 14:40:58 +0200226
227
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100228Analyzer = collections.namedtuple(
229 'Analyzer', ['name', 'func', 'executable', 'sample_rate_hz'])
oprypinf2501002017-04-12 05:00:56 -0700230
231
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100232def _ConfigurePythonPath(args):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100233 script_dir = os.path.dirname(os.path.realpath(__file__))
234 checkout_root = os.path.abspath(os.path.join(script_dir, os.pardir,
235 os.pardir))
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100236
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100237 # TODO(https://crbug.com/1029452): Use a copy rule and add these from the
238 # out dir like for the third_party/protobuf code.
239 sys.path.insert(
240 0, os.path.join(checkout_root, 'third_party', 'catapult', 'tracing'))
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100241
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100242 # The low_bandwidth_audio_perf_test gn rule will build the protobuf stub
243 # for python, so put it in the path for this script before we attempt to
244 # import it.
245 histogram_proto_path = os.path.join(os.path.abspath(args.build_dir),
246 'pyproto', 'tracing', 'tracing', 'proto')
247 sys.path.insert(0, histogram_proto_path)
248 proto_stub_path = os.path.join(os.path.abspath(args.build_dir), 'pyproto')
249 sys.path.insert(0, proto_stub_path)
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100250
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100251 # Fail early in case the proto hasn't been built.
252 try:
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +0100253 #pylint: disable=unused-import
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100254 import histogram_pb2
255 except ImportError as e:
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100256 raise ImportError('Could not import histogram_pb2. You need to build the '
257 'low_bandwidth_audio_perf_test target before invoking '
258 'this script. Expected to find '
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +0100259 'histogram_pb2.py in %s.' % histogram_proto_path) from e
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100260
261
kjellander8f8d1a02017-03-06 04:01:16 -0800262def main():
Mirko Bonadeic6206652022-02-01 18:52:49 +0100263 logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
264 level=logging.INFO,
265 datefmt='%Y-%m-%d %H:%M:%S')
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100266 logging.info('Invoked with %s', str(sys.argv))
kjellander8f8d1a02017-03-06 04:01:16 -0800267
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +0100268 args, extra_test_args = _ParseArgs()
kjellander8f8d1a02017-03-06 04:01:16 -0800269
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100270 _ConfigurePythonPath(args)
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100271
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100272 # Import catapult modules here after configuring the pythonpath.
273 from tracing.value import histogram_set
274 from tracing.value.diagnostics import reserved_infos
275 from tracing.value.diagnostics import generic_set
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100276
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100277 pesq_path, polqa_path = _GetPathToTools()
278 if pesq_path is None:
279 return 1
kjellander8f8d1a02017-03-06 04:01:16 -0800280
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100281 out_dir = os.path.join(args.build_dir, '..')
282 if args.android:
283 test_command = [
284 os.path.join(args.build_dir, 'bin', 'run_low_bandwidth_audio_test'),
285 '-v', '--num-retries', args.num_retries
286 ]
287 else:
288 test_command = [os.path.join(args.build_dir, 'low_bandwidth_audio_test')]
oprypin92220ff2017-03-23 03:40:03 -0700289
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100290 analyzers = [Analyzer('pesq', _RunPesq, pesq_path, 16000)]
291 # Check if POLQA can run at all, or skip the 48 kHz tests entirely.
292 example_path = os.path.join(SRC_DIR, 'resources', 'voice_engine',
293 'audio_tiny48.wav')
294 if polqa_path and _RunPolqa(polqa_path, example_path, example_path):
295 analyzers.append(Analyzer('polqa', _RunPolqa, polqa_path, 48000))
oprypin92220ff2017-03-23 03:40:03 -0700296
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100297 histograms = histogram_set.HistogramSet()
298 for analyzer in analyzers:
299 # Start the test executable that produces audio files.
300 test_process = subprocess.Popen(_LogCommand(test_command + [
301 '--sample_rate_hz=%d' % analyzer.sample_rate_hz,
302 '--test_case_prefix=%s' % analyzer.name,
Jeremy Leconte7b0a30e2022-02-22 09:53:41 +0100303 ] + extra_test_args),
Mirko Bonadei4a3e5602022-02-02 16:19:10 +0100304 universal_newlines=True,
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100305 stdout=subprocess.PIPE,
306 stderr=subprocess.STDOUT)
307 perf_results_file = None
308 try:
309 lines = iter(test_process.stdout.readline, '')
310 for result in ExtractTestRuns(lines, echo=True):
311 (android_device, test_name, reference_file, degraded_file,
312 perf_results_file) = result
oprypin92220ff2017-03-23 03:40:03 -0700313
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100314 adb_prefix = (args.adb_path, )
315 if android_device:
316 adb_prefix += ('-s', android_device)
oprypin92220ff2017-03-23 03:40:03 -0700317
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100318 reference_file = _GetFile(reference_file,
319 out_dir,
320 android=args.android,
321 adb_prefix=adb_prefix)
322 degraded_file = _GetFile(degraded_file,
323 out_dir,
324 move=True,
325 android=args.android,
326 adb_prefix=adb_prefix)
oprypin92220ff2017-03-23 03:40:03 -0700327
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100328 analyzer_results = analyzer.func(analyzer.executable, reference_file,
329 degraded_file)
Jeremy Leconte1d4e9822022-01-28 16:19:39 +0100330 for metric, (value, units) in list(analyzer_results.items()):
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100331 hist = histograms.CreateHistogram(metric, units, [value])
332 user_story = generic_set.GenericSet([test_name])
333 hist.diagnostics[reserved_infos.STORIES.name] = user_story
Patrik Höglund3b4bbf52020-03-26 08:41:09 +0100334
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100335 # Output human readable results.
Jeremy Leconte1d4e9822022-01-28 16:19:39 +0100336 print('RESULT %s: %s= %s %s' % (metric, test_name, value, units))
oprypin92220ff2017-03-23 03:40:03 -0700337
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100338 if args.remove:
339 os.remove(reference_file)
340 os.remove(degraded_file)
341 finally:
342 test_process.terminate()
343 if perf_results_file:
344 perf_results_file = _GetFile(perf_results_file,
345 out_dir,
346 move=True,
347 android=args.android,
348 adb_prefix=adb_prefix)
349 _MergeInPerfResultsFromCcTests(histograms, perf_results_file)
350 if args.remove:
351 os.remove(perf_results_file)
oprypin6d305ba2017-03-30 04:01:30 -0700352
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100353 if args.isolated_script_test_perf_output:
354 with open(args.isolated_script_test_perf_output, 'wb') as f:
355 f.write(histograms.AsProto().SerializeToString())
Edward Lemurb4017712018-01-15 14:21:09 +0100356
Jeremy Leconte7a5978e2022-02-22 07:26:33 +0000357 if args.isolated_script_test_output:
358 with open(args.isolated_script_test_output, 'w') as f:
359 json.dump({"version": 3}, f)
360
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100361 return test_process.wait()
kjellander8f8d1a02017-03-06 04:01:16 -0800362
363
364if __name__ == '__main__':
Jeremy Lecontef22c78b2021-12-07 19:49:48 +0100365 sys.exit(main())