blob: 20cf49e1379abd8778f74ffcf3a6c9a4fbb3993b [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.
Patrik Höglund99233532018-08-17 15:46:24 +020020* Linux: Not implemented
Oleh Prypin739b8162018-05-17 13:28:29 +020021
22NOTICE: When running this script as a buildbot step, make sure to set
23usePTY=False for the build step when adding it, or the subprocess will die as
24soon the step has executed.
25
26If any command line arguments are passed to the script, it is executed as a
27command in a subprocess.
28"""
29
Oleh Prypin739b8162018-05-17 13:28:29 +020030# psutil is not installed on non-Linux machines by default.
31import psutil # pylint: disable=F0401
32import subprocess
33import sys
Oleh Prypin739b8162018-05-17 13:28:29 +020034
35
36WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam')
37WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app')
Oleh Prypin739b8162018-05-17 13:28:29 +020038
39
40def IsWebCamRunning():
41 if sys.platform == 'win32':
42 process_name = 'ManyCam.exe'
43 elif sys.platform.startswith('darwin'):
44 process_name = 'ManyCam'
45 elif sys.platform.startswith('linux'):
Patrik Höglund99233532018-08-17 15:46:24 +020046 # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
47 # longer in use.
48 print 'Virtual webcam: no-op on Linux'
49 return True
Oleh Prypin739b8162018-05-17 13:28:29 +020050 else:
51 raise Exception('Unsupported platform: %s' % sys.platform)
52 for p in psutil.process_iter():
53 try:
54 if process_name == p.name:
55 print 'Found a running virtual webcam (%s with PID %s)' % (p.name,
56 p.pid)
57 return True
58 except psutil.AccessDenied:
59 pass # This is normal if we query sys processes, etc.
60 return False
61
62
63def StartWebCam():
64 try:
65 if sys.platform == 'win32':
66 subprocess.check_call(WEBCAM_WIN)
67 print 'Successfully launched virtual webcam.'
68 elif sys.platform.startswith('darwin'):
69 subprocess.check_call(WEBCAM_MAC)
70 print 'Successfully launched virtual webcam.'
71 elif sys.platform.startswith('linux'):
Patrik Höglund99233532018-08-17 15:46:24 +020072 # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
73 # longer in use.
74 print 'Not implemented on Linux'
Oleh Prypin739b8162018-05-17 13:28:29 +020075
76 except Exception as e:
77 print 'Failed to launch virtual webcam: %s' % e
78 return False
79
80 return True
81
82
83def _ForcePythonInterpreter(cmd):
84 """Returns the fixed command line to call the right python executable."""
85 out = cmd[:]
86 if out[0] == 'python':
87 out[0] = sys.executable
88 elif out[0].endswith('.py'):
89 out.insert(0, sys.executable)
90 return out
91
92
93def Main(argv):
Oleh Prypin09ec3cf2018-05-17 16:15:59 +020094 if not IsWebCamRunning():
95 if not StartWebCam():
96 return 1
Oleh Prypin739b8162018-05-17 13:28:29 +020097
98 if argv:
99 return subprocess.call(_ForcePythonInterpreter(argv))
Oleh Prypin09ec3cf2018-05-17 16:15:59 +0200100 else:
101 return 0
Oleh Prypin739b8162018-05-17 13:28:29 +0200102
103
104if __name__ == '__main__':
105 sys.exit(Main(sys.argv[1:]))