Spaces:
Sleeping
Sleeping
File size: 7,275 Bytes
8aedc84 8344c24 8aedc84 8344c24 8aedc84 8344c24 8aedc84 8344c24 8aedc84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import logging
from fastapi import APIRouter, HTTPException, WebSocket
from .core import VideoCore
from .models import (
CreateRoomRequest,
ParticipantRole,
RecoveryPolicy,
VideoEncoding,
WebRTCSignalRequest,
)
logger = logging.getLogger(__name__)
video_router = APIRouter(prefix="/video", tags=["video"])
video_core = VideoCore()
# ============= WORKSPACE-SCOPED ENDPOINTS =============
@video_router.get("/workspaces/{workspace_id}/rooms")
async def list_rooms_in_workspace(workspace_id: str):
"""List all video rooms in a workspace"""
rooms = video_core.list_rooms(workspace_id)
return {
"success": True,
"workspace_id": workspace_id,
"rooms": rooms,
"total": len(rooms),
}
@video_router.post("/workspaces/{workspace_id}/rooms")
async def create_room_in_workspace(
workspace_id: str, request: CreateRoomRequest | None = None
):
"""Create a new video room in a workspace"""
room_id = None
config = None
recovery_config = None
if request:
room_id = request.room_id
config = request.config
recovery_config = request.recovery_config
# Create room with explicit workspace_id
created_workspace_id, room_id = video_core.create_room(
workspace_id, room_id, config, recovery_config
)
return {
"success": True,
"workspace_id": created_workspace_id,
"room_id": room_id,
"message": f"Video room {room_id} created successfully in workspace {created_workspace_id}",
}
@video_router.get("/workspaces/{workspace_id}/rooms/{room_id}")
async def get_room_in_workspace(workspace_id: str, room_id: str):
"""Get room details from a workspace"""
room_info = video_core.get_room_info(workspace_id, room_id)
if "error" in room_info:
raise HTTPException(status_code=404, detail="Room not found")
return {"success": True, "workspace_id": workspace_id, "room": room_info}
@video_router.delete("/workspaces/{workspace_id}/rooms/{room_id}")
async def delete_room_in_workspace(workspace_id: str, room_id: str):
"""Delete a video room from a workspace"""
if video_core.delete_room(workspace_id, room_id):
return {
"success": True,
"workspace_id": workspace_id,
"message": f"Room {room_id} deleted successfully from workspace {workspace_id}",
}
raise HTTPException(status_code=404, detail="Room not found")
@video_router.get("/workspaces/{workspace_id}/rooms/{room_id}/state")
async def get_room_state_in_workspace(workspace_id: str, room_id: str):
"""Get current room state from a workspace"""
state = video_core.get_room_state(workspace_id, room_id)
if "error" in state:
raise HTTPException(status_code=404, detail="Room not found")
return {"success": True, "workspace_id": workspace_id, "state": state}
@video_router.post("/workspaces/{workspace_id}/rooms/{room_id}/webrtc/signal")
async def handle_webrtc_signal_in_workspace(
workspace_id: str, room_id: str, signal: WebRTCSignalRequest
):
"""Handle WebRTC signaling (offer, answer, ice) in a workspace"""
try:
# Get the participant's role from the room
room = video_core._get_room(workspace_id, room_id)
participant_role: ParticipantRole | None = None
if room:
if room.producer == signal.client_id:
participant_role = ParticipantRole.PRODUCER
elif signal.client_id in room.consumers:
participant_role = ParticipantRole.CONSUMER
logger.info(
f"🔧 WebRTC signal from {signal.client_id} (role: {participant_role}) in workspace {workspace_id}, room {room_id}"
)
response = await video_core.handle_webrtc_signal(
workspace_id, room_id, signal.client_id, signal.message, participant_role
)
except ValueError as e:
raise HTTPException(status_code=400) from e
except Exception as e:
raise HTTPException(status_code=500) from e
else:
if response:
return {"success": True, "workspace_id": workspace_id, "response": response}
return {
"success": True,
"workspace_id": workspace_id,
"message": "Signal processed",
}
@video_router.websocket("/workspaces/{workspace_id}/rooms/{room_id}/ws")
async def websocket_endpoint_in_workspace(
websocket: WebSocket, workspace_id: str, room_id: str
):
"""WebSocket connection for room management and heartbeat in a workspace"""
await video_core.handle_websocket(websocket, workspace_id, room_id)
# ============= SERVICE STATUS ENDPOINTS =============
@video_router.get("/status")
async def get_status():
"""Get video service status"""
total_workspaces = len(video_core.workspaces)
total_rooms = sum(len(rooms) for rooms in video_core.workspaces.values())
cleanup_info = video_core.get_cleanup_status()
return {
"service": "video",
"status": "active",
"workspaces_count": total_workspaces,
"rooms_count": total_rooms,
"webrtc_connections_count": len(video_core.webrtc_connections),
"websocket_connections_count": len(video_core.websocket_connections),
"version": "2.0.0",
"supported_roles": [role.value for role in ParticipantRole],
"supported_encodings": [encoding.value for encoding in VideoEncoding],
"recovery_policies": [policy.value for policy in RecoveryPolicy],
"cleanup": {
"enabled": cleanup_info["cleanup_enabled"],
"inactivity_timeout_hours": cleanup_info["inactivity_timeout_minutes"] / 60,
"cleanup_interval_minutes": cleanup_info["cleanup_interval_minutes"],
},
}
@video_router.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "service": "video"}
@video_router.get("/cleanup/status")
async def get_cleanup_status():
"""Get cleanup system status and room information"""
status = video_core.get_cleanup_status()
return {"success": True, "cleanup_status": status}
@video_router.post("/cleanup/manual")
async def trigger_manual_cleanup():
"""Manually trigger room cleanup"""
result = await video_core.manual_cleanup()
return {"success": True, "cleanup_result": result}
@video_router.get("/")
async def video_status():
"""Video service main endpoint"""
return {
"status": "active",
"service": "video",
"message": "Video service running with workspace-scoped room-based WebRTC streaming",
"version": "2.0.0",
"architecture": "workspace/producer/consumer rooms",
"endpoints": [
"/video/workspaces/{workspace_id}/rooms - List all video rooms in workspace",
"/video/workspaces/{workspace_id}/rooms/{room_id} - Get room details",
"/video/workspaces/{workspace_id}/rooms/{room_id}/webrtc/signal - WebRTC signaling",
"/video/workspaces/{workspace_id}/rooms/{room_id}/ws - WebSocket connection",
"/video/status - Service status",
"/video/cleanup/status - Cleanup system status",
"/video/cleanup/manual - Manual cleanup trigger",
],
}
|