Repository: pyscript/pyscript
File: pyscript.core/src/stdlib/pyweb/media.py (Python)

Code before:
    @classmethod
    async def load(cls, audio=False, video=True):
        options = window.Object.new()
        options.audio = audio
        if isinstance(video, bool):
            options.video = video
        else:
            # TODO: Think this can be simplified but need to check it on the pyodide side

            # TODO: this is pyodide specific. shouldn't be!
            options.video = window.Object.new()
            for k in video:
                setattr(
                    options.video,
                    k,
                    to_js(video[k], dict_converter=window.Object.fromEntries),
                )

        stream = await window.navigator.mediaDevices.getUserMedia(options)
        return stream

    async def get_stream(self):
        key = self.kind.replace("input", "").replace("output", "")
        options = {key: {"deviceId": {"exact": self.id}}}

        print("optioniiiiii", options)
        return await self.load(**options)


async def list_devices() -> list[dict]:
    """
    Return the list of the currently available media input and output devices,
    such as microphones, cameras, headsets, and so forth.

    Output:

        list(dict) - list of dictionaries representing the available media devices.
            Each dictionary has the following keys:
            * deviceId: a string that is an identifier for the represented device
                that is persisted across sessions. It is un-guessable by other
                applications and unique to the origin of the calling application.
                It is reset when the user clears cookies (for Private Browsing, a
                different identifier is used that is not persisted across sessions).

            * groupId: a string that is a group identifier. Two devices have the same
                group identifier if they belong to the same physical device — for
                example a monitor with both a built-in camera and a microphone.

            * kind: an enumerated value that is either "videoinput", "audioinput"
                or "audiooutput".


Review comment (bug):
is this meant to be kept in the code? if yes, could we make it a `window.console.debug(...)` instead ? we already had users complaining we log too much and at least via the explicit debug method, unless explicitly enabled, these logs would not show up.

Code after:

    def __getitem__(self, key):
        return getattr(self, key)

    @classmethod
    async def load(cls, audio=False, video=True):
        """Load the device stream."""
        options = window.Object.new()
        options.audio = audio
        if isinstance(video, bool):
            options.video = video
        else:
            # TODO: Think this can be simplified but need to check it on the pyodide side

            # TODO: this is pyodide specific. shouldn't be!
            options.video = window.Object.new()
            for k in video:
                setattr(
                    options.video,
                    k,
                    to_js(video[k], dict_converter=window.Object.fromEntries),
                )

        stream = await window.navigator.mediaDevices.getUserMedia(options)
        return stream

    async def get_stream(self):
        key = self.kind.replace("input", "").replace("output", "")
        options = {key: {"deviceId": {"exact": self.id}}}

        return await self.load(**options)


async def list_devices() -> list[dict]:
    """
    Return the list of the currently available media input and output devices,
    such as microphones, cameras, headsets, and so forth.

    Output:

        list(dict) - list of dictionaries representing the available media devices.
            Each dictionary has the following keys:
            * deviceId: a string that is an identifier for the represented device
                that is persisted across sessions. It is un-guessable by other
                applications and unique to the origin of the calling application.
                It is reset when the user clears cookies (for Private Browsing, a
                different identifier is used that is not persisted across sessions).

            * groupId: a string that is a group identifier. Two devices have the same
                group identifier if they belong to the same physical device — for
                example a monitor with both a built-in camera and a microphone.