|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from fishjam.agent import Agent, AgentSession, IncomingTrackData, IncomingTrackImage |
| 4 | +from fishjam.integrations.gemini import GeminiIntegration |
| 5 | + |
| 6 | +from .config import IMAGE_CAPTURE_INTERVAL |
| 7 | +from .session import MultimodalSession |
| 8 | +from .worker import BackgroundWorker |
| 9 | + |
| 10 | + |
| 11 | +class MultimodalAgent: |
| 12 | + def __init__(self, room_id: str, agent: Agent, worker: BackgroundWorker): |
| 13 | + self._room_id = room_id |
| 14 | + self._agent = agent |
| 15 | + self._worker = worker |
| 16 | + self._task: asyncio.Task[None] | None = None |
| 17 | + self._capture_task: asyncio.Task[None] | None = None |
| 18 | + |
| 19 | + # Session management per peer |
| 20 | + self._sessions: dict[str, MultimodalSession] = {} |
| 21 | + |
| 22 | + # Track caching: peer_id -> set of video track_ids |
| 23 | + self._peer_tracks: dict[str, set[str]] = {} |
| 24 | + # Reverse lookup: track_id -> peer_id |
| 25 | + self._track_to_peer: dict[str, str] = {} |
| 26 | + |
| 27 | + # Agent session reference for capturing images and sending audio |
| 28 | + self._agent_session: AgentSession | None = None |
| 29 | + |
| 30 | + async def _start(self): |
| 31 | + async with self._agent.connect() as session: |
| 32 | + self._agent_session = session |
| 33 | + |
| 34 | + # Create output track for Gemini audio responses |
| 35 | + |
| 36 | + self._output_track = await session.add_track( |
| 37 | + GeminiIntegration.GEMINI_OUTPUT_AUDIO_SETTINGS |
| 38 | + ) |
| 39 | + print(f"Agent connected to room {self._room_id}") |
| 40 | + |
| 41 | + async for message in session.receive(): |
| 42 | + match message: |
| 43 | + case IncomingTrackImage(track_id=track_id, data=data): |
| 44 | + peer_id = self._track_to_peer.get(track_id) |
| 45 | + if peer_id and peer_id in self._sessions: |
| 46 | + self._sessions[peer_id].send_image(data) |
| 47 | + print(f"Sent image from {track_id} to {peer_id}") |
| 48 | + |
| 49 | + case IncomingTrackData(peer_id=peer_id, data=data): |
| 50 | + if peer_id in self._sessions: |
| 51 | + self._sessions[peer_id].send_audio(data) |
| 52 | + |
| 53 | + self._agent_session = None |
| 54 | + print(f"Agent disconnected from room {self._room_id}") |
| 55 | + |
| 56 | + async def _periodic_capture(self): |
| 57 | + while True: |
| 58 | + await asyncio.sleep(IMAGE_CAPTURE_INTERVAL) |
| 59 | + |
| 60 | + if not self._agent_session: |
| 61 | + continue |
| 62 | + |
| 63 | + # Capture images from all known video tracks |
| 64 | + for track_id in self._track_to_peer.keys(): |
| 65 | + try: |
| 66 | + await self._agent_session.capture_image(track_id) |
| 67 | + print(f"Requested image capture for track {track_id}") |
| 68 | + except Exception as e: |
| 69 | + print(f"Error capturing image from track {track_id}: {e}") |
| 70 | + |
| 71 | + def _handle_audio_response(self, peer_id: str, audio: bytes): |
| 72 | + if self._output_track: |
| 73 | + self._worker.run_in_background(self._output_track.send_chunk(audio)) |
| 74 | + |
| 75 | + def on_peer_enter(self, peer_id: str): |
| 76 | + if peer_id in self._sessions: |
| 77 | + return |
| 78 | + |
| 79 | + # Initialize track cache for this peer |
| 80 | + self._peer_tracks[peer_id] = set() |
| 81 | + |
| 82 | + # Start agent connection if this is the first peer |
| 83 | + if len(self._sessions) == 0: |
| 84 | + self._task = self._worker.run_in_background(self._start()) |
| 85 | + capture_coro = self._periodic_capture() |
| 86 | + self._capture_task = self._worker.run_in_background(capture_coro) |
| 87 | + |
| 88 | + # Create multimodal session for this peer |
| 89 | + def on_audio(audio: bytes, pid: str = peer_id): |
| 90 | + self._handle_audio_response(pid, audio) |
| 91 | + |
| 92 | + session = MultimodalSession(on_audio) |
| 93 | + self._sessions[peer_id] = session |
| 94 | + self._worker.run_in_background(session.start(peer_id)) |
| 95 | + |
| 96 | + def on_peer_leave(self, peer_id: str): |
| 97 | + if peer_id not in self._sessions: |
| 98 | + return |
| 99 | + |
| 100 | + # Clean up track cache |
| 101 | + if peer_id in self._peer_tracks: |
| 102 | + for track_id in self._peer_tracks[peer_id]: |
| 103 | + self._track_to_peer.pop(track_id, None) |
| 104 | + del self._peer_tracks[peer_id] |
| 105 | + |
| 106 | + # End the session |
| 107 | + self._sessions.pop(peer_id).end() |
| 108 | + |
| 109 | + # Stop agent if no more sessions |
| 110 | + if len(self._sessions) == 0: |
| 111 | + if self._task is not None: |
| 112 | + self._task.cancel() |
| 113 | + self._task = None |
| 114 | + if self._capture_task is not None: |
| 115 | + self._capture_task.cancel() |
| 116 | + self._capture_task = None |
| 117 | + |
| 118 | + def on_track_added(self, peer_id: str, track_id: str, is_video: bool): |
| 119 | + if not is_video: |
| 120 | + return |
| 121 | + |
| 122 | + if peer_id not in self._peer_tracks: |
| 123 | + self._peer_tracks[peer_id] = set() |
| 124 | + |
| 125 | + self._peer_tracks[peer_id].add(track_id) |
| 126 | + self._track_to_peer[track_id] = peer_id |
| 127 | + print(f"Added video track {track_id} for peer {peer_id}") |
| 128 | + |
| 129 | + def on_track_removed(self, peer_id: str, track_id: str, is_video: bool): |
| 130 | + if not is_video: |
| 131 | + return |
| 132 | + |
| 133 | + if peer_id in self._peer_tracks: |
| 134 | + self._peer_tracks[peer_id].discard(track_id) |
| 135 | + self._track_to_peer.pop(track_id, None) |
| 136 | + print(f"Removed video track {track_id} for peer {peer_id}") |
0 commit comments