MicroPython explorations.

This commit is contained in:
Nicholas H.Tollervey
2025-03-19 10:07:47 +00:00
parent 11e94f4ae9
commit 042fb93ef4
3 changed files with 25 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@@ -31,25 +31,22 @@ class Device:
@classmethod @classmethod
async def load(cls, audio=False, video=True): async def load(cls, audio=False, video=True):
"""Load the device stream.""" """
options = window.Object.new() Load the device stream.
options.audio = audio """
options = {}
options["audio"] = audio
if isinstance(video, bool): if isinstance(video, bool):
options.video = video options["video"] = video
else: else:
# TODO: Think this can be simplified but need to check it on the pyodide side options["video"] = {}
# TODO: this is pyodide specific. shouldn't be!
options.video = window.Object.new()
for k in video: for k in video:
setattr(options.video, k, to_js(video[k])) options["video"][k] = video[k]
return await window.navigator.mediaDevices.getUserMedia(to_js(options))
return await window.navigator.mediaDevices.getUserMedia(options)
async def get_stream(self): async def get_stream(self):
key = self.kind.replace("input", "").replace("output", "") key = self.kind.replace("input", "").replace("output", "")
options = {key: {"deviceId": {"exact": self.id}}} options = {key: {"deviceId": {"exact": self.id}}}
return await self.load(**options) return await self.load(**options)

View File

@@ -6,10 +6,6 @@ from pyscript import media
import upytest import upytest
@upytest.skip(
"Uses Pyodide-specific to_js function in MicroPython",
skip_when=upytest.is_micropython,
)
async def test_device_enumeration(): async def test_device_enumeration():
"""Test enumerating media devices.""" """Test enumerating media devices."""
devices = await media.list_devices() devices = await media.list_devices()
@@ -23,18 +19,21 @@ async def test_device_enumeration():
# Browser security might restrict actual values until permissions are granted # Browser security might restrict actual values until permissions are granted
assert hasattr(device, "id"), "Device should have id property" assert hasattr(device, "id"), "Device should have id property"
assert hasattr(device, "kind"), "Device should have kind property" assert hasattr(device, "kind"), "Device should have kind property"
assert device.kind in ["videoinput", "audioinput", "audiooutput"], \ assert device.kind in [
f"Device should have a valid kind, got: {device.kind}" "videoinput",
"audioinput",
"audiooutput",
], f"Device should have a valid kind, got: {device.kind}"
# Verify dictionary access works with actual device # Verify dictionary access works with actual device
assert device["id"] == device.id, "Dictionary access should match property access" assert (
assert device["kind"] == device.kind, "Dictionary access should match property access" device["id"] == device.id
), "Dictionary access should match property access"
assert (
device["kind"] == device.kind
), "Dictionary access should match property access"
@upytest.skip(
"Uses Pyodide-specific to_js function in MicroPython",
skip_when=upytest.is_micropython,
)
async def test_video_stream_acquisition(): async def test_video_stream_acquisition():
"""Test video stream.""" """Test video stream."""
try: try:
@@ -52,21 +51,16 @@ async def test_video_stream_acquisition():
except Exception as e: except Exception as e:
# If the browser blocks access, the test should still pass # If the browser blocks access, the test should still pass
# This is because we're testing the API works, not that permissions are granted # This is because we're testing the API works, not that permissions are granted
assert True, f"Stream acquisition attempted but may require permissions: {str(e)}" assert (
True
), f"Stream acquisition attempted but may require permissions: {str(e)}"
@upytest.skip(
"Uses Pyodide-specific to_js function in MicroPython",
skip_when=upytest.is_micropython,
)
async def test_custom_video_constraints(): async def test_custom_video_constraints():
"""Test loading video with custom constraints.""" """Test loading video with custom constraints."""
try: try:
# Define custom constraints # Define custom constraints
constraints = { constraints = {"width": 640, "height": 480}
"width": 640,
"height": 480
}
# Load stream with custom constraints # Load stream with custom constraints
stream = await media.Device.load(video=constraints) stream = await media.Device.load(video=constraints)