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 |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 21 | import subprocess |
| 22 | import sys |
| 23 | |
| 24 | |
| 25 | 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 | os.pardir)) |
| 28 | |
| 29 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame^] | 30 | def _LogCommand(command): |
| 31 | logging.info('Running %r', command) |
| 32 | return command |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def _ParseArgs(): |
| 36 | parser = argparse.ArgumentParser(description='Run low-bandwidth audio tests.') |
| 37 | parser.add_argument('build_dir', |
| 38 | help='Path to the build directory (e.g. out/Release).') |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame^] | 39 | parser.add_argument('--remove', action='store_true', |
| 40 | help='Remove output audio files after testing.') |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 41 | args = parser.parse_args() |
| 42 | return args |
| 43 | |
| 44 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame^] | 45 | def _GetPlatform(): |
| 46 | if sys.platform == 'win32': |
| 47 | return 'win' |
| 48 | elif sys.platform == 'darwin': |
| 49 | return 'mac' |
| 50 | elif sys.platform.startswith('linux'): |
| 51 | return 'linux' |
| 52 | |
| 53 | |
| 54 | def _GetExecutableExtension(): |
| 55 | if sys.platform == 'win32': |
| 56 | return '.exe' |
| 57 | else: |
| 58 | return '' |
| 59 | |
| 60 | |
| 61 | def _DownloadTools(): |
| 62 | tools_dir = os.path.join(SRC_DIR, 'tools-webrtc') |
| 63 | toolchain_dir = os.path.join(tools_dir, 'audio_quality') |
| 64 | |
| 65 | # Download pesq. |
| 66 | download_script = os.path.join(tools_dir, 'download_tools.py') |
| 67 | command = [sys.executable, download_script, toolchain_dir] |
| 68 | subprocess.check_call(_LogCommand(command)) |
| 69 | |
| 70 | pesq_path = os.path.join(toolchain_dir, _GetPlatform(), |
| 71 | 'pesq' + _GetExecutableExtension()) |
| 72 | return pesq_path |
| 73 | |
| 74 | |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 75 | def main(): |
| 76 | # pylint: disable=W0101 |
| 77 | logging.basicConfig(level=logging.INFO) |
| 78 | |
| 79 | args = _ParseArgs() |
| 80 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame^] | 81 | pesq_path = _DownloadTools() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 82 | |
oprypin | 92220ff | 2017-03-23 03:40:03 -0700 | [diff] [blame^] | 83 | test_executable_path = os.path.join(args.build_dir, |
| 84 | 'low_bandwidth_audio_test' + _GetExecutableExtension()) |
| 85 | |
| 86 | # Start the test executable that produces audio files. |
| 87 | command = [test_executable_path] |
| 88 | test_process = subprocess.Popen(_LogCommand(command), stdout=subprocess.PIPE) |
| 89 | |
| 90 | for line in iter(test_process.stdout.readline, ''): |
| 91 | # Echo the output to screen. |
| 92 | sys.stdout.write(line) |
| 93 | |
| 94 | # Extract specific lines that contain information about produced files. |
| 95 | match = re.search(r'^TEST (\w+) ([^:]+?):([^:]+?)\n?$', line) |
| 96 | if not match: |
| 97 | continue |
| 98 | test_name, reference_file, degraded_file = match.groups() |
| 99 | |
| 100 | # Analyze audio |
| 101 | command = [pesq_path, '+16000', reference_file, degraded_file] |
| 102 | pesq_output = subprocess.check_output(_LogCommand(command)) |
| 103 | |
| 104 | if args.remove: |
| 105 | os.remove(degraded_file) |
| 106 | |
| 107 | # Find the scores in stdout of pesq. |
| 108 | match = re.search( |
| 109 | r'Prediction \(Raw MOS, MOS-LQO\):\s+=\s+([\d.]+)\s+([\d.]+)', |
| 110 | pesq_output) |
| 111 | if match: |
| 112 | raw_mos, _ = match.groups() |
| 113 | |
| 114 | # Output a result for the perf dashboard. |
| 115 | print 'RESULT pesq_mos: %s= %s score' % (test_name, raw_mos) |
| 116 | else: |
| 117 | logging.error('PESQ: %s', pesq_output.splitlines()[-1]) |
| 118 | |
| 119 | return test_process.wait() |
kjellander | 8f8d1a0 | 2017-03-06 04:01:16 -0800 | [diff] [blame] | 120 | |
| 121 | |
| 122 | if __name__ == '__main__': |
| 123 | sys.exit(main()) |