Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 80ff00c

Browse files
janssonCommit bot
authored andcommitted
Improve USB device reset logic
BUG=webrtc:7203 NOTRY=True Review-Url: https://codereview.webrtc.org/2789533002 Cr-Commit-Position: refs/heads/master@{#17656}
1 parent b213a16 commit 80ff00c

File tree

3 files changed

+113
-33
lines changed

3 files changed

+113
-33
lines changed

PRESUBMIT.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def Join(*args):
471471

472472
test_directories = [
473473
Join('webrtc', 'tools', 'py_event_log_analyzer'),
474+
Join('webrtc', 'tools'),
474475
Join('webrtc', 'audio', 'test', 'unittests'),
475476
] + [
476477
root for root, _, files in os.walk(Join('tools-webrtc'))

webrtc/tools/run_video_analysis.py renamed to webrtc/tools/video_analysis.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
# in the file PATENTS. All contributing project authors may
88
# be found in the AUTHORS file in the root of the source tree.
99

10+
import glob
1011
import optparse
1112
import os
13+
import shutil
1214
import subprocess
1315
import sys
1416
import time
15-
import glob
16-
import re
17-
import shutil
17+
1818

1919
# Used to time-stamp output files and directories
2020
CURRENT_TIME = time.strftime("%d_%m_%Y-%H:%M:%S")
@@ -158,47 +158,67 @@ def CreateRecordingDirs(options):
158158
return record_paths
159159

160160

161-
def RestartMagewellDevices(ref_video_device, test_video_device):
162-
"""Reset the USB ports where Magewell capture devices are connected to.
161+
def FindUsbPortForV4lDevices(ref_video_device, test_video_device):
162+
"""Tries to find the usb port for ref_video_device and test_video_device.
163163
164164
Tries to find the provided ref_video_device and test_video_device devices
165165
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.
166206
This is due to Magewell capture devices have proven to be unstable after the
167207
first recording attempt.
168208
169-
Args :
170-
ref_video_device(string): reference recording device path.
171-
test_video_device(string): test recording device path
209+
Args:
210+
ref_video_device_path(string): reference recording device path.
211+
test_video_device_path(string): test recording device path
172212
173213
Raises:
174214
MagewellError: If no magewell devices are found.
175215
"""
176216

177217
# Get the dev/videoN device name from the command line arguments.
178-
ref_magewell = ref_video_device.split('/')[2]
179-
test_magewell = test_video_device.split('/')[2]
180-
181-
# Find the device location including USB and USB Bus ID's.
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)
218+
ref_magewell_path = ref_video_device_path.split('/')[2]
219+
test_magewell_path = test_video_device_path.split('/')[2]
220+
magewell_usb_ports = FindUsbPortForV4lDevices(ref_magewell_path,
221+
test_magewell_path)
202222

203223
# Abort early if no devices are found.
204224
if len(magewell_usb_ports) == 0:
@@ -413,7 +433,6 @@ def CompareVideos(options, cropped_ref_file, cropped_test_file):
413433
crop_height = param.split(':')[1]
414434

415435
compare_cmd = [
416-
sys.executable,
417436
compare_videos_script,
418437
'--ref_video', cropped_ref_file,
419438
'--test_video', cropped_test_file,

webrtc/tools/video_analysis_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
import glob
11+
import unittest
12+
from video_analysis import FindUsbPortForV4lDevices
13+
14+
15+
class RunVideoAnalysisTest(unittest.TestCase):
16+
def setGlobPath(self, path1, path2):
17+
self.path1 = path1
18+
self.path2 = path2
19+
20+
def setUp(self):
21+
self.path1 = ''
22+
self.path2 = ''
23+
self.requestNbr = 1
24+
25+
def glob_mock(string):
26+
# Eat incoming string.
27+
del string
28+
if self.requestNbr == 1:
29+
self.requestNbr += 1
30+
return self.path1
31+
else:
32+
self.requestNbr = 1
33+
return self.path2
34+
35+
# Override the glob function with our own that returns a string set by the
36+
# test.
37+
glob.glob = glob_mock
38+
39+
# Verifies that the correct USB id is returned.
40+
def testFindUSBPortForV4lDevices(self):
41+
short_path1 = ('/sys/bus/usb/devices/usb1/1-1/driver/4-4/4-4:1.0/'
42+
'video4linux/video0')
43+
short_path2 = ('/sys/bus/usb/devices/usb1/1-1/driver/4-3/4-3:1.0/'
44+
'video4linux/video1')
45+
self.setGlobPath(short_path1, short_path2)
46+
short_usb_ids = ['4-4', '4-3']
47+
self.assertEqual(FindUsbPortForV4lDevices('video0', 'video1'),
48+
short_usb_ids)
49+
50+
long_path1 = ('/sys/bus/usb/devices/usb1/1-1/driver/3-3/3-3.1:1.0/'
51+
'video4linux/video0')
52+
long_path2 = ('/sys/bus/usb/devices/usb1/1-1/driver/3-2/3-2.1:1.0/'
53+
'video4linux/video1')
54+
self.setGlobPath(long_path1, long_path2)
55+
long_usb_ids = ['3-3.1', '3-2.1']
56+
self.assertEqual(FindUsbPortForV4lDevices('video0', 'video1'), long_usb_ids)
57+
58+
59+
if __name__ == "__main__":
60+
unittest.main()

0 commit comments

Comments
 (0)