Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 1 | #!/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 | |
| 12 | Returns a non-zero return code if the webcam could not be started. |
| 13 | |
| 14 | Prerequisites: |
| 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öglund | 9923353 | 2018-08-17 15:46:24 +0200 | [diff] [blame] | 20 | * Linux: Not implemented |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 21 | |
| 22 | NOTICE: When running this script as a buildbot step, make sure to set |
| 23 | usePTY=False for the build step when adding it, or the subprocess will die as |
| 24 | soon the step has executed. |
| 25 | |
| 26 | If any command line arguments are passed to the script, it is executed as a |
| 27 | command in a subprocess. |
| 28 | """ |
| 29 | |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 30 | # psutil is not installed on non-Linux machines by default. |
| 31 | import psutil # pylint: disable=F0401 |
| 32 | import subprocess |
| 33 | import sys |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 34 | |
| 35 | |
| 36 | WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam') |
| 37 | WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app') |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def 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öglund | 9923353 | 2018-08-17 15:46:24 +0200 | [diff] [blame] | 46 | # 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 Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 50 | 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 | |
| 63 | def 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öglund | 9923353 | 2018-08-17 15:46:24 +0200 | [diff] [blame] | 72 | # 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 Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 75 | |
| 76 | except Exception as e: |
| 77 | print 'Failed to launch virtual webcam: %s' % e |
| 78 | return False |
| 79 | |
| 80 | return True |
| 81 | |
| 82 | |
| 83 | def _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 | |
| 93 | def Main(argv): |
Oleh Prypin | 09ec3cf | 2018-05-17 16:15:59 +0200 | [diff] [blame] | 94 | if not IsWebCamRunning(): |
| 95 | if not StartWebCam(): |
| 96 | return 1 |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 97 | |
| 98 | if argv: |
| 99 | return subprocess.call(_ForcePythonInterpreter(argv)) |
Oleh Prypin | 09ec3cf | 2018-05-17 16:15:59 +0200 | [diff] [blame] | 100 | else: |
| 101 | return 0 |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 102 | |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | sys.exit(Main(sys.argv[1:])) |