Spaces:
Running
Running
File size: 10,005 Bytes
02eac4b |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
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)
# ============= LEGACY ENDPOINTS (Return errors directing to new format) =============
@video_router.get("/rooms")
async def list_rooms_legacy():
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": "Use workspace-scoped endpoint: GET /video/workspaces/{workspace_id}/rooms",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.post("/rooms")
async def create_room_legacy():
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": "Use workspace-scoped endpoint: POST /video/workspaces/{workspace_id}/rooms",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.get("/rooms/{room_id}")
async def get_room_legacy(room_id: str):
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": f"Use workspace-scoped endpoint: GET /video/workspaces/{{workspace_id}}/rooms/{room_id}",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.delete("/rooms/{room_id}")
async def delete_room_legacy(room_id: str):
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": f"Use workspace-scoped endpoint: DELETE /video/workspaces/{{workspace_id}}/rooms/{room_id}",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.get("/rooms/{room_id}/state")
async def get_room_state_legacy(room_id: str):
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": f"Use workspace-scoped endpoint: GET /video/workspaces/{{workspace_id}}/rooms/{room_id}/state",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.post("/rooms/{room_id}/webrtc/signal")
async def handle_webrtc_signal_legacy(room_id: str, signal: WebRTCSignalRequest):
"""Legacy endpoint - directs to workspace-scoped API"""
raise HTTPException(
status_code=400,
detail={
"error": "Legacy endpoint deprecated",
"message": f"Use workspace-scoped endpoint: POST /video/workspaces/{{workspace_id}}/rooms/{room_id}/webrtc/signal",
"migration_guide": "All video rooms now require a workspace_id for better organization and security",
},
)
@video_router.websocket("/rooms/{room_id}/ws")
async def websocket_endpoint_legacy(websocket: WebSocket, room_id: str):
"""Legacy endpoint - WebSocket connection (this will fail immediately)"""
await websocket.close(
code=4000,
reason="Legacy endpoint deprecated. Use /video/workspaces/{workspace_id}/rooms/{room_id}/ws",
)
# ============= 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())
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],
}
@video_router.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "service": "video"}
@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",
],
"migration_note": "All legacy /video/rooms/* endpoints are deprecated. Use workspace-scoped endpoints.",
}
|