Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: webrtc/tools/video_analysis.py

Issue 2789533002: Improve USB device reset logic (Closed)
Patch Set: fix test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/tools/run_video_analysis.py ('k') | webrtc/tools/video_analysis_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import glob
10 import optparse 11 import optparse
11 import os 12 import os
13 import shutil
12 import subprocess 14 import subprocess
13 import sys 15 import sys
14 import time 16 import time
15 import glob 17
16 import re
17 import shutil
18 18
19 # Used to time-stamp output files and directories 19 # Used to time-stamp output files and directories
20 CURRENT_TIME = time.strftime("%d_%m_%Y-%H:%M:%S") 20 CURRENT_TIME = time.strftime("%d_%m_%Y-%H:%M:%S")
21 21
22 22
23 class Error(Exception): 23 class Error(Exception):
24 pass 24 pass
25 25
26 26
27 class FfmpegError(Error): 27 class FfmpegError(Error):
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 os.makedirs(test_rec_dir) 151 os.makedirs(test_rec_dir)
152 152
153 record_paths = { 153 record_paths = {
154 'ref_rec_location' : os.path.abspath(ref_rec_dir), 154 'ref_rec_location' : os.path.abspath(ref_rec_dir),
155 'test_rec_location' : os.path.abspath(test_rec_dir) 155 'test_rec_location' : os.path.abspath(test_rec_dir)
156 } 156 }
157 157
158 return record_paths 158 return record_paths
159 159
160 160
161 def RestartMagewellDevices(ref_video_device, test_video_device): 161 def FindUsbPortForV4lDevices(ref_video_device, test_video_device):
162 """Reset the USB ports where Magewell capture devices are connected to. 162 """Tries to find the usb port for ref_video_device and test_video_device.
163 163
164 Tries to find the provided ref_video_device and test_video_device devices 164 Tries to find the provided ref_video_device and test_video_device devices
165 which use video4linux and then do a soft reset by using USB unbind and bind. 165 which use video4linux and then do a soft reset by using USB unbind and bind.
166
167 Args:
168 ref_device(string): reference recording device path.
169 test_device(string): test recording device path
170
171 Returns:
172 usb_ports(list): USB ports(string) for the devices found.
173 """
174
175 # Find the device location including USB and USB Bus ID's. Use the usb1
176 # in the path since the driver folder is a symlink which contains all the
177 # usb device port mappings and it's the same in all usbN folders. Tested
178 # on Ubuntu 14.04.
179 v4l_device_path = '/sys/bus/usb/devices/usb1/1-1/driver/**/**/video4linux/'
180 v4l_ref_device = glob.glob('%s%s' % (v4l_device_path, ref_video_device))
181 v4l_test_device = glob.glob('%s%s' % (v4l_device_path, test_video_device))
182 usb_ports = []
183 paths = []
184
185 # Split on the driver folder first since we are only interested in the
186 # folders thereafter.
187 ref_path = str(v4l_ref_device).split('driver')[1].split('/')
188 test_path = str(v4l_test_device).split('driver')[1].split('/')
189 paths.append(ref_path)
190 paths.append(test_path)
191
192 for path in paths:
193 for usb_id in path:
194 # Look for : separator and then use the first element in the list.
195 # E.g 3-3.1:1.0 split on : and [0] becomes 3-3.1 which can be used
196 # for bind/unbind.
197 if ':' in usb_id:
198 usb_ports.append(usb_id.split(':')[0])
199 return usb_ports
200
201
202 def RestartMagewellDevices(ref_video_device_path, test_video_device_path):
203 """Reset the USB ports where Magewell capture devices are connected to.
204
205 Performs a soft reset by using USB unbind and bind.
166 This is due to Magewell capture devices have proven to be unstable after the 206 This is due to Magewell capture devices have proven to be unstable after the
167 first recording attempt. 207 first recording attempt.
168 208
169 Args : 209 Args:
170 ref_video_device(string): reference recording device path. 210 ref_video_device_path(string): reference recording device path.
171 test_video_device(string): test recording device path 211 test_video_device_path(string): test recording device path
172 212
173 Raises: 213 Raises:
174 MagewellError: If no magewell devices are found. 214 MagewellError: If no magewell devices are found.
175 """ 215 """
176 216
177 # Get the dev/videoN device name from the command line arguments. 217 # Get the dev/videoN device name from the command line arguments.
178 ref_magewell = ref_video_device.split('/')[2] 218 ref_magewell_path = ref_video_device_path.split('/')[2]
179 test_magewell = test_video_device.split('/')[2] 219 test_magewell_path = test_video_device_path.split('/')[2]
180 220 magewell_usb_ports = FindUsbPortForV4lDevices(ref_magewell_path,
181 # Find the device location including USB and USB Bus ID's. 221 test_magewell_path)
182 device_string = '/sys/bus/usb/devices/usb*/**/**/video4linux/'
183 ref_magewell_device = glob.glob('%s%s' % (device_string, ref_magewell))
184 test_magewell_device = glob.glob('%s%s' % (device_string, test_magewell))
185
186 magewell_usb_ports = []
187
188 # Figure out the USB bus and port ID for each device.
189 ref_magewell_path = str(ref_magewell_device).split('/')
190 for directory in ref_magewell_path:
191 # Find the folder with pattern "N-N", e.g. "4-3" or \
192 # "[USB bus ID]-[USB port]"
193 if re.match(r'^\d-\d$', directory):
194 magewell_usb_ports.append(directory)
195
196 test_magewell_path = str(test_magewell_device).split('/')
197 for directory in test_magewell_path:
198 # Find the folder with pattern "N-N", e.g. "4-3" or \
199 # "[USB bus ID]-[USB port]"
200 if re.match(r'^\d-\d$', directory):
201 magewell_usb_ports.append(directory)
202 222
203 # Abort early if no devices are found. 223 # Abort early if no devices are found.
204 if len(magewell_usb_ports) == 0: 224 if len(magewell_usb_ports) == 0:
205 raise MagewellError('No magewell devices found.') 225 raise MagewellError('No magewell devices found.')
206 else: 226 else:
207 print '\nResetting USB ports where magewell devices are connected...' 227 print '\nResetting USB ports where magewell devices are connected...'
208 # Use the USB bus and port ID (e.g. 4-3) to unbind and bind the USB devices 228 # Use the USB bus and port ID (e.g. 4-3) to unbind and bind the USB devices
209 # (i.e. soft eject and insert). 229 # (i.e. soft eject and insert).
210 for usb_port in magewell_usb_ports: 230 for usb_port in magewell_usb_ports:
211 echo_cmd = ['echo', usb_port] 231 echo_cmd = ['echo', usb_port]
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 options.app_name, CURRENT_TIME) 426 options.app_name, CURRENT_TIME)
407 427
408 # Find the crop dimensions (e.g. 950 and 420) in the ref crop parameter 428 # Find the crop dimensions (e.g. 950 and 420) in the ref crop parameter
409 # string: 'hflip, crop=950:420:130:56' 429 # string: 'hflip, crop=950:420:130:56'
410 for param in options.ref_crop_parameters.split('crop'): 430 for param in options.ref_crop_parameters.split('crop'):
411 if param[0] == '=': 431 if param[0] == '=':
412 crop_width = param.split(':')[0].split('=')[1] 432 crop_width = param.split(':')[0].split('=')[1]
413 crop_height = param.split(':')[1] 433 crop_height = param.split(':')[1]
414 434
415 compare_cmd = [ 435 compare_cmd = [
416 sys.executable,
417 compare_videos_script, 436 compare_videos_script,
418 '--ref_video', cropped_ref_file, 437 '--ref_video', cropped_ref_file,
419 '--test_video', cropped_test_file, 438 '--test_video', cropped_test_file,
420 '--frame_analyzer', os.path.abspath(options.frame_analyzer), 439 '--frame_analyzer', os.path.abspath(options.frame_analyzer),
421 '--zxing_path', options.zxing_path, 440 '--zxing_path', options.zxing_path,
422 '--ffmpeg_path', options.ffmpeg, 441 '--ffmpeg_path', options.ffmpeg,
423 '--stats_file_ref', os.path.join(os.path.dirname(cropped_ref_file), 442 '--stats_file_ref', os.path.join(os.path.dirname(cropped_ref_file),
424 cropped_ref_file + '_stats.txt'), 443 cropped_ref_file + '_stats.txt'),
425 '--stats_file_test', os.path.join(os.path.dirname(cropped_test_file), 444 '--stats_file_test', os.path.join(os.path.dirname(cropped_test_file),
426 cropped_test_file + '_stats.txt'), 445 cropped_test_file + '_stats.txt'),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 if options.compare_videos_script: 494 if options.compare_videos_script:
476 CompareVideos(options, recording_result['cropped_ref_file'], 495 CompareVideos(options, recording_result['cropped_ref_file'],
477 recording_result['cropped_test_file']) 496 recording_result['cropped_test_file'])
478 else: 497 else:
479 print ('Skipping compare videos step due to compare_videos flag were not ' 498 print ('Skipping compare videos step due to compare_videos flag were not '
480 'passed.') 499 'passed.')
481 500
482 501
483 if __name__ == '__main__': 502 if __name__ == '__main__':
484 sys.exit(main()) 503 sys.exit(main())
OLDNEW
« no previous file with comments | « webrtc/tools/run_video_analysis.py ('k') | webrtc/tools/video_analysis_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698