246 lines
7.3 KiB
PHP
246 lines
7.3 KiB
PHP
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
|
|
|
|
include_once APPPATH.'/vendor/autoload.php';
|
|
use Twilio\Rest\Client;
|
|
use Twilio\Jwt\AccessToken;
|
|
use Twilio\Jwt\Grants\VideoGrant;
|
|
class Twlio
|
|
{
|
|
|
|
function getServerInfo($print = false,$json = false)
|
|
{
|
|
$twilio = new Client(TWLIO_SID, TWLIO_AUTH_TOKEN);
|
|
$token = $twilio->tokens
|
|
->create();
|
|
$response_data = array();
|
|
$response_data['username'] = $token->username;
|
|
$response_data['ice_servers'] = $token->iceServers;
|
|
$response_data['date_updated'] = $token->dateUpdated;
|
|
$response_data['ttl'] = $token->ttl;
|
|
$response_data['date_created'] = $token->dateCreated;
|
|
$response_data['password'] = $token->password;
|
|
$response_data['accountSid'] = $token->accountSid;
|
|
|
|
if($print)
|
|
{
|
|
$json ? print_r(json_encode($response_data,true)) : print_r($response_data);
|
|
}
|
|
else
|
|
{
|
|
|
|
return json_encode($response_data,true);
|
|
}
|
|
|
|
}
|
|
|
|
function getAPIKey($print = false,$json = false)
|
|
{
|
|
$twilio = new Client(TWLIO_SID, TWLIO_AUTH_TOKEN);
|
|
|
|
$new_key = $twilio->newKeys->create();
|
|
|
|
return array('apikey' => $new_key->sid,'apisecret' => $new_key->secret);
|
|
|
|
// print_r($new_key->dateCreated);
|
|
// print_r($new_key->secret);
|
|
// print_r($new_key->friendyName);
|
|
// try
|
|
// {
|
|
// if(is_object($new_key) && property_exists($new_key, 'secret'))
|
|
// {
|
|
// return $new_key->secret;
|
|
// }
|
|
// else
|
|
// {
|
|
// throw new Exception("Couldn't get API Key from Twlio", 1);
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
// catch(Exception $e)
|
|
// {
|
|
// // print_r($e->getMessage());
|
|
// $data['dataStatus'] = false;
|
|
// $data['status'] = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
|
|
// $data['msg'] = $e->getMessage();
|
|
// $this->response($data,REST_Controller::HTTP_OK);
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
function getVideoAccessJWTToken($apiKey,$apiSecret,$roomName,$officerName)
|
|
{
|
|
|
|
|
|
// Required for all Twilio access tokens
|
|
$twilioAccountSid = TWLIO_SID;
|
|
$twilioApiKey = $apiKey;
|
|
$twilioApiSecret = $apiSecret;
|
|
|
|
// Required for Video grant
|
|
$roomName = $roomName;
|
|
// An identifier for your app - can be anything you'd like
|
|
$identity = $officerName;
|
|
|
|
// Create access token, which we will serialize and send to the client
|
|
$token = new AccessToken(
|
|
$twilioAccountSid,
|
|
$twilioApiKey,
|
|
$twilioApiSecret,
|
|
3600,
|
|
$identity
|
|
);
|
|
|
|
// Create Video grant
|
|
$videoGrant = new VideoGrant();
|
|
$videoGrant->setRoom($roomName);
|
|
|
|
// Add grant to token
|
|
$token->addGrant($videoGrant);
|
|
|
|
// render token to string
|
|
return $token->toJWT();
|
|
}
|
|
|
|
function listOfRoomsByRoomName($roomName)
|
|
{
|
|
echo $roomName;
|
|
$sid = TWLIO_SID;
|
|
$token = TWLIO_AUTH_TOKEN;
|
|
$twilio = new Client($sid, $token);
|
|
|
|
$rooms = $twilio->video->v1->rooms
|
|
->read(["uniqueName" => $roomName,"status" => "completed"], 20);
|
|
// print_r($rooms);die()
|
|
foreach ($rooms as $record) {
|
|
print($record);
|
|
}
|
|
}
|
|
|
|
function getRoomDataByRoomID($roomID)
|
|
{
|
|
//echo $roomName;
|
|
$sid = TWLIO_SID;
|
|
$token = TWLIO_AUTH_TOKEN;
|
|
$twilio = new Client($sid, $token);
|
|
|
|
//$rooms = $twilio->video->v1->rooms
|
|
// ->read(["uniqueName" => $roomName], 20);
|
|
// print_r($rooms);
|
|
$room = $twilio->video->v1->rooms($roomID)
|
|
->fetch();
|
|
// print($room->uniqueName);
|
|
// print($room->accountSid);
|
|
//print($room->type);
|
|
// print($room->status);
|
|
return ($room->status);
|
|
|
|
// foreach ($rooms as $record) {
|
|
// print($record->sid);
|
|
// }
|
|
}
|
|
// 'sid
|
|
// 'status
|
|
// 'dateCreated
|
|
// 'dateUpdated
|
|
// 'accountSid
|
|
// 'enableTurn
|
|
// 'uniqueName
|
|
// 'statusCallback
|
|
// 'statusCallbackMethod
|
|
// 'endTime
|
|
// 'duration
|
|
// 'type
|
|
// 'maxParticipants
|
|
// 'recordParticipantsOnConnect
|
|
// 'videoCodecs
|
|
// 'mediaRegion
|
|
// 'url
|
|
// 'links
|
|
function startComposistion($roomID)
|
|
{
|
|
$apiKeySid = TWILIO_API_KEY;
|
|
$apiKeySecret = TWILIO_API_KEY_SECRET;
|
|
$client = new Client($apiKeySid, $apiKeySecret);
|
|
|
|
$composition = $client->video->compositions->create($roomID, [
|
|
'audioSources' => '*',
|
|
'videoLayout' => array(
|
|
'grid' => array (
|
|
'video_sources' => array('*')
|
|
)
|
|
),
|
|
// 'statusCallback' => 'http://my.server.org/callbacks',
|
|
'format' => 'mp4'
|
|
]);
|
|
//print($composition);
|
|
$result_array['compositionId'] = $composition->sid;
|
|
$result_array['accountSid'] = $composition->accountSid;
|
|
$result_array['status'] = $composition->status;
|
|
$result_array['dateCreated'] = $composition->dateCreated;
|
|
$result_array['dateCompleted'] = $composition->dateCompleted;
|
|
$result_array['roomSid'] = $composition->roomSid;
|
|
$result_array['dateDeleted'] = $composition->dateDeleted;
|
|
$result_array['audioSources'] = $composition->audioSources;
|
|
$result_array['audioSourcesExcluded'] = $composition->audioSourcesExcluded;
|
|
$result_array['videoLayout'] = $composition->videoLayout;
|
|
$result_array['resolution'] = $composition->resolution;
|
|
$result_array['trim'] = $composition->trim;
|
|
$result_array['format'] = $composition->format;
|
|
$result_array['bitrate'] = $composition->bitrate;
|
|
$result_array['size'] = $composition->size;
|
|
$result_array['duration'] = $composition->duration;
|
|
$result_array['url'] = $composition->url;
|
|
$result_array['links'] = $composition->links;
|
|
return $result_array;
|
|
}
|
|
|
|
function getCompositionStatus($composition_id)
|
|
{
|
|
$apiKeySid = TWILIO_API_KEY;
|
|
$apiKeySecret = TWILIO_API_KEY_SECRET;
|
|
$client = new Client($apiKeySid, $apiKeySecret);
|
|
|
|
$composition = $client->video->compositions($composition_id)
|
|
->fetch();
|
|
|
|
$result_array['compositionId'] = $composition->sid;
|
|
$result_array['accountSid'] = $composition->accountSid;
|
|
$result_array['status'] = $composition->status;
|
|
$result_array['dateCreated'] = $composition->dateCreated;
|
|
$result_array['dateCompleted'] = $composition->dateCompleted;
|
|
$result_array['roomSid'] = $composition->roomSid;
|
|
$result_array['dateDeleted'] = $composition->dateDeleted;
|
|
$result_array['audioSources'] = $composition->audioSources;
|
|
$result_array['audioSourcesExcluded'] = $composition->audioSourcesExcluded;
|
|
$result_array['videoLayout'] = $composition->videoLayout;
|
|
$result_array['resolution'] = $composition->resolution;
|
|
$result_array['trim'] = $composition->trim;
|
|
$result_array['format'] = $composition->format;
|
|
$result_array['bitrate'] = $composition->bitrate;
|
|
$result_array['size'] = $composition->size;
|
|
$result_array['duration'] = $composition->duration;
|
|
$result_array['url'] = $composition->url;
|
|
$result_array['links'] = $composition->links;
|
|
return $result_array;
|
|
}
|
|
|
|
function compositionVideoFile($composition_id)
|
|
{
|
|
$apiKeySid = TWILIO_API_KEY;
|
|
$apiKeySecret = TWILIO_API_KEY_SECRET;
|
|
$client = new Client($apiKeySid, $apiKeySecret);
|
|
$uri = "https://video.twilio.com/v1/Compositions/$composition_id/Media?Ttl=3600";
|
|
$response = $client->request("GET", $uri);
|
|
$mediaLocation = $response->getContent()["redirect_to"];
|
|
return $mediaLocation;
|
|
}
|
|
}
|
|
|
|
|
|
?>
|