blob: 952ebd61b68825ae6339e897a690c8cc6decc4ab [file] [log] [blame]
Oleh Prypin739b8162018-05-17 13:28:29 +02001#!/usr/bin/env python
2# Copyright (c) 2014 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"""Checks if a virtual webcam is running and starts it if not.
11
12Returns a non-zero return code if the webcam could not be started.
13
14Prerequisites:
15* The Python interpreter must have the psutil package installed.
16* Windows: a scheduled task named 'ManyCam' must exist and be configured to
17 launch ManyCam preconfigured to auto-play the test clip.
18* Mac: ManyCam must be installed in the default location and be preconfigured
19 to auto-play the test clip.
20* Linux: The v4l2loopback kernel module must be compiled and loaded to the
21 kernel already and the v4l2_file_player application must be compiled and put
22 in the location specified below.
23
24NOTICE: When running this script as a buildbot step, make sure to set
25usePTY=False for the build step when adding it, or the subprocess will die as
26soon the step has executed.
27
28If any command line arguments are passed to the script, it is executed as a
29command in a subprocess.
30"""
31
32import os
33# psutil is not installed on non-Linux machines by default.
34import psutil # pylint: disable=F0401
35import subprocess
36import sys
37import time
38
39
40WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam')
41WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app')
42E = os.path.expandvars
43WEBCAM_LINUX = (
44 E('$HOME/fake-webcam-driver/linux/v4l2_file_player/v4l2_file_player'),
45 E('$HOME/webrtc_video_quality/reference_video.yuv'),
46 '640', '480', '/dev/video0',
47)
48
49
50def IsWebCamRunning():
51 if sys.platform == 'win32':
52 process_name = 'ManyCam.exe'
53 elif sys.platform.startswith('darwin'):
54 process_name = 'ManyCam'
55 elif sys.platform.startswith('linux'):
56 process_name = 'v4l2_file_player'
57 else:
58 raise Exception('Unsupported platform: %s' % sys.platform)
59 for p in psutil.process_iter():
60 try:
61 if process_name == p.name:
62 print 'Found a running virtual webcam (%s with PID %s)' % (p.name,
63 p.pid)
64 return True
65 except psutil.AccessDenied:
66 pass # This is normal if we query sys processes, etc.
67 return False
68
69
70def StartWebCam():
71 try:
72 if sys.platform == 'win32':
73 subprocess.check_call(WEBCAM_WIN)
74 print 'Successfully launched virtual webcam.'
75 elif sys.platform.startswith('darwin'):
76 subprocess.check_call(WEBCAM_MAC)
77 print 'Successfully launched virtual webcam.'
78 elif sys.platform.startswith('linux'):
79
80 # Must redirect stdout/stderr/stdin to avoid having the subprocess
81 # being killed when the parent shell dies (happens on the bots).
82 process = subprocess.Popen(WEBCAM_LINUX, stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE,
84 stdin=subprocess.PIPE)
85 # If the v4l2loopback module is not loaded or incorrectly configured,
86 # the process will still launch but will die immediately.
87 # Wait for a second and then check for aliveness to catch such errors.
88 time.sleep(1)
89 if process.poll() is None:
90 print 'Successfully launched virtual webcam with PID %s' % process.pid
91 else:
92 print 'Failed to launch virtual webcam.'
93 return False
94
95 except Exception as e:
96 print 'Failed to launch virtual webcam: %s' % e
97 return False
98
99 return True
100
101
102def _ForcePythonInterpreter(cmd):
103 """Returns the fixed command line to call the right python executable."""
104 out = cmd[:]
105 if out[0] == 'python':
106 out[0] = sys.executable
107 elif out[0].endswith('.py'):
108 out.insert(0, sys.executable)
109 return out
110
111
112def Main(argv):
113 if IsWebCamRunning():
114 return 0
115 if not StartWebCam():
116 return 1
117
118 if argv:
119 return subprocess.call(_ForcePythonInterpreter(argv))
120
121
122if __name__ == '__main__':
123 sys.exit(Main(sys.argv[1:]))