Skip to navigation

Bridge Server Architecture & Session Management

View as Markdown

Build a server that sits between MeetStream and your AI stack. The bridge receives real-time meeting audio, manages one session per MeetStream connection, routes audio through your processing pipeline, and sends responses (audio, chat, images) back into the meeting.


Architecture

MeetStream Bridge Architecture

The bridge server exposes two WebSocket endpoints. Each MeetStream connection opens one to each:

EndpointDirectionFormatPurpose
/bridge/audioMeetStream → YouBinary framesLive meeting audio with speaker metadata
/bridgeMeetStream ↔ YouJSON textCommands: send audio, chat, images, interrupt

Each MeetStream connection is identified by a bot_id. The session manager maintains one AI session per bot_id, created on first connection and torn down when both WebSockets disconnect.


Session Lifecycle

MeetStream Bridge Session Lifecycle

Key rules:

  • Either channel can arrive first. The session is created on whichever connects first.
  • Both channels share the same bot_id and the same AI session.
  • Cleanup happens when both WebSockets have disconnected for a given bot_id.

Core Components

1. Session Manager

The session manager is the central coordinator. It maps bot_id to:

  • An AI session (your STT/LLM/TTS pipeline)
  • References to the audio and control WebSockets
  • Per-session locks to prevent race conditions during creation
import asyncio
import json
import logging
from typing import Any, Dict, Optional
from fastapi import WebSocket
from starlette.websockets import WebSocketState
logger = logging.getLogger("bridge")
class SessionManager:
def __init__(self):
self.sessions: Dict[str, Any] = {}
self.audio_ws: Dict[str, WebSocket] = {}
self.control_ws: Dict[str, WebSocket] = {}
self._locks: Dict[str, asyncio.Lock] = {}
def _lock_for(self, bot_id: str) -> asyncio.Lock:
if bot_id not in self._locks:
self._locks[bot_id] = asyncio.Lock()
return self._locks[bot_id]
async def ensure_session(self, bot_id: str):
"""Create an AI session for this bot_id if one doesn't exist."""
async with self._lock_for(bot_id):
if bot_id in self.sessions:
return
session = await self._create_session(bot_id)
self.sessions[bot_id] = session
logger.info(f"[{bot_id}] Session created")
async def close_session(self, bot_id: str):
"""Tear down the AI session for this bot_id."""
async with self._lock_for(bot_id):
session = self.sessions.pop(bot_id, None)
if session:
await self._destroy_session(bot_id, session)
logger.info(f"[{bot_id}] Session closed")
async def maybe_cleanup(self, bot_id: str):
"""Close the session if both WebSockets have disconnected."""
if bot_id not in self.audio_ws and bot_id not in self.control_ws:
await self.close_session(bot_id)
async def _create_session(self, bot_id: str) -> Any:
"""
Replace this with your AI pipeline initialization.
Examples: start an STT stream, create an LLM session,
initialize a TTS engine, connect to a realtime API, etc.
"""
raise NotImplementedError("Implement _create_session()")
async def _destroy_session(self, bot_id: str, session: Any):
"""
Replace this with your AI pipeline teardown.
Close connections, flush buffers, release resources.
"""
raise NotImplementedError("Implement _destroy_session()")

2. Audio Decoder

Decodes the binary frame format from MeetStream. See Live Audio Capture & Frame Decoding for the full specification.

Google Meet and Teams send 0x01 frames at 48 kHz. Zoom sends 0x04 frames, which carry the sample rate and the channel count in the header. The decoder returns the sample rate with the audio.

from typing import Optional, Tuple
def decode_audio_frame(data: bytes) -> Optional[Tuple[str, str, int, bytes]]:
"""Decode a MeetStream binary audio frame.
Returns (speaker_id, speaker_name, sample_rate, pcm_bytes) or None.
"""
if len(data) < 5:
return None
if data[0] == 0x01: # Google Meet and Microsoft Teams: 48 kHz mono
sample_rate, off = 48000, 1
elif data[0] == 0x04: # Zoom: sample rate and channels in the header
sample_rate, off = int.from_bytes(data[1:5], "little"), 6
else:
return None
sid_len = int.from_bytes(data[off : off + 2], "little")
off += 2
speaker_id = data[off : off + sid_len].decode("utf-8")
off += sid_len
sname_len = int.from_bytes(data[off : off + 2], "little")
off += 2
speaker_name = data[off : off + sname_len].decode("utf-8")
off += sname_len
return speaker_id, speaker_name, sample_rate, data[off:]

3. Audio Resampler

Google Meet and Teams audio arrives at 48 kHz. Zoom audio arrives at the rate in the frame header (32 kHz). Resample to the rate of your model before you send audio to it (for example, 24 kHz for OpenAI Realtime or 16 kHz for Whisper), and resample back to 48 kHz before you send audio to the meeting.

import numpy as np
def resample_pcm16(pcm_bytes: bytes, src_hz: int, dst_hz: int) -> bytes:
"""Resample PCM16 LE audio between sample rates."""
if src_hz == dst_hz:
return pcm_bytes
x = np.frombuffer(pcm_bytes, dtype=np.int16).astype(np.float32)
n_out = int(len(x) * dst_hz / src_hz)
t_src = np.linspace(0, 1, len(x), endpoint=False)
t_dst = np.linspace(0, 1, n_out, endpoint=False)
y = np.interp(t_dst, t_src, x)
return np.clip(y, -32768, 32767).astype(np.int16).tobytes()
# Incoming audio: use the sample_rate that decode_audio_frame() returns
MODEL_RATE = 24000 # Your model's expected rate
OUTGOING_RATE = 48000 # Send audio back to the meeting at 48 kHz

4. Speaker Filter

Filter out MeetStream’s own audio to prevent echo/feedback loops. The display name appears in the speaker_name field.

IGNORED_SPEAKERS = {
"My Bot Name",
"Meeting Assistant",
}
AGENT_KEYWORDS = ["bot", "agent", "assistant", "ai"]
def should_ignore_speaker(speaker_name: str) -> bool:
if speaker_name in IGNORED_SPEAKERS:
return True
lower = speaker_name.lower()
return any(kw in lower for kw in AGENT_KEYWORDS)

5. Command Sender

Helper functions to build outbound commands for the control channel. See Meeting Control & Command Patterns for the full command reference.

import base64
async def safe_send(ws: WebSocket, payload: dict):
"""Send JSON to a WebSocket, swallowing errors."""
try:
if ws.client_state == WebSocketState.CONNECTED:
await ws.send_text(json.dumps(payload))
except Exception as e:
logger.warning(f"WebSocket send failed: {e}")
async def send_audio_to_meeting(ws: WebSocket, bot_id: str, pcm_bytes: bytes, sample_rate: int = 48000):
await safe_send(ws, {
"command": "sendaudio",
"bot_id": bot_id,
"audiochunk": base64.b64encode(pcm_bytes).decode("utf-8"),
"sample_rate": sample_rate,
"encoding": "pcm16",
"channels": 1,
"endianness": "little",
})
async def send_chat_to_meeting(ws: WebSocket, bot_id: str, text: str):
await safe_send(ws, {
"command": "sendmsg",
"bot_id": bot_id,
"message": text,
"msg": text,
})
async def send_interrupt_to_meeting(ws: WebSocket, bot_id: str):
await safe_send(ws, {
"command": "interrupt",
"bot_id": bot_id,
"action": "clear_audio_queue",
})
async def send_resume_audio_to_meeting(ws: WebSocket, bot_id: str, resume_id: str):
"""Send before the first sendaudio of each new response."""
await safe_send(ws, {
"command": "resume_audio",
"bot_id": bot_id,
"resume_id": resume_id,
})

On Zoom and Teams, send resume_audio after an interrupt. Google Meet ignores resume_audio, so you can send it on all platforms.


WebSocket Endpoints

Audio Endpoint: /bridge/audio

Receives binary audio frames from the meeting. Decodes speaker metadata, filters out MeetStream’s own audio, and routes PCM to your AI pipeline.

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
app = FastAPI()
manager = SessionManager()
@app.websocket("/bridge/audio")
async def audio_endpoint(websocket: WebSocket):
await websocket.accept()
bot_id = None
try:
while True:
raw = await websocket.receive()
if raw["type"] == "websocket.disconnect":
break
# Text frame: the handshake
if "text" in raw and raw["text"]:
data = json.loads(raw["text"])
if data.get("type") == "ready":
bot_id = data.get("bot_id")
if not bot_id:
await websocket.close(code=1003)
return
manager.audio_ws[bot_id] = websocket
await manager.ensure_session(bot_id)
await safe_send(websocket, {
"type": "ack",
"message": f"Audio channel bound to {bot_id}",
})
continue
# Binary frames: speaker-tagged PCM audio (0x01 or 0x04)
if "bytes" in raw and raw["bytes"] and bot_id:
result = decode_audio_frame(raw["bytes"])
if result is None:
continue
speaker_id, speaker_name, sample_rate, pcm_bytes = result
if should_ignore_speaker(speaker_name):
continue
if pcm_bytes:
pcm_resampled = resample_pcm16(pcm_bytes, sample_rate, MODEL_RATE)
session = manager.sessions.get(bot_id)
if session:
await session.send_audio(pcm_resampled)
except WebSocketDisconnect:
pass
except Exception as e:
logger.error(f"[{bot_id}] Audio endpoint error: {e}")
finally:
if bot_id:
manager.audio_ws.pop(bot_id, None)
await manager.maybe_cleanup(bot_id)

Control Endpoint: /bridge

Your AI pipeline sends commands (sendaudio, sendmsg, interrupt, resume_audio) over this connection. MeetStream sends the ready handshake and command acknowledgements.

@app.websocket("/bridge")
async def control_endpoint(websocket: WebSocket):
await websocket.accept()
bot_id = None
try:
# Wait for handshake
init = json.loads(await websocket.receive_text())
if init.get("type") != "ready" or not init.get("bot_id"):
await websocket.close(code=1003)
return
bot_id = init["bot_id"]
manager.control_ws[bot_id] = websocket
await manager.ensure_session(bot_id)
await safe_send(websocket, {
"command": "ack",
"bot_id": bot_id,
"message": f"Control channel bound to {bot_id}",
})
# Start the event pump that forwards AI output to MeetStream
asyncio.create_task(event_pump(bot_id))
# Main loop: read command acknowledgements from MeetStream
while True:
data = json.loads(await websocket.receive_text())
logger.debug(f"[{bot_id}] Control message: {data}")
except WebSocketDisconnect:
pass
except Exception as e:
logger.error(f"[{bot_id}] Control endpoint error: {e}")
finally:
if bot_id:
manager.control_ws.pop(bot_id, None)
await manager.maybe_cleanup(bot_id)

Event Pump

The event pump is a background task that continuously reads output from your AI pipeline and forwards it to the meeting through the control WebSocket. This is the outbound half of the bridge.

async def event_pump(bot_id: str):
"""Forward AI pipeline output to the MeetStream control channel."""
session = manager.sessions.get(bot_id)
if not session:
return
turn = 0
audio_resumed = False # True after resume_audio is sent for the current response
try:
async for event in session:
ws = manager.control_ws.get(bot_id)
if not ws or ws.client_state != WebSocketState.CONNECTED:
continue
# AI produced audio → send to meeting
if event.type == "audio":
if not audio_resumed:
# Zoom and Teams hold audio after an interrupt until resume_audio.
# Google Meet ignores resume_audio, so send it on every platform.
await send_resume_audio_to_meeting(ws, bot_id, f"turn-{turn}")
audio_resumed = True
pcm_model = event.audio_bytes # at MODEL_RATE
pcm_out = resample_pcm16(pcm_model, MODEL_RATE, OUTGOING_RATE)
await send_audio_to_meeting(ws, bot_id, pcm_out, OUTGOING_RATE)
# AI speech was interrupted → clear the playback queue
elif event.type == "audio_interrupted":
await send_interrupt_to_meeting(ws, bot_id)
turn += 1
audio_resumed = False
# AI produced a text response → send as chat
elif event.type == "text_response":
await send_chat_to_meeting(ws, bot_id, event.text)
except Exception as e:
logger.error(f"[{bot_id}] Event pump error: {e}")

Note: The event types above (audio, audio_interrupted, text_response) are illustrative. Replace them with whatever events your AI framework emits. Keep the resume_audio step: on Zoom and Teams, send resume_audio after an interrupt. Google Meet ignores resume_audio, so you can send it on all platforms.


Full Working Skeleton

A minimal but complete bridge server. Replace the YourAISession class with your actual AI pipeline.

import asyncio
import base64
import json
import logging
from contextlib import asynccontextmanager
from typing import Any, Dict, Optional, Tuple
import numpy as np
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from starlette.websockets import WebSocketState
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("bridge")
# ── Configuration ─────────────────────────────────────────────────────────────
# Incoming audio: Google Meet and Teams send 48 kHz, Zoom sends the rate in the frame header
MODEL_RATE = 24000 # Your AI model's expected sample rate
OUTGOING_RATE = 48000 # Send audio back to the meeting at 48 kHz
IGNORED_SPEAKERS = {"My Bot Name"}
AGENT_KEYWORDS = ["bot", "agent", "assistant"]
# ── Audio utilities ───────────────────────────────────────────────────────────
def decode_audio_frame(data: bytes) -> Optional[Tuple[str, str, int, bytes]]:
"""Return (speaker_id, speaker_name, sample_rate, pcm_bytes), or None."""
if len(data) < 5:
return None
if data[0] == 0x01: # Google Meet and Microsoft Teams: 48 kHz mono
sample_rate, off = 48000, 1
elif data[0] == 0x04: # Zoom: sample rate and channels in the header
sample_rate, off = int.from_bytes(data[1:5], "little"), 6
else:
return None
sid_len = int.from_bytes(data[off : off + 2], "little")
off += 2
sid = data[off : off + sid_len].decode("utf-8")
off += sid_len
sname_len = int.from_bytes(data[off : off + 2], "little")
off += 2
sname = data[off : off + sname_len].decode("utf-8")
off += sname_len
return sid, sname, sample_rate, data[off:]
def resample_pcm16(pcm_bytes: bytes, src_hz: int, dst_hz: int) -> bytes:
if src_hz == dst_hz:
return pcm_bytes
x = np.frombuffer(pcm_bytes, dtype=np.int16).astype(np.float32)
n_out = int(len(x) * dst_hz / src_hz)
t_src = np.linspace(0, 1, len(x), endpoint=False)
t_dst = np.linspace(0, 1, n_out, endpoint=False)
y = np.interp(t_dst, t_src, x)
return np.clip(y, -32768, 32767).astype(np.int16).tobytes()
def should_ignore_speaker(name: str) -> bool:
if name in IGNORED_SPEAKERS:
return True
lower = name.lower()
return any(kw in lower for kw in AGENT_KEYWORDS)
# ── Safe WebSocket send ───────────────────────────────────────────────────────
async def safe_send(ws: WebSocket, payload: dict):
try:
if ws.client_state == WebSocketState.CONNECTED:
await ws.send_text(json.dumps(payload))
except Exception as e:
logger.warning(f"send failed: {e}")
# ── Your AI session (replace this) ───────────────────────────────────────────
class YourAISession:
"""
Stub. Replace with your actual AI pipeline.
This could be:
- An OpenAI Realtime API session
- A Whisper STT + GPT + TTS pipeline
- A LiveKit agent session
- Any other audio-in/audio-out system
"""
def __init__(self):
# Your pipeline puts output events on this queue.
self.output: asyncio.Queue = asyncio.Queue()
async def send_audio(self, pcm_bytes: bytes):
"""Feed PCM audio (at MODEL_RATE) into your pipeline."""
pass
async def interrupt(self):
"""Signal the pipeline to stop generating."""
pass
async def events(self):
"""Yield output events from your pipeline.
Expected event shapes (adapt to your framework):
{"type": "audio", "pcm_bytes": bytes}
{"type": "audio_interrupted"}
{"type": "text", "content": str}
"""
while True:
yield await self.output.get()
# ── Session Manager ───────────────────────────────────────────────────────────
class SessionManager:
def __init__(self):
self.sessions: Dict[str, YourAISession] = {}
self.audio_ws: Dict[str, WebSocket] = {}
self.control_ws: Dict[str, WebSocket] = {}
self._locks: Dict[str, asyncio.Lock] = {}
def _lock(self, bot_id: str) -> asyncio.Lock:
if bot_id not in self._locks:
self._locks[bot_id] = asyncio.Lock()
return self._locks[bot_id]
async def ensure_session(self, bot_id: str):
async with self._lock(bot_id):
if bot_id in self.sessions:
return
self.sessions[bot_id] = YourAISession()
asyncio.create_task(self._event_pump(bot_id))
logger.info(f"[{bot_id}] Session created")
async def close_session(self, bot_id: str):
async with self._lock(bot_id):
self.sessions.pop(bot_id, None)
logger.info(f"[{bot_id}] Session closed")
async def maybe_cleanup(self, bot_id: str):
if bot_id not in self.audio_ws and bot_id not in self.control_ws:
await self.close_session(bot_id)
async def _event_pump(self, bot_id: str):
"""Background task: forward AI output → MeetStream control WS."""
session = self.sessions.get(bot_id)
if not session:
return
turn = 0
audio_resumed = False # True after resume_audio is sent for the current response
try:
async for event in session.events():
ws = self.control_ws.get(bot_id)
if not ws or ws.client_state != WebSocketState.CONNECTED:
continue
etype = event.get("type")
if etype == "audio":
if not audio_resumed:
# Zoom and Teams hold audio after an interrupt until resume_audio.
# Google Meet ignores resume_audio, so send it on every platform.
await safe_send(ws, {
"command": "resume_audio",
"bot_id": bot_id,
"resume_id": f"turn-{turn}",
})
audio_resumed = True
pcm_out = resample_pcm16(event["pcm_bytes"], MODEL_RATE, OUTGOING_RATE)
await safe_send(ws, {
"command": "sendaudio",
"bot_id": bot_id,
"audiochunk": base64.b64encode(pcm_out).decode("utf-8"),
"sample_rate": OUTGOING_RATE,
"encoding": "pcm16",
"channels": 1,
"endianness": "little",
})
elif etype == "audio_interrupted":
await safe_send(ws, {
"command": "interrupt",
"bot_id": bot_id,
"action": "clear_audio_queue",
})
turn += 1
audio_resumed = False
elif etype == "text":
await safe_send(ws, {
"command": "sendmsg",
"bot_id": bot_id,
"message": event["content"],
"msg": event["content"],
})
except Exception as e:
logger.error(f"[{bot_id}] Event pump error: {e}")
manager = SessionManager()
# ── FastAPI Application ───────────────────────────────────────────────────────
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
app = FastAPI(title="MeetStream Bridge", lifespan=lifespan)
@app.get("/health")
async def health():
return {"status": "healthy"}
@app.websocket("/bridge/audio")
async def audio_endpoint(websocket: WebSocket):
"""Receives live meeting audio from MeetStream."""
await websocket.accept()
bot_id = None
try:
while True:
raw = await websocket.receive()
if raw["type"] == "websocket.disconnect":
break
# Text frame: the handshake
if "text" in raw and raw["text"]:
data = json.loads(raw["text"])
if data.get("type") == "ready":
bot_id = data.get("bot_id")
if not bot_id:
await websocket.close(code=1003)
return
manager.audio_ws[bot_id] = websocket
await manager.ensure_session(bot_id)
await safe_send(websocket, {
"type": "ack",
"message": f"Audio channel bound to {bot_id}",
})
continue
# Binary frame: speaker-tagged PCM audio (0x01 or 0x04)
if "bytes" in raw and raw["bytes"] and bot_id:
result = decode_audio_frame(raw["bytes"])
if result is None:
continue
_, speaker_name, sample_rate, pcm_bytes = result
if should_ignore_speaker(speaker_name):
continue
if pcm_bytes:
pcm_r = resample_pcm16(pcm_bytes, sample_rate, MODEL_RATE)
session = manager.sessions.get(bot_id)
if session:
await session.send_audio(pcm_r)
except WebSocketDisconnect:
pass
except Exception as e:
logger.error(f"[{bot_id}] Audio error: {e}")
finally:
if bot_id:
manager.audio_ws.pop(bot_id, None)
await manager.maybe_cleanup(bot_id)
@app.websocket("/bridge")
async def control_endpoint(websocket: WebSocket):
"""Two-way command channel with MeetStream."""
await websocket.accept()
bot_id = None
try:
init = json.loads(await websocket.receive_text())
if init.get("type") != "ready" or not init.get("bot_id"):
await websocket.close(code=1003)
return
bot_id = init["bot_id"]
manager.control_ws[bot_id] = websocket
await manager.ensure_session(bot_id)
await safe_send(websocket, {
"command": "ack",
"bot_id": bot_id,
"message": f"Control channel bound to {bot_id}",
})
# Read command acknowledgements from MeetStream
while True:
data = json.loads(await websocket.receive_text())
logger.debug(f"[{bot_id}] Control message: {data}")
except WebSocketDisconnect:
pass
except Exception as e:
logger.error(f"[{bot_id}] Control error: {e}")
finally:
if bot_id:
manager.control_ws.pop(bot_id, None)
await manager.maybe_cleanup(bot_id)
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)

Run:

pip install fastapi uvicorn numpy
uvicorn server:app --host 0.0.0.0 --port 8000

Connecting MeetStream

When creating a MeetStream session, point both WebSocket URLs at your bridge server:

{
"meeting_link": "https://meet.google.com/abc-defg-hij",
"bot_name": "My Assistant",
"live_audio_required": {
"websocket_url": "wss://your-bridge.com/bridge/audio"
},
"socket_connection_url": {
"websocket_url": "wss://your-bridge.com/bridge"
}
}

Both connections carry the same bot_id in their handshake, so the session manager can link them together.


Design Decisions

Separation of concerns. The audio channel is high-throughput binary data (hundreds of frames per minute). The control channel is low-frequency JSON commands. Splitting them avoids head-of-line blocking and makes it easier to handle each independently.

Google Meet and Teams send 0x01 frames at 48 kHz. Zoom sends 0x04 frames that carry the sample rate in the header. Reading byte 0 lets one decoder handle every platform and skip frame types that it does not know.

Both WebSocket connections race to ensure_session(). Without a lock, two AI sessions could be created for the same bot_id. The async lock ensures exactly one session is created.

A single reconnecting WebSocket shouldn’t destroy the running session. The session stays alive as long as at least one channel is connected.

Meeting audio arrives at 48 kHz from Google Meet and Teams and at 32 kHz from Zoom. Your model can need 16 kHz or 24 kHz. Send audio back to the meeting at 48 kHz. The bridge handles both conversions, so neither MeetStream nor your model needs to know about the other’s sample rate.