Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Jan 18, 2025
1 parent e3dd4b0 commit 46cbc4a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
2 changes: 2 additions & 0 deletions kos-py/pykos/requirements-webrtc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ aiohttp
opencv-python
numpy
av
# For Linux video capture
v4l2-python3; sys_platform == 'linux'
39 changes: 35 additions & 4 deletions kos-py/pykos/webrtc/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
"""Defines a simple WebRTC client using aiortc."""

import asyncio
import logging
import platform
from abc import ABC, abstractmethod

import aiohttp
import cv2
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaPlayer

logger = logging.getLogger(__name__)


def get_platform_media_config() -> tuple[str, str]:
"""Get the appropriate media source format for the current platform.
Returns:
Tuple of (device_string, format_string)
"""
system = platform.system().lower()
if system == "darwin": # MacOS
return "default:none", "avfoundation"
elif system == "linux":
return "/dev/video0", "v4l2" # Video4Linux2
else:
logger.warning("Unsupported platform %s, falling back to test pattern", system)
return "color:red", "lavfi"


class WebRTCClient(ABC):
"""Abstract base class for WebRTC clients."""
Expand All @@ -24,16 +44,27 @@ def __init__(self, server_url: str = "http://localhost:8080") -> None:

async def create_local_tracks(self) -> tuple[MediaStreamTrack | None, MediaStreamTrack | None]:
"""Create local video and audio tracks from webcam/microphone."""
device_string, format_string = get_platform_media_config()

try:
# Try to use the default webcam and microphone
player = MediaPlayer(
"default:none", format="avfoundation", options={"framerate": "30", "video_size": "640x480"}
device_string,
format=format_string,
options={"framerate": "30", "video_size": "640x480"},
)
if not player.video:
raise RuntimeError("No video device found")
return player.video, player.audio

except Exception as e:
print(f"Could not open webcam/microphone ({e}), using test sources")
# Fallback to test sources
player = MediaPlayer("color:red", format="lavfi", options={"framerate": "30", "video_size": "640x480"})
logger.warning("Could not open webcam/microphone: %s", e)
logger.warning("Using test sources instead")
player = MediaPlayer(
"color:red",
format="lavfi",
options={"framerate": "30", "video_size": "640x480"},
)
return player.video, None

async def _setup_tracks(self) -> None:
Expand Down
41 changes: 36 additions & 5 deletions kos-py/pykos/webrtc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,56 @@

import asyncio
import json
import logging
import platform
from typing import Any, Coroutine, List, Set

from aiohttp import web
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaPlayer

logger = logging.getLogger(__name__)

async def create_local_tracks():

def get_platform_media_config() -> tuple[str, str]:
"""Get the appropriate media source format for the current platform.
Returns:
Tuple of (device_string, format_string)
"""
system = platform.system().lower()
if system == "darwin": # MacOS
return "default:none", "avfoundation"
elif system == "linux":
return "/dev/video0", "v4l2" # Video4Linux2
else:
logger.warning("Unsupported platform %s, falling back to test pattern", system)
return "color:blue", "lavfi"


async def create_local_tracks() -> tuple[MediaStreamTrack | None, MediaStreamTrack | None]:
"""Create local video and audio tracks from webcam/microphone."""
device_string, format_string = get_platform_media_config()

try:
# Try to use the default webcam and microphone
player = MediaPlayer(
"default:none", format="avfoundation", options={"framerate": "30", "video_size": "640x480"}
device_string,
format=format_string,
options={"framerate": "30", "video_size": "640x480"},
)
if not player.video:
raise RuntimeError("No video device found")
return player.video, player.audio

except Exception as e:
print(f"Could not open webcam/microphone ({e}), using test sources")
# Fallback to test sources
player = MediaPlayer("color:blue", format="lavfi", options={"framerate": "30", "video_size": "640x480"})
logger.warning("Could not open webcam/microphone: %s", e)
logger.warning("Using test sources instead")
player = MediaPlayer(
"color:blue",
format="lavfi",
options={"framerate": "30", "video_size": "640x480"},
)
return player.video, None


Expand Down

0 comments on commit 46cbc4a

Please sign in to comment.