<?php
$backup = 'rtsp://xxxxxxxxx/live2.sdp';
$main = 'rtsp://xxxxxxxxxxx/live2.sdp';
$i = 100;
while ($i < 200) {
exec("curl --head --connect-timeout 2 -i -X OPTIONS $main >out.log");
$status = 0;
foreach(file('out.log') as $line) {
$status = trim($line) ;
echo "Status : $line \n";
break;
}
if ($status == 'RTSP/1.0 200 OK') {
stopStream($backup);
startStream($main, 'xxx555');
echo "Stream Main Active \n ";
} else {
exec("curl --head --connect-timeout 1 -i -X OPTIONS $backup >out.log");
sleep(1);
$status = 0;
foreach(file('out.log') as $line) {
$status = trim($line);
echo "Status : $line \n";
break;
}
if ($status == 'RTSP/1.0 200 OK') {
stopStream($main);
usleep(200);
startStream($backup, 'xxx555');
echo " Stream Backup Active \n ";
} else {
echo " ERROR ALL INPUT \n ";
}
}
usleep(500);
}
function stopStream($url) {
$options = array (
'http' => array (
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => '{
"uri":"'.$url.'"
}'
)
);
@ getInfoHeader ( "http://xxxxxxxxxxxxxxxx:8081/rest-api/rtsp/terminate", $options );
}
function startStream($url, $streamName) {
$options = array (
'http' => array (
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => '{
"uri":"'.$url.'",
"toStream":"'.$streamName.'"
}'
)
);
$getlog2 = getInfoHeader ( "http://xxxxxxxxxxxxx:8081/rest-api/rtsp/startup", $options );
echo $getlog2;
}
function getInfoHeader($url, $options) {
$context = stream_context_create ( $options );
$result = file_get_contents ( $url, false, $context );
if ($result === FALSE) {
//echo "An error occured!";
}
return $result;
}