blob: b68a484ea0a13e4daca75afcf9a63ed9cbaee0b6 [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.
Oleh Prypin739b8162018-05-17 13:28:29 +02009"""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 15:46:24 +020019* Linux: Not implemented
Oleh Prypin739b8162018-05-17 13:28:29 +020020
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 13:28:29 +020029# psutil is not installed on non-Linux machines by default.
30import psutil # pylint: disable=F0401
31import subprocess
32import sys
Oleh Prypin739b8162018-05-17 13:28:29 +020033
Oleh Prypin739b8162018-05-17 13:28:29 +020034WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam')
35WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app')
Oleh Prypin739b8162018-05-17 13:28:29 +020036
37
38def IsWebCamRunning():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010039 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 13:28:29 +020047 return True
Mirko Bonadei8cc66952020-10-30 10:13:45 +010048 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 13:28:29 +020059
60
61def StartWebCam():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010062 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 13:28:29 +020073
Mirko Bonadei8cc66952020-10-30 10:13:45 +010074 except Exception as e:
75 print 'Failed to launch virtual webcam: %s' % e
76 return False
Oleh Prypin739b8162018-05-17 13:28:29 +020077
Mirko Bonadei8cc66952020-10-30 10:13:45 +010078 return True
Oleh Prypin739b8162018-05-17 13:28:29 +020079
80
81def _ForcePythonInterpreter(cmd):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010082 """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 13:28:29 +020089
90
91def Main(argv):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010092 if not IsWebCamRunning():
93 if not StartWebCam():
94 return 1
Oleh Prypin739b8162018-05-17 13:28:29 +020095
Mirko Bonadei8cc66952020-10-30 10:13:45 +010096 if argv:
97 return subprocess.call(_ForcePythonInterpreter(argv))
98 else:
99 return 0
Oleh Prypin739b8162018-05-17 13:28:29 +0200100
101
102if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100103 sys.exit(Main(sys.argv[1:]))