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. |
| 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 | |
| 24 | NOTICE: When running this script as a buildbot step, make sure to set |
| 25 | usePTY=False for the build step when adding it, or the subprocess will die as |
| 26 | soon the step has executed. |
| 27 | |
| 28 | If any command line arguments are passed to the script, it is executed as a |
| 29 | command in a subprocess. |
| 30 | """ |
| 31 | |
| 32 | import os |
| 33 | # psutil is not installed on non-Linux machines by default. |
| 34 | import psutil # pylint: disable=F0401 |
| 35 | import subprocess |
| 36 | import sys |
| 37 | import time |
| 38 | |
| 39 | |
| 40 | WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam') |
| 41 | WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app') |
| 42 | E = os.path.expandvars |
| 43 | WEBCAM_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 | |
| 50 | def 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 | |
| 70 | def 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 | |
| 102 | def _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 | |
| 112 | def Main(argv): |
Oleh Prypin | 09ec3cf | 2018-05-17 16:15:59 +0200 | [diff] [blame^] | 113 | if not IsWebCamRunning(): |
| 114 | if not StartWebCam(): |
| 115 | return 1 |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 116 | |
| 117 | if argv: |
| 118 | return subprocess.call(_ForcePythonInterpreter(argv)) |
Oleh Prypin | 09ec3cf | 2018-05-17 16:15:59 +0200 | [diff] [blame^] | 119 | else: |
| 120 | return 0 |
Oleh Prypin | 739b816 | 2018-05-17 13:28:29 +0200 | [diff] [blame] | 121 | |
| 122 | |
| 123 | if __name__ == '__main__': |
| 124 | sys.exit(Main(sys.argv[1:])) |