$repo->listActive()]); } if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true) ?? []; $name = trim((string) ($input['name'] ?? 'Room ' . date('H:i'))); $purp = trim((string) ($input['purpose'] ?? 'screen_cast')); /* Allocate a Janus session + handle to create the VideoRoom */ $sid = $janus->createSession(); if (!$sid) { sfuJsonErr('Janus unreachable'); } $hid = $janus->attachVideoRoom($sid); if (!$hid) { sfuJsonErr('Janus attach failed'); } $janusRoomId = mt_rand(1_000_000, 9_999_999); $ok = $janus->createRoom($sid, $hid, $janusRoomId, [ 'description' => $name, 'videocodec' => 'vp8,h264', 'audiocodec' => 'opus', 'bitrate' => 1024000, ]); if (!$ok) { sfuJsonErr('Janus room creation failed'); } $id = $repo->createRoom($janusRoomId, (int) $user['id'], $name, $purp); sfuJsonOk(['room' => ['id' => $id, 'janus_room_id' => $janusRoomId, 'name' => $name]]); } if ($method === 'DELETE' && $roomId > 0) { $row = $repo->getByJanusId($roomId) ?? sfuJsonErr('room not found', 404); /* Best-effort Janus destroy */ $sid = $janus->createSession(); if ($sid) { $hid = $janus->attachVideoRoom($sid); if ($hid) { $janus->destroyRoom($sid, $hid, $roomId); } } $repo->closeRoom((int) $row['id']); sfuJsonOk(['destroyed' => true]); } sfuJsonErr('method not allowed', 405);