PHP code example of yigit-serin / laravel-openvidu

1. Go to this page and download the library: Download yigit-serin/laravel-openvidu library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yigit-serin / laravel-openvidu example snippets


SquareetLabs\LaravelOpenVidu\Providers\OpenViduServiceProvider::class,

'OpenVidu' => SquareetLabs\LaravelOpenVidu\Facades\LaravelOpenVidu::class,

return [   
    ...
    ...
    'openvidu' => [
           'app' => env('OPENVIDU_APP'), //At the moment, always "OPENVIDUAPP"
           'domain' => env('OPENVIDU_DOMAIN'), //Your OpenVidu Server machine public IP
           'port' => env('OPENVIDU_PORT'), //Listening port of your OpenVidu server, default 4443
           'secret' => env('OPENVIDU_SECRET'), //The password used to secure your OpenVidu Server
           'debug' => env('OPENVIDU_DEBUG') // true or false
       ]
    ...

return [   
    'stores' => [
            ...
            ...
            'openvidu' => [
                   'driver' => 'openvidu',
                   'table' => 'openvidu_cache'
               ]
    ]
    ...

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
use SquareetLabs\LaravelOpenVidu\SessionProperties;
use Illuminate\Support\Str;
...
/** var string */
$customSessionId = Str::random(20);

$sessionProperties = new SessionProperties(MediaMode::ROUTED, RecordingMode::MANUAL, OutputMode::COMPOSED, RecordingLayout::BEST_FIT, $customSessionId);
$session = OpenVidu::createSession($sessionProperties);

$tokenOptions = new TokenOptions(OpenViduRole::PUBLISHER);
$token = $session->generateToken($tokenOptions);


use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getActiveSessions();

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getSession($customSessionId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = $session = OpenVidu::getSession($customSessionId);
$connections = $session->getActiveConnections();

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getSession($customSessionId);
$closed = $session->close();

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getSession($customSessionId);
$unpublished = $session->forceUnpublish($streamId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getSession($customSessionId);
$disconnect = $session->forceDisconnect($connectionId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
...
$session = OpenVidu::getSession($customSessionId);
$isBeingRecording = $session->isBeingRecording($connectionId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
use SquareetLabs\LaravelOpenVidu\Enums\OutputMode;
...
/** @var string */
$recordingName = "Recording of my session";
$recordingProperties = new RecordingProperties($customSessionId, true,true, $recordingName, OutputMode::INDIVIDUAL)
$recording = OpenVidu::startRecording($customSessionId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
use SquareetLabs\LaravelOpenVidu\Enums\OutputMode;
...
$recording = OpenVidu::stopRecording($recordingId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
use SquareetLabs\LaravelOpenVidu\Enums\OutputMode;
...
$recording = OpenVidu::getRecording($recordingId);

use SquareetLabs\LaravelOpenVidu\Facades\OpenVidu;
use SquareetLabs\LaravelOpenVidu\Enums\OutputMode;
...
OpenVidu::deleteRecording($recordingId);

use SquareetLabs\LaravelOpenVidu\Events\ParticipantJoined;

class ParticipantJoinedListener
{
    /**
     * Handle the event.
     *
     * @param  ParticipantJoined  $event
     * @return void
     */
    public function handle(ParticipantJoined $event)
    {
        $event->sessionId;      // Session for which the event was triggered, a string with the session unique identifier
        $event->timestamp;      // Time when the event was triggered, UTC milliseconds
        $event->participantId;  // Identifier of the participant, a string with the participant unique identifier
        $event->platform;       // Complete description of the platform used by the participant to connect to the session
        $event->clientData;     // Additional data added client side while connecting to Session
        $event->serverData;     // Additional data added server side while generating Token
    }
}

use SquareetLabs\LaravelOpenVidu\Events\ParticipantLeft;

class ParticipantLeftListener
{
    /**
     * Handle the event.
     *
     * @param  ParticipantLeft  $event
     * @return void
     */
    public function handle(ParticipantLeft $event)
    {
        $event->sessionId;      // Session for which the event was triggered
        $event->timestamp;      // Time when the event was triggered
        $event->participantId;  // Identifier of the participant
        $event->platform;       // Complete description of the platform used by the participant to connect to the session
        $event->clientData;     // Additional data added client side while connecting to Session
        $event->serverData;     // Additional data added server side while generating Token
        $event->startTime;      // Time when the participant joined the session
        $event->duration;       // Total duration of the participant's connection to the session
        $event->reason;         // How the participant left the session.
    }
}

use SquareetLabs\LaravelOpenVidu\Events\RecordingStatusChanged;

class RecordingStatusChangedListener
{
    /**
     * Handle the event.
     *
     * @param  RecordingStatusChanged  $event
     * @return void
     */
    public function handle(RecordingStatusChanged $event)
    {
       $event->sessionId;	    // Session for which the event was triggered
       $event->timestamp;	    // Time when the event was triggered
       $event->startTime;	    // Time when the recording started
       $event->id;	            // Unique identifier of the recording
       $event->name;	        // Name given to the recording file
       $event->outputMode;	    // Output mode of the recording
       $event->hasAudio;	    // Wheter the recording file has audio or not
       $event->hasVideo;	    // Wheter the recording file has video or not
       $event->recordingLayout;	// The type of layout used in the recording. Only defined if outputMode is COMPOSED and hasVideo is true
       $event->resolution;	    // Resolution of the recorded file. Only defined if outputMode is COMPOSED and hasVideo is true	
       $event->size;            // The size of the video file. 0 until status is stopped
       $event->duration;	    //  Duration of the video file. 0 until status is stopped
       $event->status;	        // Status of the recording
       $event->reason;	        // Why the recording stopped. Only defined when status is stopped or ready
    }
}

use SquareetLabs\LaravelOpenVidu\Events\SessionCreated;

class SessionCreatedListener
{
    /**
     * Handle the event.
     *
     * @param  SessionCreated  $event
     * @return void
     */
    public function handle(SessionCreated $event)
    {
        $event->sessionId; // Session for which the event was triggered
        $event->timestamp; // Time when the event was triggered
    }
}

use SquareetLabs\LaravelOpenVidu\Events\SessionDestroyed;

class SessionDestroyedListener
{
    /**
     * Handle the event.
     *
     * @param  SessionCreated  $event
     * @return void
     */
    public function handle(SessionDestroyed $event)
    {
        $event->sessionId;      // Session for which the event was triggered
        $event->timestamp;      // Time when the event was triggered
        $event->startTime;	    // Time when the session started
        $event->duration;	    // Total duration of the session
        $event->reason;	        // Why the session was destroyed
    }
}

use SquareetLabs\LaravelOpenVidu\Events\WebRTCConnectionCreated;

class WebRTCConnectionCreatedListener
{
    /**
     * Handle the event.
     *
     * @param  WebRTCConnectionCreated  $event
     * @return void
     */
    public function handle(WebRTCConnectionCreated $event)
    {
       $event->sessionId;        // Session for which the event was triggered
       $event->timestamp;        // Time when the event was triggered	UTC milliseconds
       $event->participantId;    // Identifier of the participant	
       $event->connection;       // Whether the media connection is an inbound connection (the participant is receiving media from OpenVidu) or an outbound connection (the participant is sending media to OpenVidu)	["INBOUND","OUTBOUND"]
       $event->receivingFrom;    // If connection is "INBOUND", the participant from whom the media stream is being received	
       $event->audioEnabled;     // Whether the media connection has negotiated audio or not
       $event->videoEnabled;     // Whether the media connection has negotiated video or not
       $event->videoSource;      // If videoEnabled is true, the type of video that is being transmitted
       $event->videoFramerate;   // If videoEnabled is true, the framerate of the transmitted video
       $event->videoDimensions;  // If videoEnabled is true, the dimensions transmitted video
    }
}

use SquareetLabs\LaravelOpenVidu\Events\WebRTCConnectionDestroyed;

class WebRTCConnectionDestroyedListener
{
    /**
     * Handle the event.
     *
     * @param  WebRTCConnectionDestroyed  $event
     * @return void
     */
    public function handle(WebRTCConnectionDestroyed $event)
    {
       $event->sessionId;        // Session for which the event was triggered
       $event->timestamp;        // Time when the event was triggered	UTC milliseconds
       $event->participantId;    // Identifier of the participant	
       $event->connection;       // Whether the media connection is an inbound connection (the participant is receiving media from OpenVidu) or an outbound connection (the participant is sending media to OpenVidu)	["INBOUND","OUTBOUND"]
       $event->receivingFrom;    // If connection is "INBOUND", the participant from whom the media stream is being received	
       $event->audioEnabled;     // Whether the media connection has negotiated audio or not
       $event->videoEnabled;     // Whether the media connection has negotiated video or not
       $event->videoSource;      // If videoEnabled is true, the type of video that is being transmitted
       $event->videoFramerate;   // If videoEnabled is true, the framerate of the transmitted video
       $event->videoDimensions;  // If videoEnabled is true, the dimensions transmitted video
       $event->startTime;        // Time when the media connection was established	UTC milliseconds
       $event->duration;         // Total duration of the media connection	Seconds
       $event->reason;           // How the WebRTC connection was destroyed
    }
}

protected $listen = [
        ...
        'SquareetLabs\LaravelOpenVidu\Events\ParticipantJoined' => [
            'App\Listeners\ParticipantJoinedListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\ParticipantLeft' => [
            'App\Listeners\ParticipantLeftListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\RecordingStatusChanged' => [
            'App\Listeners\RecordingStatusChangedListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\SessionCreated' => [
            'App\Listeners\SessionCreatedListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\SessionDestroyed' => [
            'App\Listeners\SessionDestroyedListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\WebRTCConnectionCreated' => [
            'App\Listeners\WebRTCConnectionCreatedListener',
        ],
        'SquareetLabs\LaravelOpenVidu\Events\WebRTCConnectionDestroyed' => [
            'App\Listeners\WebRTCConnectionDestroyedListener',
        ],
        ...
    ];
bash
php artisan migrate