| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 # | 3 # |
| 4 # Use of this source code is governed by a BSD-style license | 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 | 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 | 6 # tree. An additional intellectual property rights grant can be found |
| 7 # in the file PATENTS. All contributing project authors may | 7 # in the file PATENTS. All contributing project authors may |
| 8 # be found in the AUTHORS file in the root of the source tree. | 8 # be found in the AUTHORS file in the root of the source tree. |
| 9 | 9 |
| 10 """ | 10 """ |
| 11 This script is the wrapper that runs the low-bandwidth audio test. | 11 This script is the wrapper that runs the low-bandwidth audio test. |
| 12 | 12 |
| 13 After running the test, post-process steps for calculating audio quality of the | 13 After running the test, post-process steps for calculating audio quality of the |
| 14 output files will be performed. | 14 output files will be performed. |
| 15 """ | 15 """ |
| 16 | 16 |
| 17 import argparse | 17 import argparse |
| 18 import logging | 18 import logging |
| 19 import os | 19 import os |
| 20 import re | 20 import re |
| 21 import shutil |
| 21 import subprocess | 22 import subprocess |
| 22 import sys | 23 import sys |
| 23 | 24 |
| 24 | 25 |
| 25 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 26 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 26 SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir, | 27 SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir, |
| 27 os.pardir)) | 28 os.pardir)) |
| 28 | 29 |
| 29 | 30 |
| 30 def _LogCommand(command): | 31 def _LogCommand(command): |
| 31 logging.info('Running %r', command) | 32 logging.info('Running %r', command) |
| 32 return command | 33 return command |
| 33 | 34 |
| 34 | 35 |
| 35 def _ParseArgs(): | 36 def _ParseArgs(): |
| 36 parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.') | 37 parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.') |
| 37 parser.add_argument('build_dir', | 38 parser.add_argument('build_dir', |
| 38 help='Path to the build directory (e.g. out/Release).') | 39 help='Path to the build directory (e.g. out/Release).') |
| 39 parser.add_argument('--remove', action='store_true', | 40 parser.add_argument('--remove', action='store_true', |
| 40 help='Remove output audio files after testing.') | 41 help='Remove output audio files after testing.') |
| 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') |
| 41 args = parser.parse_args() | 45 args = parser.parse_args() |
| 42 return args | 46 return args |
| 43 | 47 |
| 44 | 48 |
| 45 def _GetPlatform(): | 49 def _GetPlatform(): |
| 46 if sys.platform == 'win32': | 50 if sys.platform == 'win32': |
| 47 return 'win' | 51 return 'win' |
| 48 elif sys.platform == 'darwin': | 52 elif sys.platform == 'darwin': |
| 49 return 'mac' | 53 return 'mac' |
| 50 elif sys.platform.startswith('linux'): | 54 elif sys.platform.startswith('linux'): |
| 51 return 'linux' | 55 return 'linux' |
| 52 | 56 |
| 53 | 57 |
| 54 def _GetExecutableExtension(): | |
| 55 if sys.platform == 'win32': | |
| 56 return '.exe' | |
| 57 else: | |
| 58 return '' | |
| 59 | |
| 60 | |
| 61 def _DownloadTools(): | 58 def _DownloadTools(): |
| 62 tools_dir = os.path.join(SRC_DIR, 'tools-webrtc') | 59 tools_dir = os.path.join(SRC_DIR, 'tools-webrtc') |
| 63 toolchain_dir = os.path.join(tools_dir, 'audio_quality') | 60 toolchain_dir = os.path.join(tools_dir, 'audio_quality') |
| 64 | 61 |
| 65 # Download pesq. | 62 # Download pesq. |
| 66 download_script = os.path.join(tools_dir, 'download_tools.py') | 63 download_script = os.path.join(tools_dir, 'download_tools.py') |
| 67 command = [sys.executable, download_script, toolchain_dir] | 64 command = [sys.executable, download_script, toolchain_dir] |
| 68 subprocess.check_call(_LogCommand(command)) | 65 subprocess.check_call(_LogCommand(command)) |
| 69 | 66 |
| 70 pesq_path = os.path.join(toolchain_dir, _GetPlatform(), | 67 pesq_path = os.path.join(toolchain_dir, _GetPlatform(), 'pesq') |
| 71 'pesq' + _GetExecutableExtension()) | |
| 72 return pesq_path | 68 return pesq_path |
| 73 | 69 |
| 74 | 70 |
| 71 def _GetFile(file_path, out_dir, android=False, adb_path=None): |
| 72 out_file_name = os.path.basename(file_path) |
| 73 out_file_path = os.path.join(out_dir, out_file_name) |
| 74 |
| 75 if android: |
| 76 # Pull the file from the connected Android device |
| 77 adb_command = [adb_path, 'pull', file_path, out_dir] |
| 78 subprocess.check_call(_LogCommand(adb_command)) |
| 79 elif os.path.abspath(file_path) != os.path.abspath(out_file_path): |
| 80 shutil.copy(file_path, out_file_path) |
| 81 |
| 82 return out_file_path |
| 83 |
| 84 |
| 75 def main(): | 85 def main(): |
| 76 # pylint: disable=W0101 | 86 # pylint: disable=W0101 |
| 77 logging.basicConfig(level=logging.INFO) | 87 logging.basicConfig(level=logging.INFO) |
| 78 | 88 |
| 79 args = _ParseArgs() | 89 args = _ParseArgs() |
| 80 | 90 |
| 81 pesq_path = _DownloadTools() | 91 pesq_path = _DownloadTools() |
| 82 | 92 |
| 83 test_executable_path = os.path.join(args.build_dir, | 93 out_dir = os.path.join(args.build_dir, '..') |
| 84 'low_bandwidth_audio_test' + _GetExecutableExtension()) | 94 if args.android: |
| 95 test_command = [os.path.join(args.build_dir, 'bin', |
| 96 'run_low_bandwidth_audio_test'), '-v'] |
| 97 else: |
| 98 test_command = [os.path.join(args.build_dir, 'low_bandwidth_audio_test')] |
| 85 | 99 |
| 86 # Start the test executable that produces audio files. | 100 # Start the test executable that produces audio files. |
| 87 command = [test_executable_path] | 101 test_process = subprocess.Popen(_LogCommand(test_command), |
| 88 test_process = subprocess.Popen(_LogCommand(command), stdout=subprocess.PIPE) | 102 stdout=subprocess.PIPE) |
| 89 | 103 |
| 90 for line in iter(test_process.stdout.readline, ''): | 104 for line in iter(test_process.stdout.readline, ''): |
| 91 # Echo the output to screen. | 105 # Echo the output to screen. |
| 92 sys.stdout.write(line) | 106 sys.stdout.write(line) |
| 93 | 107 |
| 94 # Extract specific lines that contain information about produced files. | 108 # Extract specific lines that contain information about produced files. |
| 95 match = re.search(r'^TEST (\w+) ([^:]+?):([^:]+?)\n?$', line) | 109 # Output from Android has a prefix, need to skip it. |
| 110 match = re.search(r'^(?:I\b.+\b)?TEST (\w+) ([^ ]+?) ([^ ]+?)\s*$', line) |
| 96 if not match: | 111 if not match: |
| 97 continue | 112 continue |
| 98 test_name, reference_file, degraded_file = match.groups() | 113 test_name = match.group(1) |
| 114 reference_file = _GetFile(match.group(2), out_dir, |
| 115 args.android, args.adb_path) |
| 116 degraded_file = _GetFile(match.group(3), out_dir, |
| 117 args.android, args.adb_path) |
| 99 | 118 |
| 100 # Analyze audio | 119 # Analyze audio. |
| 101 command = [pesq_path, '+16000', reference_file, degraded_file] | 120 pesq_command = [pesq_path, '+16000', |
| 102 pesq_output = subprocess.check_output(_LogCommand(command)) | 121 os.path.basename(reference_file), |
| 103 | 122 os.path.basename(degraded_file)] |
| 104 if args.remove: | 123 # Need to provide paths in the current directory due to a bug in PESQ: |
| 105 os.remove(degraded_file) | 124 # On Mac, for some 'path/to/file.wav', if 'file.wav' is longer than |
| 125 # 'path/to', PESQ crashes. |
| 126 pesq_output = subprocess.check_output(_LogCommand(pesq_command), |
| 127 cwd=out_dir) |
| 106 | 128 |
| 107 # Find the scores in stdout of pesq. | 129 # Find the scores in stdout of pesq. |
| 108 match = re.search( | 130 match = re.search( |
| 109 r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', | 131 r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', |
| 110 pesq_output) | 132 pesq_output) |
| 111 if match: | 133 if match: |
| 112 raw_mos, _ = match.groups() | 134 raw_mos, _ = match.groups() |
| 113 | 135 |
| 114 # Output a result for the perf dashboard. | 136 # Output a result for the perf dashboard. |
| 115 print 'RESULT pesq_mos: %s= %s score' % (test_name, raw_mos) | 137 print 'RESULT pesq_mos: %s= %s score' % (test_name, raw_mos) |
| 116 else: | 138 else: |
| 117 logging.error('PESQ: %s', pesq_output.splitlines()[-1]) | 139 logging.error('PESQ: %s', pesq_output.splitlines()[-1]) |
| 118 | 140 |
| 141 if args.remove: |
| 142 os.remove(reference_file) |
| 143 os.remove(degraded_file) |
| 144 |
| 119 return test_process.wait() | 145 return test_process.wait() |
| 120 | 146 |
| 121 | 147 |
| 122 if __name__ == '__main__': | 148 if __name__ == '__main__': |
| 123 sys.exit(main()) | 149 sys.exit(main()) |
| OLD | NEW |