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 |
| 18 | import logging |
| 19 | import os |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 20 | import re |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 21 | import shutil |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | |
| 26 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 27 | SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir, |
| 28 | os.pardir)) |
| 29 | |
| 30 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 31 | def _LogCommand(command): |
| 32 | logging.info('Running %r', command) |
| 33 | return command |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def _ParseArgs(): |
| 37 | parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.') |
| 38 | parser.add_argument('build_dir', |
| 39 | help='Path to the build directory (e.g. out/Release).') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 40 | parser.add_argument('--remove', action='store_true', |
| 41 | help='Remove output audio files after testing.') |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 42 | parser.add_argument('--android', action='store_true', |
| 43 | help='Perform the test on a connected Android device instead.') |
| 44 | parser.add_argument('--adb-path', help='Path to adb binary.', default='adb') |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 45 | args = parser.parse_args() |
| 46 | return args |
| 47 | |
| 48 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 49 | def _GetPlatform(): |
| 50 | if sys.platform == 'win32': |
| 51 | return 'win' |
| 52 | elif sys.platform == 'darwin': |
| 53 | return 'mac' |
| 54 | elif sys.platform.startswith('linux'): |
| 55 | return 'linux' |
| 56 | |
| 57 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 58 | def _DownloadTools(): |
| 59 | tools_dir = os.path.join(SRC_DIR, 'tools-webrtc') |
| 60 | toolchain_dir = os.path.join(tools_dir, 'audio_quality') |
| 61 | |
| 62 | # Download pesq. |
| 63 | download_script = os.path.join(tools_dir, 'download_tools.py') |
| 64 | command = [sys.executable, download_script, toolchain_dir] |
| 65 | subprocess.check_call(_LogCommand(command)) |
| 66 | |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 67 | pesq_path = os.path.join(toolchain_dir, _GetPlatform(), 'pesq') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 68 | return pesq_path |
| 69 | |
| 70 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 71 | def ExtractTestRuns(lines, echo=False): |
| 72 | """Extracts information about tests from the output of a test runner. |
| 73 | |
| 74 | Produces tuples (android_device, test_name, reference_file, degraded_file). |
| 75 | """ |
| 76 | for line in lines: |
| 77 | if echo: |
| 78 | sys.stdout.write(line) |
| 79 | |
| 80 | # Output from Android has a prefix with the device name. |
| 81 | android_prefix_re = r'(?:I\b.+\brun_tests_on_device\((.+?)\)\s*)?' |
| 82 | test_re = r'^' + android_prefix_re + r'TEST (\w+) ([^ ]+?) ([^ ]+?)\s*$' |
| 83 | |
| 84 | match = re.search(test_re, line) |
| 85 | if match: |
| 86 | yield match.groups() |
| 87 | |
| 88 | |
| 89 | def _GetFile(file_path, out_dir, move=False, |
| 90 | android=False, adb_prefix=('adb',)): |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 91 | out_file_name = os.path.basename(file_path) |
| 92 | out_file_path = os.path.join(out_dir, out_file_name) |
| 93 | |
| 94 | if android: |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 95 | # Pull the file from the connected Android device. |
| 96 | adb_command = adb_prefix + ('pull', file_path, out_dir) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 97 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 98 | if move: |
| 99 | # Remove that file. |
| 100 | adb_command = adb_prefix + ('shell', 'rm', file_path) |
| 101 | subprocess.check_call(_LogCommand(adb_command)) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 102 | elif os.path.abspath(file_path) != os.path.abspath(out_file_path): |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 103 | if move: |
| 104 | shutil.move(file_path, out_file_path) |
| 105 | else: |
| 106 | shutil.copy(file_path, out_file_path) |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 107 | |
| 108 | return out_file_path |
| 109 | |
| 110 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 111 | def main(): |
| 112 | # pylint: disable=W0101 |
| 113 | logging.basicConfig(level=logging.INFO) |
| 114 | |
| 115 | args = _ParseArgs() |
| 116 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 117 | pesq_path = _DownloadTools() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 118 | |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 119 | out_dir = os.path.join(args.build_dir, '..') |
| 120 | if args.android: |
| 121 | test_command = [os.path.join(args.build_dir, 'bin', |
| 122 | 'run_low_bandwidth_audio_test'), '-v'] |
| 123 | else: |
| 124 | test_command = [os.path.join(args.build_dir, 'low_bandwidth_audio_test')] |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 125 | |
| 126 | # Start the test executable that produces audio files. |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 127 | test_process = subprocess.Popen(_LogCommand(test_command), |
| 128 | stdout=subprocess.PIPE) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 129 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 130 | try: |
| 131 | lines = iter(test_process.stdout.readline, '') |
| 132 | for result in ExtractTestRuns(lines, echo=True): |
| 133 | (android_device, test_name, reference_file, degraded_file) = result |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 134 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 135 | adb_prefix = (args.adb_path,) |
| 136 | if android_device: |
| 137 | adb_prefix += ('-s', android_device) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 138 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 139 | reference_file = _GetFile(reference_file, out_dir, |
| 140 | android=args.android, adb_prefix=adb_prefix) |
| 141 | degraded_file = _GetFile(degraded_file, out_dir, move=True, |
| 142 | android=args.android, adb_prefix=adb_prefix) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 143 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 144 | # Analyze audio. |
| 145 | pesq_command = [pesq_path, '+16000', |
| 146 | os.path.basename(reference_file), |
| 147 | os.path.basename(degraded_file)] |
| 148 | # Need to provide paths in the current directory due to a bug in PESQ: |
| 149 | # On Mac, for some 'path/to/file.wav', if 'file.wav' is longer than |
| 150 | # 'path/to', PESQ crashes. |
| 151 | pesq_output = subprocess.check_output(_LogCommand(pesq_command), |
| 152 | cwd=out_dir) |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 153 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 154 | # Find the scores in stdout of pesq. |
| 155 | match = re.search( |
| 156 | r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', |
| 157 | pesq_output) |
| 158 | if match: |
| 159 | raw_mos, _ = match.groups() |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 160 | |
oprypin | abd101b | 2017-04-06 23:21:30 -0700 | [diff] [blame^] | 161 | # Output a result for the perf dashboard. |
| 162 | print 'RESULT pesq_mos: %s= %s score' % (test_name, raw_mos) |
| 163 | else: |
| 164 | logging.error('PESQ: %s', pesq_output.splitlines()[-1]) |
| 165 | |
| 166 | if args.remove: |
| 167 | os.remove(reference_file) |
| 168 | os.remove(degraded_file) |
| 169 | finally: |
| 170 | test_process.terminate() |
oprypin | 6d305ba | 2017-03-30 04:01:30 -0700 | [diff] [blame] | 171 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame] | 172 | return test_process.wait() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 173 | |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | sys.exit(main()) |