RTippin / janus-client

Laravel API wrapper to interact fluently with your Janus Media Server. Core server interactions, as well as the video room plugin included.
MIT License
16 stars 6 forks source link
client janus janus-client janus-gateway laravel media-server meetecho plugin video-call video-room webrtc websocket wrapper

Laravel Janus Gateway Client

[![Latest Version on Packagist][ico-version]][link-packagist] [![Total Downloads][ico-downloads]][link-downloads] [![Tests][ico-test]][link-test] [![StyleCI][ico-styleci]][link-styleci] [![License][ico-license]][link-license]


This package provides a client to fluently interact with your [Janus Gateway Server][link-janus]

Prerequisites

Laravel PHP janus-client
8.x ^7.4 ^8.0 ^8.1 <= 1.0.0
9.x ^8.0.2 ^8.1 >= 1.1.0
10.x ^8.1 ^8.2 >= 1.2.0
11.x ^8.2 ^8.3 >= 1.3.0

Included


Fluent, convenient, clean.

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',
]

Installation

Via Composer

composer require rtippin/janus-client

Publish the config file

php artisan vendor:publish --tag=janus

Config

'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'),

General Usage

Notice, Janus is registered as a singleton. Once you instantiate our class, it will be kept in memory with its current state for that request cycle.

Obtaining the Janus client

Using Facade

use RTippin\Janus\Facades\Janus;

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

Using Dependency Injection

<?php

namespace App\Http\Controllers;

use RTippin\Janus\Janus;

class JanusController
{
    private Janus $janus;

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

info()

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 will initiate a handshake with janus to set our session ID for any following request. This is a fluent method and can be chained.
```php
Janus::connect();

attach(string $plugin)

$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();

## Example Cycle
- Say we want to obtain a list of video rooms, 4 calls must be made.
    - First we connect which sets our session id.
    - Then we want to attach to the video room plugin to set our handle id.
    - Once attached, we send janus our command message to list rooms.
    - If no further calls need to be made, we then disconnect which will reset our session and handle ID's. This also ensures state sessions are not kept in your janus servers memory.

```php
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();

Shared Plugin Methods

All Plugin methods will return the plugin response array from janus directly.

Examples using VideoRoom plugin

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

Example video room call to remove all rooms

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)

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

### `{JanusPlugin}->getPluginPayload(?string $key = null)`
- Get the API payload for the last plugin method called.
```php
//Make plugin call. 
Janus::videoRoom()->list();

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

Video Room

For full docs relating to the video room plugin and its responses, please check the [Official Docs][link-videoroom]

Using Facade

use RTippin\Janus\Facades\Janus;

$videoRoom = Janus::videoRoom();

Using Dependency Injection

<?php

namespace App\Http\Controllers;

use RTippin\Janus\Plugins\VideoRoom;

class VideoRoomController
{
    private VideoRoom $videoRoom;

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

list()

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

### `allowed(int $room, string $action, ?array $allowed = null, ?string $secret = null)`
- You can configure whether to check tokens or add/remove people who can join a room.
```php
$allowed = Janus::videoRoom()->allowed(12345678, 'remove', ['token'], 'SECRET');

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

//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();



---

## Credits - Richard Tippin

## License - MIT

### Please see the [license file](LICENSE.md) for more information.

[link-author]: https://github.com/rtippin
[ico-version]: https://img.shields.io/packagist/v/rtippin/janus-client.svg?style=plastic&cacheSeconds=3600
[ico-downloads]: https://img.shields.io/packagist/dt/rtippin/janus-client.svg?style=plastic&cacheSeconds=3600
[link-test]: https://github.com/RTippin/janus-client/actions
[ico-test]: https://img.shields.io/github/actions/workflow/status/rtippin/janus-client/test.yml?branch=master&style=plastic
[ico-styleci]: https://styleci.io/repos/387571926/shield?style=plastic&cacheSeconds=3600
[ico-license]: https://img.shields.io/github/license/RTippin/janus-client?style=plastic
[link-packagist]: https://packagist.org/packages/rtippin/janus-client
[link-downloads]: https://packagist.org/packages/rtippin/janus-client
[link-license]: https://packagist.org/packages/rtippin/janus-client
[link-styleci]: https://styleci.io/repos/387571926
[link-janus]: https://janus.conf.meetecho.com/docs/index.html
[link-videoroom]: https://janus.conf.meetecho.com/docs/videoroom.html