blob: 6482d3d0209cba828185f771b1299d8c0e355304 [file] [log] [blame]
kjellander8f8d1a02017-03-06 04:01:16 -08001#!/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"""
11This script is the wrapper that runs the low-bandwidth audio test.
12
13After running the test, post-process steps for calculating audio quality of the
14output files will be performed.
15"""
16
17import argparse
18import logging
19import os
oprypin92220ff2017-03-23 03:40:03 -070020import re
kjellander8f8d1a02017-03-06 04:01:16 -080021import subprocess
22import sys
23
24
25SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
26SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir,
27 os.pardir))
28
29
oprypin92220ff2017-03-23 03:40:03 -070030def _LogCommand(command):
31 logging.info('Running %r', command)
32 return command
kjellander8f8d1a02017-03-06 04:01:16 -080033
34
35def _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).')
oprypin92220ff2017-03-23 03:40:03 -070039 parser.add_argument('--remove', action='store_true',
40 help='Remove output audio files after testing.')
kjellander8f8d1a02017-03-06 04:01:16 -080041 args = parser.parse_args()
42 return args
43
44
oprypin92220ff2017-03-23 03:40:03 -070045def _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
54def _GetExecutableExtension():
55 if sys.platform == 'win32':
56 return '.exe'
57 else:
58 return ''
59
60
61def _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
kjellander8f8d1a02017-03-06 04:01:16 -080075def main():
76 # pylint: disable=W0101
77 logging.basicConfig(level=logging.INFO)
78
79 args = _ParseArgs()
80
oprypin92220ff2017-03-23 03:40:03 -070081 pesq_path = _DownloadTools()
kjellander8f8d1a02017-03-06 04:01:16 -080082
oprypin92220ff2017-03-23 03:40:03 -070083 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()
kjellander8f8d1a02017-03-06 04:01:16 -0800120
121
122if __name__ == '__main__':
123 sys.exit(main())