File: /home/imensosw/www/imenso.co/dev/wowza_demo/Stream.php
<?php
require('dbconfig.php');
require_once(__DIR__.'/vendor/autoload.php');
include 'Settings.php';
class Stream extends Dbconfig {
protected $hostName;
protected $userName;
protected $password;
protected $dbName;
private $strTable = 'stream_record';
private $dbConnect = false;
public function __construct(){
if(!$this->dbConnect){
$database = new dbConfig();
$this -> hostName = $database -> serverName;
$this -> userName = $database -> userName;
$this -> password = $database ->password;
$this -> dbName = $database -> dbName;
$conn = new mysqli($this->hostName, $this->userName, $this->password, $this->dbName);
if($conn->connect_error){
die("Error failed to connect to MySQL: " . $conn->connect_error);
} else{
$this->dbConnect = $conn;
}
}
}
private function getData($sqlQuery) {
$result = mysqli_query($this->dbConnect, $sqlQuery);
if(!$result){
die('Error in query: '. mysqli_error());
}
$data= array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$data[]=$row;
}
return $data;
}
private function getNumRows($sqlQuery) {
$result = mysqli_query($this->dbConnect, $sqlQuery);
if(!$result){
die('Error in query: '. mysqli_error());
}
$numRows = mysqli_num_rows($result);
return $numRows;
}
public function streamList(){
$sqlQuery = "SELECT * FROM ".$this->strTable." ";
if(!empty($_POST["search"]["value"])){
$sqlQuery .= 'where(id LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR ip LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR port LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR user LIKE "%'.$_POST["search"]["value"].'%" ';
$sqlQuery .= ' OR stream_name LIKE "%'.$_POST["search"]["value"].'%") ';
}
if(!empty($_POST["order"])){
$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
} else {
$sqlQuery .= 'ORDER BY id DESC ';
}
if($_POST["length"] != -1){
$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$result = mysqli_query($this->dbConnect, $sqlQuery);
$sqlQuery1 = "SELECT * FROM ".$this->strTable." ";
$result1 = mysqli_query($this->dbConnect, $sqlQuery1);
$numRows = mysqli_num_rows($result1);
$streamData = array();
$i=0;
while( $stream = mysqli_fetch_assoc($result) ) {
$strRows = array();
$i++;
//$strRows[] = $stream['id'];
$strRows[] = $i;
$strRows[] = $stream['ip'];
$strRows[] = $stream['port'];
$strRows[] = $stream['user'];
$strRows[] = $stream['stream_name'];
/*$strRows[] = '<button type="button" name="update" id="'.$stream["id"].'" class="btn btn-warning btn-xs update">Update</button>';*/
$strRows[] = '<button type="button" name="delete" id="'.$stream["id"].'" class="btn btn-danger btn-xs delete" >Delete</button>';
if($stream['status'] == "disconnected")
{
$strRows[] = '<button type="button" name="connect" id="'.$stream["id"].'" class="btn btn-success btn-xs connectdisconnect">Connect</button>';
}
else
{
$strRows[] = '<button type="button" name="disconnect" id="'.$stream["id"].'" class="btn btn-danger btn-xs connectdisconnect" >Disconnect</button> <a href="view.php?stream_name='.$stream['stream_name'].'" role="button" name="View Stream" id="view_'.$stream["id"].'" class="btn btn-danger btn-xs connectdisconnect" target="_blank" style="margin-top:1px;">View Streaming</a>';
}
$streamData[] = $strRows;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $numRows,
"recordsFiltered" => $numRows,
"data" => $streamData
);
echo json_encode($output);
}
public function getStream(){
if($_POST["strId"]) {
$sqlQuery = "
SELECT * FROM ".$this->strTable."
WHERE id = '".$_POST["strId"]."'";
$result = mysqli_query($this->dbConnect, $sqlQuery);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo json_encode($row);
}
}
public function updateStream(){
if($_POST['strId']) {
$updateQuery = "UPDATE ".$this->strTable."
SET ip = '".$_POST["strIP"]."', port = '".$_POST["strPort"]."', user = '".$_POST["username"]."', pass = '".$_POST["password"]."' WHERE id ='".$_POST["strId"]."'";
$isUpdated = mysqli_query($this->dbConnect, $updateQuery);
}
}
public function addStream(){
$stream_name = "stream_".rand(1, 10000);
$setup = $this->wowzaSetting();
$sf = new Com\Wowza\StreamFile($setup, APPLICATION_NAME, $stream_name);
if($_POST["strIP"] != '')
{
$response = $sf->create(array("uri"=>"rtsp://".$_POST["username"].":".$_POST["password"]."@".$_POST["strIP"].":".$_POST["strPort"]), "rtp");
}
else
{
$response = $sf->create(array("uri"=>"rtsp://".$_POST["username"].":".$_POST["password"]."@".$_POST["strIP"]), "rtp");
}
/*print_r($response);
die();*/
if($response->success)
{
$insertQuery = "INSERT INTO ".$this->strTable." (ip, port, user, pass, stream_name)
VALUES ('".$_POST["strIP"]."', '".$_POST["strPort"]."', '".$_POST["username"]."', '".$_POST["password"]."', '".$stream_name."')";
$isUpdated = mysqli_query($this->dbConnect, $insertQuery);
}
}
public function deleteStream(){
if($_POST["strId"])
{
$sqlQuery = "SELECT * FROM ".$this->strTable." WHERE id = '".$_POST["strId"]."'";
$result = mysqli_query($this->dbConnect, $sqlQuery);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$setup = $this->wowzaSetting();
$application = new Com\Wowza\Application($setup, APPLICATION_NAME);
$response = $application->getIncomingStream($row['stream_name'].'.stream');
if($response->name)
{
$sf = new Com\Wowza\StreamFile($setup, APPLICATION_NAME, $row['stream_name']);
$disconect_response = $sf->disconnect();
}
$deletesf = new Com\Wowza\StreamFile($setup, APPLICATION_NAME, $row['stream_name']);
$remove_response = $deletesf->remove();
if ($remove_response->success)
{
$sqlDelete = "DELETE FROM ".$this->strTable." WHERE id = '".$_POST["strId"]."'";
mysqli_query($this->dbConnect, $sqlDelete);
}
}
}
public function streamConnection()
{
$sqlQuery = "SELECT * FROM ".$this->strTable." WHERE id = '".$_POST["strId"]."'";
$result = mysqli_query($this->dbConnect, $sqlQuery);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$setup = $this->wowzaSetting();
$sf = new Com\Wowza\StreamFile($setup, APPLICATION_NAME, $row['stream_name']);
if($row['status'] == "disconnected")
{
$response = $sf->connect();
if ($response->success)
{
$updateQuery = "UPDATE ".$this->strTable." SET status = 'connected' WHERE id ='".$_POST["strId"]."'";
$isUpdated = mysqli_query($this->dbConnect, $updateQuery);
}
}
else
{
$response = $sf->disconnect();
if ($response->success)
{
$updateQuery = "UPDATE ".$this->strTable." SET status = 'disconnected' WHERE id ='".$_POST["strId"]."'";
$isUpdated = mysqli_query($this->dbConnect, $updateQuery);
}
}
}
public function wowzaSetting()
{
$setup = new Com\Wowza\Entities\Application\Helpers\Settings();
$setup->setHost(WOWZA_HOST);
$setup->setUsername(WOWZA_USERNAME);
$setup->setPassword(WOWZA_PASSWORD);
return $setup;
}
}
?>