blob: b68a484ea0a13e4daca75afcf9a63ed9cbaee0b6 [file] [log] [blame]
Oleh Prypin739b8162018-05-17 11:28:291#!/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.
Oleh Prypin739b8162018-05-17 11:28:299"""Checks if a virtual webcam is running and starts it if not.
10
11Returns a non-zero return code if the webcam could not be started.
12
13Prerequisites:
14* The Python interpreter must have the psutil package installed.
15* Windows: a scheduled task named 'ManyCam' must exist and be configured to
16 launch ManyCam preconfigured to auto-play the test clip.
17* Mac: ManyCam must be installed in the default location and be preconfigured
18 to auto-play the test clip.
Patrik Höglund99233532018-08-17 13:46:2419* Linux: Not implemented
Oleh Prypin739b8162018-05-17 11:28:2920
21NOTICE: When running this script as a buildbot step, make sure to set
22usePTY=False for the build step when adding it, or the subprocess will die as
23soon the step has executed.
24
25If any command line arguments are passed to the script, it is executed as a
26command in a subprocess.
27"""
28
Oleh Prypin739b8162018-05-17 11:28:2929# psutil is not installed on non-Linux machines by default.
30import psutil # pylint: disable=F0401
31import subprocess
32import sys
Oleh Prypin739b8162018-05-17 11:28:2933
Oleh Prypin739b8162018-05-17 11:28:2934WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam')
35WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app')
Oleh Prypin739b8162018-05-17 11:28:2936
37
38def IsWebCamRunning():
Mirko Bonadei8cc66952020-10-30 09:13:4539 if sys.platform == 'win32':
40 process_name = 'ManyCam.exe'
41 elif sys.platform.startswith('darwin'):
42 process_name = 'ManyCam'
43 elif sys.platform.startswith('linux'):
44 # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
45 # longer in use.
46 print 'Virtual webcam: no-op on Linux'
Oleh Prypin739b8162018-05-17 11:28:2947 return True
Mirko Bonadei8cc66952020-10-30 09:13:4548 else:
49 raise Exception('Unsupported platform: %s' % sys.platform)
50 for p in psutil.process_iter():
51 try:
52 if process_name == p.name:
53 print 'Found a running virtual webcam (%s with PID %s)' % (
54 p.name, p.pid)
55 return True
56 except psutil.AccessDenied:
57 pass # This is normal if we query sys processes, etc.
58 return False
Oleh Prypin739b8162018-05-17 11:28:2959
60
61def StartWebCam():
Mirko Bonadei8cc66952020-10-30 09:13:4562 try:
63 if sys.platform == 'win32':
64 subprocess.check_call(WEBCAM_WIN)
65 print 'Successfully launched virtual webcam.'
66 elif sys.platform.startswith('darwin'):
67 subprocess.check_call(WEBCAM_MAC)
68 print 'Successfully launched virtual webcam.'
69 elif sys.platform.startswith('linux'):
70 # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
71 # longer in use.
72 print 'Not implemented on Linux'
Oleh Prypin739b8162018-05-17 11:28:2973
Mirko Bonadei8cc66952020-10-30 09:13:4574 except Exception as e:
75 print 'Failed to launch virtual webcam: %s' % e
76 return False
Oleh Prypin739b8162018-05-17 11:28:2977
Mirko Bonadei8cc66952020-10-30 09:13:4578 return True
Oleh Prypin739b8162018-05-17 11:28:2979
80
81def _ForcePythonInterpreter(cmd):
Mirko Bonadei8cc66952020-10-30 09:13:4582 """Returns the fixed command line to call the right python executable."""
83 out = cmd[:]
84 if out[0] == 'python':
85 out[0] = sys.executable
86 elif out[0].endswith('.py'):
87 out.insert(0, sys.executable)
88 return out
Oleh Prypin739b8162018-05-17 11:28:2989
90
91def Main(argv):
Mirko Bonadei8cc66952020-10-30 09:13:4592 if not IsWebCamRunning():
93 if not StartWebCam():
94 return 1
Oleh Prypin739b8162018-05-17 11:28:2995
Mirko Bonadei8cc66952020-10-30 09:13:4596 if argv:
97 return subprocess.call(_ForcePythonInterpreter(argv))
98 else:
99 return 0
Oleh Prypin739b8162018-05-17 11:28:29100
101
102if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 09:13:45103 sys.exit(Main(sys.argv[1:]))