agence104/livekit-server-sdk

LiveKit的服务器端SDK。

1.2.4 2024-09-05 01:15 UTC

This package is not auto-updated.

Last update: 2024-09-24 13:00:44 UTC


README

PHP API用于管理房间和创建访问令牌。该库旨在与livekit-server配合使用。使用PHP后端管理对LiveKit的访问。

安装

要求

  • php: >= 8

Composer

composer require agence104/livekit-server-sdk

使用

环境变量

您可以将凭证存储在环境变量中。如果在创建RoomServiceClient或AccessToken时未传递host、api-key或api-secret,将使用以下环境变量的值

  • LIVEKIT_URL
  • LIVEKIT_API_KEY
  • LIVEKIT_API_SECRET

创建访问令牌

为参与者创建加入房间的令牌。

use Agence104\LiveKit\AccessToken;
use Agence104\LiveKit\AccessTokenOptions;
use Agence104\LiveKit\VideoGrant;

// If this room doesn't exist, it'll be automatically created when the first
// client joins.
$roomName = 'name-of-room';
// The identifier to be used for participant.
$participantName = 'user-name';

// Define the token options.
$tokenOptions = (new AccessTokenOptions())
  ->setIdentity($participantName);

// Define the video grants.
$videoGrant = (new VideoGrant())
  ->setRoomJoin();
  ->setRoomName($roomName);

// Initialize and fetch the JWT Token. 
$token = (new AccessToken('api-key', 'secret-key'))
  ->init($tokenOptions)
  ->setGrant($videoGrant)
  ->toJwt();

默认情况下,令牌在6小时后过期。您可以通过在访问令牌选项中传递ttl来覆盖此设置。ttl以秒(数字)表示。

解析访问令牌

将JWT令牌转换为ClaimGrants。

use Agence104\LiveKit\AccessToken;

// Initialize and parse the JWT Token. 
$claimGrants = (new AccessToken('api-key', 'secret-key'))  
  ->fromJwt($token);

访问令牌中的权限

可以自定义每个参与者的权限

use Agence104\LiveKit\VideoGrant;

$videoGrant = (new VideoGrant())
  ->setRoomJoin() // TRUE by default.
  ->setRoomName('name-of-room')
  ->setCanPublish(FALSE)
  ->setCanSubscribe() // TRUE by default.
  ->setGrant($videoGrant);

这将允许参与者订阅轨道,但不能将他们自己的内容发布到房间中。

管理房间

RoomServiceClient提供列出、创建和删除房间的API。它还需要一对API密钥/密钥对才能操作。

use Agence104\LiveKit\RoomServiceClient;
use Agence104\LiveKit\RoomCreateOptions;

$host = 'https://my.livekit.host';
$svc = new RoomServiceClient($host, 'api-key', 'secret-key');

// List rooms.
$rooms = $svc->listRooms();

// Create a new room.
$opts = (new RoomCreateOptions())
  ->setName('myroom')
  ->setEmptyTimeout(10)
  ->setMaxParticipants(20);
$room = $svc->createRoom($opts);

// Delete a room.
$svc->deleteRoom('myroom');

运行测试

我们将使用Lando来简化测试执行过程。但是,如果您选择直接在本地环境中运行测试,当然可以采取这种方法。

步骤1

通过复制example.dev并重命名副本为.env来生成环境文件,然后相应地输入您的凭证。

步骤2

启动lando项目。

lando start

步骤3

生成将作为大多数测试用例测试环境的LiveKit房间。

lando create-test-room

步骤4

在房间内初始化5个测试用户。在单独的终端窗口中运行此命令。

lando start-test-users

步骤5

是时候忙碌起来进行测试了。

lando test

步骤6

测试完成后,是时候清理了。

  • 结束lando start-test-users命令。
  • 运行lando delete-test-room以删除测试房间。