xref: /aosp_15_r20/external/webrtc/tools_webrtc/ensure_webcam_is_running.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/usr/bin/env vpython3
2
3# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS.  All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
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.
20* Linux: Not implemented
21
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
30import subprocess
31import sys
32# psutil is not installed on non-Linux machines by default.
33import psutil  # pylint: disable=F0401
34
35WEBCAM_WIN = ('schtasks', '/run', '/tn', 'ManyCam')
36WEBCAM_MAC = ('open', '/Applications/ManyCam/ManyCam.app')
37
38
39def IsWebCamRunning():
40  if sys.platform == 'win32':
41    process_name = 'ManyCam.exe'
42  elif sys.platform.startswith('darwin'):
43    process_name = 'ManyCam'
44  elif sys.platform.startswith('linux'):
45    # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
46    # longer in use.
47    print('Virtual webcam: no-op on Linux')
48    return True
49  else:
50    raise Exception('Unsupported platform: %s' % sys.platform)
51  for p in psutil.process_iter():
52    try:
53      if process_name == p.name:
54        print('Found a running virtual webcam (%s with PID %s)' %
55              (p.name, p.pid))
56        return True
57    except psutil.AccessDenied:
58      pass  # This is normal if we query sys processes, etc.
59  return False
60
61
62def StartWebCam():
63  try:
64    if sys.platform == 'win32':
65      subprocess.check_call(WEBCAM_WIN)
66      print('Successfully launched virtual webcam.')
67    elif sys.platform.startswith('darwin'):
68      subprocess.check_call(WEBCAM_MAC)
69      print('Successfully launched virtual webcam.')
70    elif sys.platform.startswith('linux'):
71      # TODO(bugs.webrtc.org/9636): Currently a no-op on Linux: sw webcams no
72      # longer in use.
73      print('Not implemented on Linux')
74
75  except Exception as e:
76    print('Failed to launch virtual webcam: %s' % e)
77    return False
78
79  return True
80
81
82def _ForcePythonInterpreter(cmd):
83  """Returns the fixed command line to call the right python executable."""
84  out = cmd[:]
85  if out[0] == 'vpython3':
86    out[0] = sys.executable
87  elif out[0].endswith('.py'):
88    out.insert(0, sys.executable)
89  return out
90
91
92def Main(argv):
93  if not IsWebCamRunning():
94    if not StartWebCam():
95      return 1
96
97  if argv:
98    return subprocess.call(_ForcePythonInterpreter(argv))
99  return 0
100
101
102if __name__ == '__main__':
103  sys.exit(Main(sys.argv[1:]))
104