sfu-signaling: real Janus signaling service implementation

- 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>
This commit is contained in:
Anton Afanasyeu
2026-06-28 22:27:59 +02:00
parent ece49a035a
commit 0658e26ae4
9 changed files with 723 additions and 0 deletions

68
public/api/join.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* SFU join API — POST /api/join
*
* Client sends: { room_id, jsep: { type: "offer", sdp: "..." }, role: "publisher"|"subscriber" }
* Server relays to Janus VideoRoom, returns: { jsep: { type: "answer", sdp: "..." }, handle_id, session_id }
*
* Client subsequently maintains a WebSocket to Janus directly (proxied via nginx /sfu/ws).
*/
require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
sfuJsonErr('method not allowed', 405);
}
$user = sfuRequireAuth();
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$janusRoomId = (int) ($input['room_id'] ?? 0);
$jsep = $input['jsep'] ?? null;
$role = in_array($input['role'] ?? '', ['publisher', 'subscriber'], true)
? $input['role'] : 'publisher';
if (!$janusRoomId) { sfuJsonErr('room_id required'); }
if (!$jsep || empty($jsep['sdp'])) { sfuJsonErr('jsep.sdp required'); }
$repo = new \AndroidCast\Sfu\SfuRoomRepository(sfuPdo());
$janus = janusClient();
$room = $repo->getByJanusId($janusRoomId);
if (!$room) { sfuJsonErr('room not found', 404); }
/* Create a fresh Janus session + handle for this participant */
$sid = $janus->createSession();
if (!$sid) { sfuJsonErr('Janus session failed'); }
$hid = $janus->attachVideoRoom($sid);
if (!$hid) { sfuJsonErr('Janus attach failed'); }
/* Send join + offer to Janus */
$joinBody = [
'request' => 'join',
'room' => $janusRoomId,
'ptype' => $role === 'publisher' ? 'publisher' : 'subscriber',
'display' => $user['email'] ?? 'anon',
];
$resp = $janus->sendJsep($sid, $hid, $joinBody, [
'type' => $jsep['type'],
'sdp' => $jsep['sdp'],
]);
/* Record participant in DB */
$participantId = $repo->joinRoom(
(int) $room['id'],
(int) $user['id'],
$hid,
$role
);
/* Return negotiated JSEP answer + WS connection details */
$answer = $resp['jsep'] ?? null;
sfuJsonOk([
'session_id' => $sid,
'handle_id' => $hid,
'participant_id' => $participantId,
'jsep' => $answer,
'ws_url' => '/app/androidcast_project/sfu/ws',
]);

60
public/api/rooms.php Normal file
View File

@@ -0,0 +1,60 @@
<?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);

25
public/api/stats.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/**
* SFU stats ingest — POST /api/stats
* Body: { participant_id, bitrate_kbps, packet_loss }
* Called periodically by Android client (every 5s) to feed analytics.
*/
require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
sfuJsonErr('method not allowed', 405);
}
sfuRequireAuth();
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$pid = (int) ($input['participant_id'] ?? 0);
$bw = (float) ($input['bitrate_kbps'] ?? 0.0);
$pl = (float) ($input['packet_loss'] ?? 0.0);
if ($pid <= 0) { sfuJsonErr('participant_id required'); }
$repo = new \AndroidCast\Sfu\SfuRoomRepository(sfuPdo());
$repo->recordStats($pid, $bw, $pl);
sfuJsonOk(['recorded' => true]);