Wowza Connection Count Script
How many viewers are watching your live stream? There are two built in methods to get your viewer or connection counts during a live stream using Wowza Streaming Engine. The first, new method, is to visit the Engine Manager. You visit the following URL:
http://[ec2-url]:8088/enginemanager/ Â (username: wowza and password is: instance id)
The second method is to access an XML file with the connection information. You visit the following URL:
http://[ec2-url]:8086/connectioncounts
Both of these methods get you what you need, but you have to visit them during the stream, while your server is running.
The following illustrates how to automate this process. In order for the following script to work, you must have port 8086 open on your server, and you must also modify a configuration file on your server so a username and password is not needed.
Modify Configuration File on Server
This assumes you already have a running server with Wowza Streaming Engine up and running. *note: you can do the following using a startup package, explained in another post.
Modify /wowza/conf/VHost.xml changing the following two sections. You are changing “admin-digest” to “none”. The following shows already changed to “none”.
<HTTPProvider> <BaseClass>com.wowza.wms.http.HTTPConnectionInfo</BaseClass> <RequestFilters>connectioninfo*</RequestFilters> <AuthenticationMethod>none</AuthenticationMethod> </HTTPProvider> <HTTPProvider> <BaseClass>com.wowza.wms.http.HTTPConnectionCountsXML</BaseClass> <RequestFilters>connectioncounts*</RequestFilters> <AuthenticationMethod>none</AuthenticationMethod> </HTTPProvider>
Now, you can set your webserver to run the following script as a cron job, and get viewer counts emailed to you automatically.
liveviewers.php
$hostname = "ec2-url";
$xml_data=file_get_contents("http://$hostname:8086/connectioncounts");
$doc = new DOMDocument();
$doc->loadXML($xml_data);
$wms = $doc->getElementsByTagName('WowzaStreamingEngine');
$wmstotalactive = $wms->item(0)->getElementsByTagName("ConnectionsCurrent")->item(0)->nodeValue;
$wmstotaloutbytes = $wms->item(0)->getElementsByTagName("MessagesOutBytesRate")->item(0)->nodeValue;
$wmstotaloutbits = $wmstotaloutbytes * '8';
echo "<center><b>Hostname:</b> $hostname<br></center><hr> <b>Server Total Active Connections:</b> $wmstotalactive<br> <b>Total Outbound bitrate:</b> $wmstotaloutbits<br><hr>";
$emailBody = "Hostname: $hostname \n\n Server Total Active Connections: $wmstotalactive \n Total Outbound bitrate: $wmstotaloutbits\n\n";
$wmsapp = $doc->getElementsByTagName('Application');
$wmscnt = $wmsapp->length;
echo "<center>Applications</center>";
$emailBody .= "Applications";
for ($idx = 0; $idx < $wmscnt; $idx++) {
$appname = $wmsapp->item($idx)->getElementsByTagName("Name")->item(0)->nodeValue;
$appccount = $wmsapp->item($idx)->getElementsByTagName("ConnectionsCurrent")->item(0)->nodeValue;
$appoutbytes = $wmsapp->item($idx)->getElementsByTagName("MessagesOutBytesRate")->item(0)->nodeValue;
$appoutbits = $appoutbytes * '8';
echo "<hr><b>Application Name:</b> $appname<br><b> Active Connections:</b> $appccount<br> <b>Application Bits Out:</b> $appoutbits
<br>";
$emailBody .= "\n Application Name: $appname \n Active Connections: $appccount \n Application Bits Out: $appoutbits \n";
}
echo "<hr><center>Streams</center>";
$emailBody .= "\n Streams";
$wmsast = $doc->getElementsByTagName('Stream');
$wmsasct = $wmsast->length;
for ($sidx = 0; $sidx < $wmsasct; $sidx++) {
$strname = $wmsast->item($sidx)->getElementsByTagName("Name")->item(0)->nodeValue;
$strcuperino = $wmsast->item($sidx)->getElementsByTagName("SessionsCupertino")->item(0)->nodeValue;
$strctot = $wmsast->item($sidx)->getElementsByTagName("SessionsTotal")->item(0)->nodeValue;
echo "<hr><b>Stream URL:</b> $strname <br> <b>Connections to Stream:</b> $strctot<br>";
$emailBody .= "\nStream URL: $strname \n Cupertino: $strcuperino \n Connections to Stream: $strctot\n";
}
$emailTo = "info@domain.com";
$current_date = date("Y-m-d");
$emailSubject = "Live Stream Update: " . $current_date;
$emailFrom = "info@domain.com";
$emailHeader = "From: $emailFrom\n"
. "MIME-Version: 1.0\n"
. "Content-type: text/plain; charset=\"UTF-8\"\n"
. "Content-transfer-encoding: 8bit\n";
mail($emailTo, $emailSubject, $emailBody, $emailHeader);
