Files
OwnCord/Server/ws/livekit_webhook.go
T
jevb 9f381f54e9 feat: voice/video polish — refactor, AudioWorklet VAD, bug fixes, UX improvements
Research-driven voice/video polish pass based on Discord/TeamSpeak comparison.

Refactor:
- Split livekitSession.ts (1,509 lines) into 4 modules: audioPipeline.ts,
  audioElements.ts, deviceManager.ts + facade in livekitSession.ts
- Facade pattern preserves all existing exports (zero breaking changes)

AudioWorklet VAD:
- Migrated VAD from setTimeout polling to AudioWorklet (vad-worklet.js)
- Runs on audio thread, works when app is backgrounded
- Graceful fallback to setTimeout if AudioWorklet unavailable

Bug fixes:
- Token TTL extended from 4h to 24h (eliminates fragile long sessions)
- Ghost voice state: retry with exponential backoff (3 attempts, 100-400ms)
- Client token refresh adjusted to 23h (1h before expiry)

UX improvements:
- Speaker indicator: pulsing green glow animation (speak-pulse keyframes)
- Permission recovery: "Grant Microphone" button in VoiceWidget for
  listen-only mode with listenOnly state in voiceStore
- Device hot-swap: devicechange listener with 500ms debounce, auto-fallback
  to default device, toast notification
- Camera/screenshare stop: toast feedback on disable
- Connection quality: auto-expand stats pane on poor/bad quality (3s debounce)
- Bandwidth display: human-readable Mbps in stats pane (formatBitrate)

Observability:
- Voice session metrics: voice_sessions counter on /api/v1/metrics endpoint

Tests:
- 55 new unit tests for audioPipeline + audioElements modules
- 22 new Go tests for HTTPS proxy (WebSocket upgrade, origin validation,
  path blocking)
- 11 new voice E2E tests (lifecycle, widget, speaker indicators)
- Pre-refactor snapshot tests for livekitSession public API

Docs:
- DESIGN.md: full design system documentation (tokens, typography, colors,
  spacing, motion, voice-specific tokens)
- VOICE-COMPARISON-MATRIX.md: 25-behavior comparison across Discord,
  TeamSpeak, Guilded
- voice-video-polish.md: CEO plan with scope decisions
2026-03-29 00:51:54 +01:00

196 lines
5.9 KiB
Go

package ws
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
"strings"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
)
// NewLiveKitWebhookHandler returns an HTTP handler that processes LiveKit
// webhook events. It synchronises LiveKit room state back into OwnCord's
// voice_states DB — primarily for crash recovery when a participant
// disconnects from LiveKit without sending a WS voice_leave.
//
// Speaker detection is handled client-side via LiveKit's
// RoomEvent.ActiveSpeakersChanged (lower latency than webhooks).
func (h *Hub) NewLiveKitWebhookHandler(apiKey, apiSecret string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(io.LimitReader(r.Body, 64*1024))
if err != nil {
slog.Error("livekit webhook: read body failed", "error", err)
http.Error(w, "bad request", http.StatusBadRequest)
return
}
// Verify the webhook token from the Authorization header.
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
slog.Warn("livekit webhook: missing Authorization header")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
// LiveKit sends "Bearer <token>" in the Authorization header.
tokenStr := strings.TrimPrefix(authHeader, "Bearer ")
verifier, err := auth.ParseAPIToken(tokenStr)
if err != nil {
slog.Warn("livekit webhook: invalid token", "error", err)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if verifier.APIKey() != apiKey {
slog.Warn("livekit webhook: API key mismatch",
"got", verifier.APIKey(), "want", apiKey)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
// Verify checks both the HMAC signature and the exp/nbf claims
// (via jwt.Claims.Validate with Time: time.Now() inside the SDK).
// Expired tokens are rejected with an error here.
if _, _, err := verifier.Verify(apiSecret); err != nil {
slog.Warn("livekit webhook: token verification failed", "error", err)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
// Parse the webhook event payload.
var event livekit.WebhookEvent
if err := json.Unmarshal(body, &event); err != nil {
slog.Warn("livekit webhook: invalid JSON", "error", err)
http.Error(w, "bad request", http.StatusBadRequest)
return
}
slog.Info("livekit webhook received",
"event", event.Event,
"room", event.GetRoom().GetName(),
"participant", event.GetParticipant().GetIdentity(),
)
switch event.Event {
case "participant_joined":
h.handleWebhookParticipantJoined(&event)
case "participant_left":
h.handleWebhookParticipantLeft(&event)
default:
slog.Debug("livekit webhook: unhandled event", "event", event.Event)
}
w.WriteHeader(http.StatusOK)
}
}
// parseIdentity extracts a user ID from a LiveKit participant identity
// formatted as "user-{id}".
func parseIdentity(identity string) (int64, error) {
if !strings.HasPrefix(identity, "user-") {
return 0, fmt.Errorf("invalid identity format: %s", identity)
}
return strconv.ParseInt(identity[5:], 10, 64)
}
// parseRoomChannelID extracts a channel ID from a LiveKit room name
// formatted as "channel-{id}".
func parseRoomChannelID(roomName string) (int64, error) {
if !strings.HasPrefix(roomName, "channel-") {
return 0, fmt.Errorf("invalid room name format: %s", roomName)
}
return strconv.ParseInt(roomName[8:], 10, 64)
}
func (h *Hub) handleWebhookParticipantJoined(event *livekit.WebhookEvent) {
p := event.GetParticipant()
if p == nil {
return
}
userID, err := parseIdentity(p.Identity)
if err != nil {
slog.Warn("livekit webhook: participant_joined bad identity",
"identity", p.Identity, "error", err)
return
}
slog.Info("livekit webhook: participant joined",
"user_id", userID,
"room", event.GetRoom().GetName())
// State is already persisted by handleVoiceJoin before the token is
// issued. This webhook confirms the client actually connected.
}
func (h *Hub) handleWebhookParticipantLeft(event *livekit.WebhookEvent) {
p := event.GetParticipant()
room := event.GetRoom()
if p == nil || room == nil {
return
}
userID, err := parseIdentity(p.Identity)
if err != nil {
slog.Warn("livekit webhook: participant_left bad identity",
"identity", p.Identity, "error", err)
return
}
channelID, err := parseRoomChannelID(room.Name)
if err != nil {
slog.Warn("livekit webhook: participant_left bad room",
"room", room.Name, "error", err)
return
}
slog.Info("livekit webhook: participant left",
"user_id", userID,
"channel_id", channelID)
// Clean up voice state if the user disconnected from LiveKit
// without sending a WS voice_leave (e.g. crash, network loss).
h.mu.RLock()
c, exists := h.clients[userID]
h.mu.RUnlock()
if exists {
// Only clean up if the user is still in the channel that fired the
// webhook. If they've already moved or left, don't touch their state.
currentChID := c.getVoiceChID()
if currentChID == channelID {
c.clearVoiceChID()
if h.db != nil {
if err := leaveVoiceChannelWithRetry(h, userID, channelID); err != nil {
slog.Error("livekit webhook: LeaveVoiceChannel exhausted retries",
"error", err, "user_id", userID, "channel_id", channelID)
}
}
h.BroadcastToAll(buildVoiceLeave(channelID, userID))
slog.Info("livekit webhook: cleaned up stale voice state",
"user_id", userID,
"channel_id", channelID)
}
} else {
// Client already disconnected from WS — ensure DB is clean.
if h.db != nil {
if err := leaveVoiceChannelWithRetry(h, userID, channelID); err != nil {
slog.Error("livekit webhook: LeaveVoiceChannel exhausted retries (client gone)",
"error", err, "user_id", userID, "channel_id", channelID)
}
}
}
}
// MountWebhookRoute is a helper for the router to mount the webhook endpoint.
func MountWebhookRoute(h *Hub, apiKey, apiSecret string) http.HandlerFunc {
return h.NewLiveKitWebhookHandler(apiKey, apiSecret)
}