blob: 5ef85371afdc904a075c9bc8bff004495f255a02 [file] [log] [blame]
mandermoed582f72017-01-23 07:55:42 -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 starts a loopback call with stubbed video in
12and out. It then analyses the video quality of the output video against the
13reference input video.
14
15It expect to be given the webrtc output build directory as the first argument
16all other arguments are optional.
17
18It assumes you have a Android device plugged in.
19"""
20
21import argparse
22import logging
23import os
24import shutil
25import subprocess
26import sys
27import tempfile
28
29
30SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
kjellander30292102017-02-09 01:05:54 -080031SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir,
32 os.pardir))
mandermoed582f72017-01-23 07:55:42 -080033
34
ehmaldonadod103f4b2017-02-16 07:20:26 -080035def _RunCommand(argv, cwd=SRC_DIR, **kwargs):
mandermoed582f72017-01-23 07:55:42 -080036 logging.info('Running %r', argv)
ehmaldonadod103f4b2017-02-16 07:20:26 -080037 subprocess.check_call(argv, cwd=cwd, **kwargs)
mandermoed582f72017-01-23 07:55:42 -080038
39
40def _ParseArgs():
41 parser = argparse.ArgumentParser(description='Start loopback video analysis.')
mandermoed582f72017-01-23 07:55:42 -080042 parser.add_argument('build_dir_android',
43 help='The path to the build directory for Android.')
44 parser.add_argument('--build_dir_x86',
45 help='The path to the build directory for building locally.')
46 parser.add_argument('--temp_dir',
47 help='A temporary directory to put the output.')
48
49 args = parser.parse_args()
50 return args
51
52
53def main():
54 logging.basicConfig(level=logging.INFO)
55
56 args = _ParseArgs()
57
mandermoed582f72017-01-23 07:55:42 -080058 build_dir_android = args.build_dir_android
59 build_dir_x86 = args.build_dir_x86
60 temp_dir = args.temp_dir
61 if not temp_dir:
62 temp_dir = tempfile.mkdtemp()
63 else:
64 if not os.path.exists(temp_dir):
65 os.makedirs(temp_dir)
66
67 if not build_dir_x86:
68 build_dir_x86 = os.path.join(temp_dir, 'LocalBuild')
69 _RunCommand(['gn', 'gen', build_dir_x86])
70 _RunCommand(['ninja', '-C', build_dir_x86, 'frame_analyzer'])
71
oprypin3b2fb202017-03-06 02:23:34 -080072 tools_dir = os.path.join(SRC_DIR, 'tools-webrtc')
73 toolchain_dir = os.path.join(tools_dir, 'video_quality_toolchain')
mandermoed582f72017-01-23 07:55:42 -080074
75 # Download ffmpeg and zxing.
oprypin3b2fb202017-03-06 02:23:34 -080076 download_script = os.path.join(tools_dir, 'download_tools.py')
77 _RunCommand([sys.executable, download_script, toolchain_dir])
mandermoed582f72017-01-23 07:55:42 -080078
79 # Run the Espresso code.
80 test_script = os.path.join(build_dir_android,
81 'bin', 'run_AppRTCMobileTestStubbedVideoIO')
82 _RunCommand([sys.executable, test_script])
83
84 # Pull the output video.
85 test_video = os.path.join(temp_dir, 'test_video.y4m')
86 _RunCommand(['adb', 'pull', '/sdcard/output.y4m', test_video])
87
88 test_video_yuv = os.path.join(temp_dir, 'test_video.yuv')
89
90 ffmpeg_path = os.path.join(toolchain_dir, 'linux', 'ffmpeg')
91
92 def convert_video(input_video, output_video):
93 _RunCommand([ffmpeg_path, '-y', '-i', input_video, output_video])
94
95 convert_video(test_video, test_video_yuv)
96
ehmaldonadod103f4b2017-02-16 07:20:26 -080097 reference_video = os.path.join(SRC_DIR,
mandermoed582f72017-01-23 07:55:42 -080098 'resources', 'reference_video_640x360_30fps.y4m')
99
100 reference_video_yuv = os.path.join(temp_dir,
101 'reference_video_640x360_30fps.yuv')
102
103 convert_video(reference_video, reference_video_yuv)
104
105 # Run compare script.
ehmaldonadod103f4b2017-02-16 07:20:26 -0800106 compare_script = os.path.join(SRC_DIR, 'webrtc', 'tools',
mandermoed582f72017-01-23 07:55:42 -0800107 'compare_videos.py')
108 zxing_path = os.path.join(toolchain_dir, 'linux', 'zxing')
109
110 # The frame_analyzer binary should be built for local computer and not for
111 # Android
112 frame_analyzer = os.path.join(build_dir_x86, 'frame_analyzer')
113
114 frame_width = 640
115 frame_height = 360
116
117 stats_file_ref = os.path.join(temp_dir, 'stats_ref.txt')
118 stats_file_test = os.path.join(temp_dir, 'stats_test.txt')
119
120 _RunCommand([
121 sys.executable, compare_script, '--ref_video', reference_video_yuv,
122 '--test_video', test_video_yuv, '--yuv_frame_width', str(frame_width),
123 '--yuv_frame_height', str(frame_height),
124 '--stats_file_ref', stats_file_ref,
125 '--stats_file_test', stats_file_test, '--frame_analyzer', frame_analyzer,
126 '--ffmpeg_path', ffmpeg_path, '--zxing_path', zxing_path])
127
128 shutil.rmtree(temp_dir)
129
130
131if __name__ == '__main__':
132 sys.exit(main())
133