jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [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 | |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 10 | import glob |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 11 | import optparse |
| 12 | import os |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 13 | import shutil |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 14 | import subprocess |
| 15 | import sys |
| 16 | import time |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 17 | |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 18 | |
| 19 | # Used to time-stamp output files and directories |
| 20 | CURRENT_TIME = time.strftime("%d_%m_%Y-%H:%M:%S") |
| 21 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 22 | |
| 23 | class Error(Exception): |
| 24 | pass |
| 25 | |
| 26 | |
| 27 | class FfmpegError(Error): |
| 28 | pass |
| 29 | |
| 30 | |
| 31 | class MagewellError(Error): |
| 32 | pass |
| 33 | |
| 34 | |
| 35 | class CompareVideosError(Error): |
| 36 | pass |
| 37 | |
| 38 | |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 39 | def _ParseArgs(): |
| 40 | """Registers the command-line options.""" |
| 41 | usage = 'usage: %prog [options]' |
| 42 | parser = optparse.OptionParser(usage=usage) |
| 43 | |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 44 | parser.add_option('--frame_width', type='int', default=1280, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 45 | help='Width of the recording. Default: %default') |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 46 | parser.add_option('--frame_height', type='int', default=720, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 47 | help='Height of the recording. Default: %default') |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 48 | parser.add_option('--framerate', type='int', default=60, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 49 | help='Recording framerate. Default: %default') |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 50 | parser.add_option('--ref_duration', type='int', default=20, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 51 | help='Reference recording duration. Default: %default') |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 52 | parser.add_option('--test_duration', type='int', default=10, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 53 | help='Test recording duration. Default: %default') |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 54 | parser.add_option('--time_between_recordings', type='int', default=5, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 55 | help='Time between starting test recording after ref.' |
| 56 | 'Default: %default') |
| 57 | parser.add_option('--ref_video_device', type='string', default='/dev/video0', |
| 58 | help='Reference recording device. Default: %default') |
| 59 | parser.add_option('--test_video_device', type='string', default='/dev/video1', |
| 60 | help='Test recording device. Default: %default') |
| 61 | parser.add_option('--app_name', type='string', |
| 62 | help='Name of the app under test.') |
| 63 | parser.add_option('--recording_api', type='string', default='Video4Linux2', |
| 64 | help='Recording API to use. Default: %default') |
| 65 | parser.add_option('--pixel_format', type='string', default='yuv420p', |
| 66 | help='Recording pixel format Default: %default') |
| 67 | parser.add_option('--ffmpeg', type='string', |
| 68 | help='Path to the ffmpeg executable for the reference ' |
| 69 | 'device.') |
| 70 | parser.add_option('--video_container', type='string', default='yuv', |
| 71 | help='Video container for the recordings.' |
| 72 | 'Default: %default') |
| 73 | parser.add_option('--compare_videos_script', type='string', |
| 74 | default='compare_videos.py', |
| 75 | help='Path to script used to compare and generate metrics.' |
| 76 | 'Default: %default') |
| 77 | parser.add_option('--frame_analyzer', type='string', |
| 78 | default='../../out/Default/frame_analyzer', |
| 79 | help='Path to the frame analyzer executable.' |
| 80 | 'Default: %default') |
| 81 | parser.add_option('--zxing_path', type='string', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame] | 82 | help='DEPRECATED.') |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 83 | parser.add_option('--ref_rec_dir', type='string', default='ref', |
| 84 | help='Path to where reference recordings will be created.' |
| 85 | 'Ideally keep the ref and test directories on separate' |
| 86 | 'drives. Default: %default') |
| 87 | parser.add_option('--test_rec_dir', type='string', default='test', |
| 88 | help='Path to where test recordings will be created.' |
| 89 | 'Ideally keep the ref and test directories on separate ' |
| 90 | 'drives. Default: %default') |
| 91 | parser.add_option('--test_crop_parameters', type='string', |
| 92 | help='ffmpeg processing parameters for the test video.') |
| 93 | parser.add_option('--ref_crop_parameters', type='string', |
| 94 | help='ffmpeg processing parameters for the ref video.') |
| 95 | |
| 96 | options, _ = parser.parse_args() |
| 97 | |
| 98 | if not options.app_name: |
| 99 | parser.error('You must provide an application name!') |
| 100 | |
| 101 | if not options.test_crop_parameters or not options.ref_crop_parameters: |
| 102 | parser.error('You must provide ref and test crop parameters!') |
| 103 | |
| 104 | # Ensure the crop filter is included in the crop parameters used for ffmpeg. |
| 105 | if 'crop' not in options.ref_crop_parameters: |
| 106 | parser.error('You must provide a reference crop filter for ffmpeg.') |
| 107 | if 'crop' not in options.test_crop_parameters: |
| 108 | parser.error('You must provide a test crop filter for ffmpeg.') |
| 109 | |
| 110 | if not options.ffmpeg: |
| 111 | parser.error('You most provide location for the ffmpeg executable.') |
| 112 | if not os.path.isfile(options.ffmpeg): |
| 113 | parser.error('Cannot find the ffmpeg executable.') |
| 114 | |
| 115 | # compare_videos.py dependencies. |
| 116 | if not os.path.isfile(options.compare_videos_script): |
| 117 | parser.warning('Cannot find compare_videos.py script, no metrics will be ' |
| 118 | 'generated!') |
| 119 | if not os.path.isfile(options.frame_analyzer): |
| 120 | parser.warning('Cannot find frame_analyzer, no metrics will be generated!') |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 121 | |
| 122 | return options |
| 123 | |
| 124 | |
| 125 | def CreateRecordingDirs(options): |
| 126 | """Create root + sub directories for reference and test recordings. |
| 127 | |
| 128 | Args: |
| 129 | options(object): Contains all the provided command line options. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 130 | |
| 131 | Returns: |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 132 | record_paths(dict): key: value pair with reference and test file |
| 133 | absolute paths. |
| 134 | """ |
| 135 | |
| 136 | # Create root directories for the video recordings. |
| 137 | if not os.path.isdir(options.ref_rec_dir): |
| 138 | os.makedirs(options.ref_rec_dir) |
| 139 | if not os.path.isdir(options.test_rec_dir): |
| 140 | os.makedirs(options.test_rec_dir) |
| 141 | |
| 142 | # Create and time-stamp directories for all the output files. |
| 143 | ref_rec_dir = os.path.join(options.ref_rec_dir, options.app_name + '_' + \ |
| 144 | CURRENT_TIME) |
| 145 | test_rec_dir = os.path.join(options.test_rec_dir, options.app_name + '_' + \ |
| 146 | CURRENT_TIME) |
| 147 | |
| 148 | os.makedirs(ref_rec_dir) |
| 149 | os.makedirs(test_rec_dir) |
| 150 | |
| 151 | record_paths = { |
| 152 | 'ref_rec_location' : os.path.abspath(ref_rec_dir), |
| 153 | 'test_rec_location' : os.path.abspath(test_rec_dir) |
| 154 | } |
| 155 | |
| 156 | return record_paths |
| 157 | |
| 158 | |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 159 | def FindUsbPortForV4lDevices(ref_video_device, test_video_device): |
| 160 | """Tries to find the usb port for ref_video_device and test_video_device. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 161 | |
| 162 | Tries to find the provided ref_video_device and test_video_device devices |
| 163 | which use video4linux and then do a soft reset by using USB unbind and bind. |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 164 | |
| 165 | Args: |
| 166 | ref_device(string): reference recording device path. |
| 167 | test_device(string): test recording device path |
| 168 | |
| 169 | Returns: |
| 170 | usb_ports(list): USB ports(string) for the devices found. |
| 171 | """ |
| 172 | |
| 173 | # Find the device location including USB and USB Bus ID's. Use the usb1 |
| 174 | # in the path since the driver folder is a symlink which contains all the |
| 175 | # usb device port mappings and it's the same in all usbN folders. Tested |
| 176 | # on Ubuntu 14.04. |
| 177 | v4l_device_path = '/sys/bus/usb/devices/usb1/1-1/driver/**/**/video4linux/' |
| 178 | v4l_ref_device = glob.glob('%s%s' % (v4l_device_path, ref_video_device)) |
| 179 | v4l_test_device = glob.glob('%s%s' % (v4l_device_path, test_video_device)) |
| 180 | usb_ports = [] |
| 181 | paths = [] |
| 182 | |
| 183 | # Split on the driver folder first since we are only interested in the |
| 184 | # folders thereafter. |
jansson | 07e20db | 2017-04-12 01:36:02 -0700 | [diff] [blame] | 185 | try: |
| 186 | ref_path = str(v4l_ref_device).split('driver')[1].split('/') |
| 187 | test_path = str(v4l_test_device).split('driver')[1].split('/') |
| 188 | except IndexError: |
| 189 | print 'Could not find one or both of the specified recording devices.' |
| 190 | else: |
| 191 | paths.append(ref_path) |
| 192 | paths.append(test_path) |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 193 | |
jansson | 07e20db | 2017-04-12 01:36:02 -0700 | [diff] [blame] | 194 | for path in paths: |
| 195 | for usb_id in path: |
| 196 | # Look for : separator and then use the first element in the list. |
| 197 | # E.g 3-3.1:1.0 split on : and [0] becomes 3-3.1 which can be used |
| 198 | # for bind/unbind. |
| 199 | if ':' in usb_id: |
| 200 | usb_ports.append(usb_id.split(':')[0]) |
| 201 | |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 202 | return usb_ports |
| 203 | |
| 204 | |
| 205 | def RestartMagewellDevices(ref_video_device_path, test_video_device_path): |
| 206 | """Reset the USB ports where Magewell capture devices are connected to. |
| 207 | |
| 208 | Performs a soft reset by using USB unbind and bind. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 209 | This is due to Magewell capture devices have proven to be unstable after the |
| 210 | first recording attempt. |
| 211 | |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 212 | Args: |
| 213 | ref_video_device_path(string): reference recording device path. |
| 214 | test_video_device_path(string): test recording device path |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 215 | |
| 216 | Raises: |
| 217 | MagewellError: If no magewell devices are found. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 218 | """ |
| 219 | |
| 220 | # Get the dev/videoN device name from the command line arguments. |
jansson | 80ff00c | 2017-04-11 07:40:26 -0700 | [diff] [blame] | 221 | ref_magewell_path = ref_video_device_path.split('/')[2] |
| 222 | test_magewell_path = test_video_device_path.split('/')[2] |
| 223 | magewell_usb_ports = FindUsbPortForV4lDevices(ref_magewell_path, |
| 224 | test_magewell_path) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 225 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 226 | # Abort early if no devices are found. |
| 227 | if len(magewell_usb_ports) == 0: |
| 228 | raise MagewellError('No magewell devices found.') |
| 229 | else: |
| 230 | print '\nResetting USB ports where magewell devices are connected...' |
| 231 | # Use the USB bus and port ID (e.g. 4-3) to unbind and bind the USB devices |
| 232 | # (i.e. soft eject and insert). |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 233 | for usb_port in magewell_usb_ports: |
| 234 | echo_cmd = ['echo', usb_port] |
| 235 | unbind_cmd = ['sudo', 'tee', '/sys/bus/usb/drivers/usb/unbind'] |
| 236 | bind_cmd = ['sudo', 'tee', '/sys/bus/usb/drivers/usb/bind'] |
| 237 | |
| 238 | # TODO(jansson) Figure out a way to call on echo once for bind & unbind |
| 239 | # if possible. |
| 240 | echo_unbind = subprocess.Popen(echo_cmd, stdout=subprocess.PIPE) |
| 241 | unbind = subprocess.Popen(unbind_cmd, stdin=echo_unbind.stdout) |
| 242 | echo_unbind.stdout.close() |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 243 | unbind.wait() |
| 244 | |
| 245 | echo_bind = subprocess.Popen(echo_cmd, stdout=subprocess.PIPE) |
| 246 | bind = subprocess.Popen(bind_cmd, stdin=echo_bind.stdout) |
| 247 | echo_bind.stdout.close() |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 248 | bind.wait() |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 249 | if bind.returncode == 0: |
| 250 | print 'Reset done!\n' |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 251 | |
| 252 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 253 | def StartRecording(options, ref_file_location, test_file_location): |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 254 | """Starts recording from the two specified video devices. |
| 255 | |
| 256 | Args: |
| 257 | options(object): Contains all the provided command line options. |
| 258 | record_paths(dict): key: value pair with reference and test file |
| 259 | absolute paths. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 260 | |
| 261 | Returns: |
| 262 | recording_files_and_time(dict): key: value pair with the path to cropped |
| 263 | test and reference video files. |
| 264 | |
| 265 | Raises: |
| 266 | FfmpegError: If the ffmpeg command fails. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 267 | """ |
| 268 | ref_file_name = '%s_%s_ref.%s' % (options.app_name, CURRENT_TIME, |
| 269 | options.video_container) |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 270 | ref_file = os.path.join(ref_file_location, ref_file_name) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 271 | |
| 272 | test_file_name = '%s_%s_test.%s' % (options.app_name, CURRENT_TIME, |
| 273 | options.video_container) |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 274 | test_file = os.path.join(test_file_location, test_file_name) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 275 | |
| 276 | # Reference video recorder command line. |
| 277 | ref_cmd = [ |
| 278 | options.ffmpeg, |
| 279 | '-v', 'error', |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 280 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 281 | '-r', '%d' % options.framerate, |
| 282 | '-f', '%s' % options.recording_api, |
| 283 | '-i', '%s' % options.ref_video_device, |
| 284 | '-pix_fmt', '%s' % options.pixel_format, |
| 285 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 286 | '-t', '%d' % options.ref_duration, |
| 287 | '-r', '%d' % options.framerate, |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 288 | ref_file |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 289 | ] |
| 290 | |
| 291 | # Test video recorder command line. |
| 292 | test_cmd = [ |
| 293 | options.ffmpeg, |
| 294 | '-v', 'error', |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 295 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 296 | '-r', '%d' % options.framerate, |
| 297 | '-f', '%s' % options.recording_api, |
| 298 | '-i', '%s' % options.test_video_device, |
| 299 | '-pix_fmt', '%s' % options.pixel_format, |
| 300 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 301 | '-t', '%d' % options.test_duration, |
| 302 | '-r', '%d' % options.framerate, |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 303 | test_file |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 304 | ] |
| 305 | print 'Trying to record from reference recorder...' |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 306 | ref_recorder = subprocess.Popen(ref_cmd) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 307 | |
| 308 | # Start the 2nd recording a little later to ensure the 1st one has started. |
| 309 | # TODO(jansson) Check that the ref_recorder output file exists rather than |
| 310 | # using sleep. |
| 311 | time.sleep(options.time_between_recordings) |
| 312 | print 'Trying to record from test recorder...' |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 313 | test_recorder = subprocess.Popen(test_cmd) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 314 | test_recorder.wait() |
| 315 | ref_recorder.wait() |
| 316 | |
| 317 | # ffmpeg does not abort when it fails, need to check return code. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 318 | if ref_recorder.returncode != 0 or test_recorder.returncode != 0: |
| 319 | # Cleanup recording directories. |
| 320 | shutil.rmtree(ref_file_location) |
| 321 | shutil.rmtree(test_file_location) |
| 322 | raise FfmpegError('Recording failed, check ffmpeg output.') |
| 323 | else: |
| 324 | print 'Ref file recorded to: ' + os.path.abspath(ref_file) |
| 325 | print 'Test file recorded to: ' + os.path.abspath(test_file) |
| 326 | print 'Recording done!\n' |
| 327 | return FlipAndCropRecordings(options, test_file_name, test_file_location, |
| 328 | ref_file_name, ref_file_location) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 329 | |
| 330 | |
| 331 | def FlipAndCropRecordings(options, test_file_name, test_file_location, |
| 332 | ref_file_name, ref_file_location): |
| 333 | """Performs a horizontal flip of the reference video to match the test video. |
| 334 | |
| 335 | This is done to the match orientation and then crops the ref and test videos |
| 336 | using the options.test_crop_parameters and options.ref_crop_parameters. |
| 337 | |
| 338 | Args: |
| 339 | options(object): Contains all the provided command line options. |
| 340 | test_file_name(string): Name of the test video file recording. |
| 341 | test_file_location(string): Path to the test video file recording. |
| 342 | ref_file_name(string): Name of the reference video file recording. |
| 343 | ref_file_location(string): Path to the reference video file recording. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 344 | |
| 345 | Returns: |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 346 | recording_files_and_time(dict): key: value pair with the path to cropped |
| 347 | test and reference video files. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 348 | |
| 349 | Raises: |
| 350 | FfmpegError: If the ffmpeg command fails. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 351 | """ |
| 352 | print 'Trying to crop videos...' |
| 353 | |
| 354 | # Ref file cropping. |
| 355 | cropped_ref_file_name = 'cropped_' + ref_file_name |
| 356 | cropped_ref_file = os.path.abspath( |
| 357 | os.path.join(ref_file_location, cropped_ref_file_name)) |
| 358 | |
| 359 | ref_video_crop_cmd = [ |
| 360 | options.ffmpeg, |
| 361 | '-v', 'error', |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 362 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 363 | '-i', '%s' % os.path.join(ref_file_location, ref_file_name), |
| 364 | '-vf', '%s' % options.ref_crop_parameters, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 365 | '-c:a', 'copy', |
| 366 | cropped_ref_file |
| 367 | ] |
| 368 | |
| 369 | # Test file cropping. |
| 370 | cropped_test_file_name = 'cropped_' + test_file_name |
| 371 | cropped_test_file = os.path.abspath( |
| 372 | os.path.join(test_file_location, cropped_test_file_name)) |
| 373 | |
| 374 | test_video_crop_cmd = [ |
| 375 | options.ffmpeg, |
| 376 | '-v', 'error', |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 377 | '-s', '%dx%d' % (options.frame_width, options.frame_height), |
| 378 | '-i', '%s' % os.path.join(test_file_location, test_file_name), |
| 379 | '-vf', '%s' % options.test_crop_parameters, |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 380 | '-c:a', 'copy', |
| 381 | cropped_test_file |
| 382 | ] |
| 383 | |
| 384 | ref_crop = subprocess.Popen(ref_video_crop_cmd) |
| 385 | ref_crop.wait() |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 386 | test_crop = subprocess.Popen(test_video_crop_cmd) |
| 387 | test_crop.wait() |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 388 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 389 | # ffmpeg does not abort when it fails, need to check return code. |
| 390 | if ref_crop.returncode != 0 or test_crop.returncode != 0: |
| 391 | # Cleanup recording directories. |
| 392 | shutil.rmtree(ref_file_location) |
| 393 | shutil.rmtree(test_file_location) |
| 394 | raise FfmpegError('Cropping failed, check ffmpeg output.') |
| 395 | else: |
| 396 | print 'Ref file cropped to: ' + cropped_ref_file |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 397 | print 'Test file cropped to: ' + cropped_test_file |
| 398 | print 'Cropping done!\n' |
| 399 | |
| 400 | # Need to return these so they can be used by other parts. |
| 401 | cropped_recordings = { |
| 402 | 'cropped_test_file' : cropped_test_file, |
| 403 | 'cropped_ref_file' : cropped_ref_file |
| 404 | } |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 405 | return cropped_recordings |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 406 | |
| 407 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 408 | def CompareVideos(options, cropped_ref_file, cropped_test_file): |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 409 | """Runs the compare_video.py script from src/rtc_tools using path. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 410 | |
| 411 | Uses the path from recording_result and writes the output to a file named |
| 412 | <options.app_name + '_' + CURRENT_TIME + '_result.txt> in the reference video |
| 413 | recording folder taken from recording_result. |
| 414 | |
| 415 | Args: |
| 416 | options(object): Contains all the provided command line options. |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 417 | cropped_ref_file(string): Path to cropped reference video file. |
| 418 | cropped_test_file(string): Path to cropped test video file. |
| 419 | |
| 420 | Raises: |
| 421 | CompareVideosError: If compare_videos.py fails. |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 422 | """ |
| 423 | print 'Starting comparison...' |
| 424 | print 'Grab a coffee, this might take a few minutes...' |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 425 | compare_videos_script = os.path.abspath(options.compare_videos_script) |
| 426 | rec_path = os.path.abspath(os.path.join( |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 427 | os.path.dirname(cropped_test_file))) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 428 | result_file_name = os.path.join(rec_path, '%s_%s_result.txt') % ( |
| 429 | options.app_name, CURRENT_TIME) |
| 430 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 431 | # Find the crop dimensions (e.g. 950 and 420) in the ref crop parameter |
| 432 | # string: 'hflip, crop=950:420:130:56' |
| 433 | for param in options.ref_crop_parameters.split('crop'): |
| 434 | if param[0] == '=': |
| 435 | crop_width = int(param.split(':')[0].split('=')[1]) |
| 436 | crop_height = int(param.split(':')[1]) |
| 437 | |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 438 | compare_cmd = [ |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 439 | compare_videos_script, |
jansson | 9b93203 | 2017-06-02 02:16:27 -0700 | [diff] [blame] | 440 | '--ref_video=%s' % cropped_ref_file, |
| 441 | '--test_video=%s' % cropped_test_file, |
| 442 | '--frame_analyzer=%s' % os.path.abspath(options.frame_analyzer), |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 443 | '--yuv_frame_height=%d' % crop_height, |
| 444 | '--yuv_frame_width=%d' % crop_width |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 445 | ] |
| 446 | |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 447 | with open(result_file_name, 'w') as f: |
jansson | 07e20db | 2017-04-12 01:36:02 -0700 | [diff] [blame] | 448 | try: |
| 449 | compare_video_recordings = subprocess.check_output(compare_cmd) |
| 450 | f.write(compare_video_recordings) |
| 451 | except subprocess.CalledProcessError as error: |
| 452 | raise CompareVideosError('Failed to perform comparison: %s' % error) |
| 453 | else: |
| 454 | print 'Result recorded to: %s' % os.path.abspath(result_file_name) |
| 455 | print 'Comparison done!' |
| 456 | return compare_video_recordings |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 457 | |
| 458 | |
| 459 | def main(): |
| 460 | """The main function. |
| 461 | |
| 462 | A simple invocation is: |
| 463 | ./run_video_analysis.py \ |
| 464 | --app_name AppRTCMobile \ |
| 465 | --ffmpeg ./ffmpeg --ref_video_device=/dev/video0 \ |
| 466 | --test_video_device=/dev/video1 \ |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 467 | --test_crop_parameters 'crop=950:420:130:56' \ |
| 468 | --ref_crop_parameters 'hflip, crop=950:420:130:56' \ |
| 469 | --ref_rec_dir /tmp/ref \ |
| 470 | --test_rec_dir /tmp/test |
| 471 | |
| 472 | This will produce the following files if successful: |
| 473 | # Original video recordings. |
| 474 | /tmp/ref/AppRTCMobile_<recording date and time>_ref.yuv |
| 475 | /tmp/test/AppRTCMobile_<recording date and time>_test.yuv |
| 476 | |
| 477 | # Cropped video recordings according to the crop parameters. |
| 478 | /tmp/ref/cropped_AppRTCMobile_<recording date and time>_ref.yuv |
| 479 | /tmp/test/cropped_AppRTCMobile_<recording date and time>_ref.yuv |
| 480 | |
| 481 | # Comparison metrics from cropped test and ref videos. |
| 482 | /tmp/test/AppRTCMobile_<recording date and time>_result.text |
| 483 | |
| 484 | """ |
| 485 | options = _ParseArgs() |
| 486 | RestartMagewellDevices(options.ref_video_device, options.test_video_device) |
| 487 | record_paths = CreateRecordingDirs(options) |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 488 | recording_result = StartRecording(options, record_paths['ref_rec_location'], |
| 489 | record_paths['test_rec_location']) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 490 | |
| 491 | # Do not require compare_video.py script to run, no metrics will be generated. |
| 492 | if options.compare_videos_script: |
jansson | 5c7a623 | 2017-03-15 08:27:31 -0700 | [diff] [blame] | 493 | CompareVideos(options, recording_result['cropped_ref_file'], |
| 494 | recording_result['cropped_test_file']) |
jansson | 1b0e3b8 | 2017-03-13 02:15:51 -0700 | [diff] [blame] | 495 | else: |
| 496 | print ('Skipping compare videos step due to compare_videos flag were not ' |
| 497 | 'passed.') |
| 498 | |
| 499 | |
| 500 | if __name__ == '__main__': |
| 501 | sys.exit(main()) |