mirror of
git://f0xx.org/ac/ac-ms-sfu-signaling
synced 2026-07-29 07:38:35 +03:00
- JanusClient.php: HTTP REST client for Janus Gateway API - SfuRoomRepository.php: room + participant + stats persistence - bootstrap.php: PDO, auth, Janus client factory - public/index.php: front-controller routing - public/health.php: Janus online check - public/api/rooms.php: list/create/destroy rooms - public/api/join.php: SDP offer relay → Janus VideoRoom - public/api/stats.php: bitrate/packet-loss analytics ingest - public/dashboard.php: SFU rooms UI (common BE dark-theme styles) Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
/**
|
|
* SFU rooms API
|
|
* GET /api/rooms — list active rooms
|
|
* POST /api/rooms — create room (body: {name, purpose})
|
|
* DELETE /api/rooms/{id} — close room
|
|
*/
|
|
require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
|
|
|
|
$user = sfuRequireAuth();
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
|
$path = trim(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?? '', '/');
|
|
$parts = explode('/', $path);
|
|
$roomId = (int) ($parts[array_key_last($parts)] ?? 0);
|
|
|
|
$repo = new \AndroidCast\Sfu\SfuRoomRepository(sfuPdo());
|
|
$janus = janusClient();
|
|
|
|
if ($method === 'GET') {
|
|
sfuJsonOk(['rooms' => $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);
|