|
| 1 | +from pyodide.ffi import to_js |
| 2 | +from pyscript import window |
| 3 | + |
| 4 | + |
| 5 | +class Device: |
| 6 | + """Device represents a media input or output device, such as a microphone, |
| 7 | + camera, or headset. |
| 8 | + """ |
| 9 | + |
| 10 | + def __init__(self, device): |
| 11 | + self._js = device |
| 12 | + |
| 13 | + @property |
| 14 | + def id(self): |
| 15 | + return self._js.deviceId |
| 16 | + |
| 17 | + @property |
| 18 | + def group(self): |
| 19 | + return self._js.groupId |
| 20 | + |
| 21 | + @property |
| 22 | + def kind(self): |
| 23 | + return self._js.kind |
| 24 | + |
| 25 | + @property |
| 26 | + def label(self): |
| 27 | + return self._js.label |
| 28 | + |
| 29 | + def __getitem__(self, key): |
| 30 | + return getattr(self, key) |
| 31 | + |
| 32 | + @classmethod |
| 33 | + async def load(cls, audio=False, video=True): |
| 34 | + """Load the device stream.""" |
| 35 | + options = window.Object.new() |
| 36 | + options.audio = audio |
| 37 | + if isinstance(video, bool): |
| 38 | + options.video = video |
| 39 | + else: |
| 40 | + # TODO: Think this can be simplified but need to check it on the pyodide side |
| 41 | + |
| 42 | + # TODO: this is pyodide specific. shouldn't be! |
| 43 | + options.video = window.Object.new() |
| 44 | + for k in video: |
| 45 | + setattr( |
| 46 | + options.video, |
| 47 | + k, |
| 48 | + to_js(video[k], dict_converter=window.Object.fromEntries), |
| 49 | + ) |
| 50 | + |
| 51 | + stream = await window.navigator.mediaDevices.getUserMedia(options) |
| 52 | + return stream |
| 53 | + |
| 54 | + async def get_stream(self): |
| 55 | + key = self.kind.replace("input", "").replace("output", "") |
| 56 | + options = {key: {"deviceId": {"exact": self.id}}} |
| 57 | + |
| 58 | + return await self.load(**options) |
| 59 | + |
| 60 | + |
| 61 | +async def list_devices() -> list[dict]: |
| 62 | + """ |
| 63 | + Return the list of the currently available media input and output devices, |
| 64 | + such as microphones, cameras, headsets, and so forth. |
| 65 | +
|
| 66 | + Output: |
| 67 | +
|
| 68 | + list(dict) - list of dictionaries representing the available media devices. |
| 69 | + Each dictionary has the following keys: |
| 70 | + * deviceId: a string that is an identifier for the represented device |
| 71 | + that is persisted across sessions. It is un-guessable by other |
| 72 | + applications and unique to the origin of the calling application. |
| 73 | + It is reset when the user clears cookies (for Private Browsing, a |
| 74 | + different identifier is used that is not persisted across sessions). |
| 75 | +
|
| 76 | + * groupId: a string that is a group identifier. Two devices have the same |
| 77 | + group identifier if they belong to the same physical device — for |
| 78 | + example a monitor with both a built-in camera and a microphone. |
| 79 | +
|
| 80 | + * kind: an enumerated value that is either "videoinput", "audioinput" |
| 81 | + or "audiooutput". |
| 82 | +
|
| 83 | + * label: a string describing this device (for example "External USB |
| 84 | + Webcam"). |
| 85 | +
|
| 86 | + Note: the returned list will omit any devices that are blocked by the document |
| 87 | + Permission Policy: microphone, camera, speaker-selection (for output devices), |
| 88 | + and so on. Access to particular non-default devices is also gated by the |
| 89 | + Permissions API, and the list will omit devices for which the user has not |
| 90 | + granted explicit permission. |
| 91 | + """ |
| 92 | + # https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices |
| 93 | + return [ |
| 94 | + Device(obj) for obj in await window.navigator.mediaDevices.enumerateDevices() |
| 95 | + ] |
0 commit comments