rtippin/janus-client

Janus 网关客户端

v1.3.0 2024-05-23 21:03 UTC

This package is auto-updated.

Last update: 2024-09-23 21:52:22 UTC


README

Latest Version on Packagist Total Downloads Tests StyleCI License

此包提供了一个客户端,可以流畅地与您的 Janus 网关服务器 交互。

先决条件

包含

  • 用于与 janus 交互的核心 REST API 包装器。
  • VideoRoom 插件包装器。

流畅、方便、简洁。

use RTippin\Janus\Facades\Janus;

$ping = Janus::ping(); 

---------------------------------------

['pong' => true]

---------------------------------------

$room = Janus::videoRoom()->create([
    'description' => 'My first room!',
    'publishers' => 4,
]);

---------------------------------------

[
  'videoroom' => 'created',
  'room' => 6663183870503329,
  'permanent' => false,
  'pin' => 'TFQuls',
  'secret' => 'y2WaVehf7cOM',
]

安装

通过 Composer

composer require rtippin/janus-client

发布配置文件

php artisan vendor:publish --tag=janus

配置

'server_endpoint' => env('JANUS_SERVER_ENDPOINT'),
'admin_server_endpoint' => env('JANUS_ADMIN_SERVER_ENDPOINT'),
'verify_ssl' => env('JANUS_VERIFY_SSL', true),
'debug' => env('JANUS_DEBUG', false),
'admin_secret' => env('JANUS_ADMIN_SECRET'),
'api_secret' => env('JANUS_API_SECRET'),
'video_room_secret' => env('JANUS_VIDEO_ROOM_SECRET'),
  • server_endpoint 是您 janus 服务器的主要 HTTP 端点。
  • admin_server_endpoint 是您 janus 服务器的管理员 HTTP 端点。
  • verify_ssl 启用或禁用我们的 Guzzle HTTP 调用对 SSL 的验证。
  • debug 当启用时,每个循环中的每个请求都会转储负载和响应。
  • admin_secret 访问管理员端点的 API 密钥。
  • api_secret 通用 API 密钥。
  • video_room_secret 可选的视频房间密钥,用于保护创建操作。

一般用法

  • 您可以选择使用我们提供的门面或依赖注入来访问我们的核心 Janus 类。

注意,Janus 被注册为单例。一旦实例化我们的类,它将保留在内存中,并保持当前状态,直到请求周期结束。

获取 Janus 客户端

使用门面

use RTippin\Janus\Facades\Janus;

$info = Janus::info() || Janus::getInstance()->info();

使用依赖注入

<?php

namespace App\Http\Controllers;

use RTippin\Janus\Janus;

class JanusController
{
    private Janus $janus;

    public function __construct(Janus $janus)
    {
       $this->janus = $janus;
    }
}

info()

  • 返回 janus 服务器信息数组。
Janus::info();

ping()

  • Ping 总是返回一个数组(即使抛出异常),其中包含 pong 作为 true|false,以及服务器延迟。
Janus::ping();

debug(bool $debug = true)

  • 通过在 Janus 上调用 debug 方法启用调试/动态转储。这将转储每个 HTTP 调用的负载、响应和延迟。
use RTippin\Janus\Facades\Janus;

Route::get('test', function(){
    Janus::debug()->ping();
    dump('It dumps inline for each http call!');
});

//OUTPUT

"PAYLOAD"

array:3 [▼
  "transaction" => "q52xpYrZJ6e6"
  "apisecret" => "secret"
  "janus" => "ping"
]

"RESPONSE"

array:2 [▼
  "janus" => "pong"
  "transaction" => "q52xpYrZJ6e6"
]

"LATENCY"

16.0

"It dumps inline for each http call!"

connect()

  • connect() 将初始化与 janus 的握手,为任何后续请求设置我们的会话 ID。这是一个流畅的方法,可以链式调用。
Janus::connect();

attach(string $plugin)

  • 附加到 janus 插件以设置我们的句柄 ID。在此请求周期中的所有后续 API 调用都将发送到此插件,除非您调用 detach 或 disconnect。这是一个流畅的方法,可以链式调用。
Janus::attach('janus.plugin.name');

detach()

  • 从当前插件/句柄断开连接。这是一个流畅的方法,可以链式调用。
Janus::detach();

disconnect()

  • 从 janus 断开连接,销毁我们的会话和句柄/插件。这是一个流畅的方法,可以链式调用。
Janus::disconnect();

message(array $message, $jsep = null)

  • 将我们的消息发送到 janus。通常在附加到插件后调用一次,并向 janus 发送命令。这是一个流畅的方法,可以链式调用。
Janus::message(['request' => 'list']);

trickle($candidate)

  • 向 janus 发送一个 trickle 候选者。这是一个流畅的方法,可以链式调用。
Janus::trickle('candidate information');

server()

  • 返回底层的 janus 服务器类,允许您设置配置或访问周期中的当前负载/响应。
use RTippin\Janus\Facades\Janus;

$server = Janus::server()
    ->setServerEndpoint('http://test.com')
    ->setAdminServerEndpoint('http://test.com/admin')
    ->setApiSecret('secret');
    
Janus::connect();

$response = $server->getApiResponse();
$payload = $server->getApiPayload();
$latency = $server->getEndLatency();

示例周期

  • 假设我们想获取视频房间的列表,必须进行 4 次调用。
    • 首先我们连接,设置我们的会话 ID。
    • 然后我们想要附加到视频房间插件,设置我们的句柄 ID。
    • 附加后,我们向 janus 发送我们的命令消息以列出房间。
    • 如果没有需要进行的进一步调用,我们将断开连接,这将重置我们的会话和处理ID。这也确保了状态会话不会被保留在您的Janus服务器内存中。
use RTippin\Janus\Facades\Janus;

//Send our command for the results we want.    
Janus::connect()
    ->attach('janus.plugin.videoroom')
    ->message(['request' => 'list']);

//Set the results from the last command sent.
$rooms = Janus::getApiResponse();

//Disconnect and reset all janus values.
Janus::disconnect();

共享插件方法

所有插件方法将直接返回Janus的插件响应数组。

  • ['plugindata']['data']的内容返回。

使用VideoRoom插件的示例

{JanusPlugin}->withoutDisconnect() | {JanusPlugin}->disconnect(bool $force = false)

  • 如果您计划在连接到同一插件的同时执行许多命令,调用此方法将只创建一个connectattach调用以重用我们的会话和处理ID。
  • 完成所有单个调用后,您必须手动调用父Janus来断开连接,或者使用disconnect(true)在当前插件实例中强制断开。
  • 这些是流式方法,可以串联使用。

示例:使用视频房间调用移除所有房间

use RTippin\Janus\Facades\Janus;

//Disable disconnects for plugin calls.
Janus::videoRoom()->withoutDisconnect();

//Grab list of rooms.
$rooms = Janus::videoRoom()->list()['list'];

//Destroy each room.
foreach ($rooms as $room) {
    Janus::videoRoom()->destroy($room['room']);
}

//Now disconnect to remove our session/handle.
Janus::videoRoom()->disconnect(true); //Forced on current plugin instance.
---------------------------------------------------------------------------
Janus::disconnect(); //Main disconnect will always be run if called.

{JanusPlugin}->getPluginResponse(?string $key = null)

  • 获取最后调用的插件方法的API响应。
    • ['plugindata']['data']的内容将返回。
//Make plugin call. 
Janus::videoRoom()->list();

//Get response.
$list = Janus::videoRoom()->getPluginResponse('list');

{JanusPlugin}->getPluginPayload(?string $key = null)

  • 获取最后调用的插件方法的API有效负载。
//Make plugin call. 
Janus::videoRoom()->list();

//Get payload.
$payload = Janus::videoRoom()->getPluginPayload();

视频房间

有关视频房间插件及其响应的完整文档,请参阅官方文档

  • 您可以通过核心Janus类/外观或核心VideoRoom类的依赖注入来访问视频房间插件。
  • 除非您指定withoutDisconnect(),否则每个主要Janus方法都完成一个完整的周期(连接、附加、消息、断开连接)。

使用门面

use RTippin\Janus\Facades\Janus;

$videoRoom = Janus::videoRoom();

使用依赖注入

<?php

namespace App\Http\Controllers;

use RTippin\Janus\Plugins\VideoRoom;

class VideoRoomController
{
    private VideoRoom $videoRoom;

    public function __construct(VideoRoom $videoRoom)
    {
       $this->videoRoom = $videoRoom;
    }
}

list()

  • 返回可用房间的列表(排除已配置或创建为私人房间的房间)。
$list = Janus::videoRoom()->list();

exists(int $room)

  • 检查是否存在房间。
$exists = Janus::videoRoom()->exists(12345678);

create(array $params = [], bool $usePin = true, bool $useSecret = true)

  • 创建一个新的视频房间。默认情况下,我们将为您创建一个PINSECRET,并设置某些属性。您设置的任何params将覆盖任何默认值。
    • 我们将合并PIN/SECRET与Janus插件响应返回的数组,以便您在需要时保存它。
$room = Janus::videoRoom()->create([
    'description' => 'My first room!',
    'publishers' => 10,
    'bitrate' => 1024000,
    'is_private' => true,
]);

edit(int $room, array $params, ?string $secret = null)

  • 编辑现有房间的允许属性。
$newProperties = [
    'new_description' => 'First room!',
    'new_bitrate' => 600000,
];

$edit = Janus::videoRoom()->edit(12345678, $newProperties, 'SECRET');

allowed(int $room, string $action, ?array $allowed = null, ?string $secret = null)

  • 您可以配置是否检查令牌或添加/删除可以加入房间的用户。
$allowed = Janus::videoRoom()->allowed(12345678, 'remove', ['token'], 'SECRET');

kick(int $room, int $participantID, ?string $secret = null)

  • 从房间中踢出参与者。
$kick = Janus::videoRoom()->kick(12345678, 987654321, 'SECRET');

listParticipants(int $room)

  • 获取特定房间中的参与者列表。
$participants = Janus::videoRoom()->listParticipants(12345678);

listForwarders(int $room, ?string $secret = null)

  • 获取特定房间中的所有转发器的列表。
$forwarders = Janus::videoRoom()->listForwarders(12345678, 'SECRET');

destroy(int $room, ?string $secret = null)

  • 销毁现有的视频房间。
$destroy = Janus::videoRoom()->destroy(12345678, 'SECRET');

moderate(int $room, int $participantID, bool $mute, ?string $mid = null, ?string $secret = null)

  • 强制静音/取消静音参与者发送的任何媒体流。
$moderate = Janus::videoRoom()->moderate(12345678, 987654321, true, 'm-line' 'SECRET');

enableRecording(int $room, bool $record, ?string $secret = null)

  • 在会议进行时启用或禁用所有参与者的录制。
$record = Janus::videoRoom()->enableRecording(12345678, true, 'SECRET');

示例:使用许多方法进行循环而无需断开连接。

use RTippin\Janus\Facades\Janus;

//Disable disconnect between each method call.
Janus::videoRoom()->withoutDisconnect();

//Run methods as needed. Connect and attach will only be called once.
if (Janus::videoRoom()->exists(12345678)['exists']) {
    Janus::videoRoom()->destroy(12345678);
}

//Disconnect and reset all janus values.
Janus::disconnect();

版权 - Richard Tippin

许可证 - MIT

有关更多信息,请参阅许可证文件