[icecast] XML statistics on remote server

Adon Irani adon at yorku.ca
Thu Feb 26 15:15:15 PST 2004



> Interesting! Can you tell me more about how you did it?
>

hi max ., here's some code . . (its for http://theDV8network.com , w/ 2
radio channels , and a possible live feed -- but you should be able to use
the functions accordingly )

let me know if i can help w/ anything else ..

a.


--

function check_listeners( $mountpoint, $stream ) {
	global $CONFIG;

	// $mountpoint = channel1, channel2, dv8_live
	// $stream = 'dialup', 'hispeed', 'mp3', 'ogg'

	if( $mountpoint == "dv8_live" ) {
		if( $stream == "mp3" ) $mountpoint = 'channel1_mp3';
		elseif( $stream == "ogg" ) $mountpoint = 'dv8_live.ogg';
	}elseif( $mountpoint == "1" ) {
		if( $stream == "mp3" ) $mountpoint = 'channel1_mp3';
		elseif( $stream == "dialup" ) $mountpoint =
'channel1_dialup.ogg';
		elseif( $stream == "hispeed" ) $mountpoint =
'channel1_hispeed.ogg';
	}elseif( $mountpoint == "2" ) {
		if( $stream == "mp3" ) $mountpoint = 'channel2_mp3';
		elseif( $stream == "dialup" ) $mountpoint =
'channel2_dialup.ogg';
		elseif( $stream == "hispeed" ) $mountpoint =
'channel2_hispeed.ogg';
	}
	if( !file_exists( '/tmp/icecast_status.xsl' )) {
		echo '<br>check_transcoder :: creating new file';
		$exec_command = 'lynx
http://'.$CONFIG['primaryhost'].':8000/status.xsl --dump >
/tmp/icecast_status.xsl';
		exec( $exec_command );
	}
	$exec_command = 'cat /tmp/icecast_status.xsl | grep -A5
'.$mountpoint.' | grep Listeners | cut -c 47- ';
	$count = exec($exec_command );
	if( $count > "" && $count >= 0 ) return $count;
	else return -1;
}

function check_title( $mountpoint ) {
	global $CONFIG;

	// $mountpoint = channel1, channel2, dv8_live
	if( $mountpoint == "dv8_live" ) {
		$mountpoint1 = 'dv8_live.ogg';
	} elseif( $mountpoint == "1" ) {
		$mountpoint1 = 'channel1_dialup.ogg';
		$mountpoint2 = 'channel1_hispeed.ogg';
	} elseif( $mountpoint == "2" ) {
		$mountpoint1 = 'channel2_dialup.ogg';
		$mountpoint2 = 'channel2_hispeed.ogg';
	}
	$exec_command = 'lynx
http://'.$CONFIG['primaryhost'].':8000/status.xsl --dump | grep -A5
'.$mountpoint.' | grep Listeners | cut -c 47- ';
	$count = exec($exec_command );
	if( $count > "" && $count >= 0 ) return $count;
	else return -1;
}

<p>function check_stream( ) {
	global $CONFIG;

// update listeners status.xsl
	$exec_command = 'lynx
http://'.$CONFIG['primaryhost'].':8000/status.xsl --dump >
/tmp/icecast_status.xsl';
	exec( $exec_command );

	$threehrsago = date( "YmdHis", strtotime( ' -5 hours' ));
	$channel1 = false;
	$channel2 = false;
	$dv8live_id = false;
	$sql_result = query("SELECT * FROM ices_session WHERE
timestamp>=\"".$threehrsago."\" ORDER BY id ASC");

// theses are the id's which SHOULD BE ..
	if( mysql_num_rows( $sql_result )) {
		for( $x=0; $x < mysql_num_rows( $sql_result ); $x++ ) {
			$sql_entry = mysql_fetch_array($sql_result);
			if( $sql_entry['playlist'] == 'dv8_live' ) {
				$sql_result2 = query("SELECT * FROM
archive WHERE status=\"livesync\" ");
				$sql_entry2 =
mysql_fetch_array($sql_result2);
				$dv8live_id = $sql_entry2['id'];
			}elseif( $sql_entry['channel'] == '1' ) {
				$channel1_file = $sql_entry['playlist'];
				$channel1_id = $sql_entry['listenid'];
			}elseif( $sql_entry['channel'] == '2' ) {
				$channel2_file = $sql_entry['playlist'];
				$channel2_id = $sql_entry['listenid'];
			}
		}
	}

// should have most recent ices_session for each mountpoint during past 3
hrs
// however, these might not be active currently .
// compare actual playlist
	$live = false;
	if( $dv8live_id > 0 ) {
		// check listener count on live stream to be sure it
exists
		$transcode = check_listeners( 'dv8_live', 'mp3' );
		$cur_live = check_listeners( 'dv8_live', 'ogg' );
		if( $cur_live == "-1" ) $cur_live = 0;
		$cur_listeners = $cur_live;
		if( $transcode >= 0 ) {
			$cur_listeners -= 1;
			$cur_listeners += $transcode;
		}
		if( $cur_listeners >= 0 ) {
	// show is live , allow link to listen .
			$live['3'] = $dv8live_id;
		}
	}
	if( $channel1_id > 0 ) {
		if( file_exists(
$CONFIG['livesync_dir'].'/channel1_dialup.txt' )) {
			$exec_command = 'cat
'.$CONFIG['livesync_dir'].'/channel1_dialup.txt';
			$playlist_dialup = exec( $exec_command );
		}
		if( file_exists(
$CONFIG['livesync_dir'].'/channel1_hispeed.txt' )) {
			$exec_command = 'cat
'.$CONFIG['livesync_dir'].'/channel1_hispeed.txt';
			$playlist_hispeed = exec( $exec_command );
		}
		if( $channel1_file == $playlist_dialup || $channel1_file
== $playlist_hispeed ) {
			// there is a match , check listener count to be
sure stream exists
			$cur_dialup = check_listeners( '1', 'dialup' );
			$cur_hispeed = check_listeners( '1', 'hispeed' );

			if( $cur_dialup >= 0 || $cur_hispeed >= 0 ) {
		// show is live , allow link to listen .
				$live['1'] = $channel1_id;
			}
		}
	}
	if( $channel2_id > 0 ) {
		if( file_exists(
$CONFIG['livesync_dir'].'/channel2_dialup.txt' )) {
			$exec_command = 'cat
'.$CONFIG['livesync_dir'].'/channel2_dialup.txt';
			$playlist_dialup = exec( $exec_command );
		}
		if( file_exists(
$CONFIG['livesync_dir'].'/channel2_hispeed.txt' )) {
			$exec_command = 'cat
'.$CONFIG['livesync_dir'].'/channel2_hispeed.txt';
			$playlist_hispeed = exec( $exec_command );
		}
		if( $channel2_file == $playlist_dialup || $channel2_file
== $playlist_hispeed ) {
			// there is a match , check listener count to be
sure stream exists
			$cur_dialup = check_listeners( '2', 'dialup' );
			$cur_hispeed = check_listeners( '2', 'hispeed' );

			if( $cur_dialup >= 0 || $cur_hispeed >= 0 ) {
		// show is live , allow link to listen .
				$live['2'] = $channel2_id;
			}
		}
	}
	return $live;
}

<p>---

<p>$streams = check_stream( );
$channel1_id = $streams['1'];
$channel2_id = $streams['2'];
$dv8live_id = $streams['3'];

echo '<br>rhizome-x_listeners :: ch1('.$channel1_id.') _
ch2('.$channel2_id.') _ live('.$dv8live_id.')  ';

// ensure that no transcoder sessions run on -- i think this crashes
icecast
$dv8live = check_listeners( 'dv8_live', 'ogg' );
$cur_dialup = check_listeners( '1', 'dialup' );
$cur_hispeed = check_listeners( '1', 'hispeed' );
if(( $dv8live == "-1" && $cur_dialup == "-1" && $cur_hispeed == "-1" ) ||
($cur_dialup == "-1" && $cur_hispeed == "-1")) {
//	echo '<br>rhizome-x_listeners :: clear ch1 ';
//	stop_ices( '1' );
//	stop_transcoder( '1' );
}
$cur_dialup = check_listeners( '2', 'dialup' );
$cur_hispeed = check_listeners( '2', 'hispeed' );
if( $cur_dialup == "-1" && $cur_hispeed == "-1" ) {
//	echo '<br>rhizome-x_listeners :: clear ch2 ';
//	stop_ices( '2' );
//	stop_transcoder( '2' );
}

//var_dump( $streams );
if( !isset( $dv8live_id )) {
//	echo 'rhizome-x_listeners :: purge dv8_live sessions';
	$sql_result = query("DELETE FROM ices_session WHERE channel=\"1\"
AND playlist=\"dv8_live\" ");
}
if( !isset( $channel1_id )) {
//	echo 'rhizome-x_listeners :: purge channel 1 sessions';
	$sql_result = query("DELETE FROM ices_session WHERE channel=\"1\"
AND playlist!=\"dv8_live\" ");
}
if( !isset( $channel2_id )) {
//	echo 'rhizome-x_listeners :: purge channel 2 sessions';
	$sql_result = query("DELETE FROM ices_session WHERE channel=\"2\"
");
}

if( $streams['3'] > 0 ) {
	$sql_result = query("SELECT * FROM archive WHERE
status=\"livesync\" ");
	if( mysql_num_rows( $sql_result )) {
		$sql_entry = mysql_fetch_array($sql_result);
		$channel1 = $sql_entry['title'];
		$sql_result = query("SELECT * FROM ices_session WHERE
playlist=\"dv8_live\" ORDER BY id DESC ");
		$sql_entry = mysql_fetch_array($sql_result);
		$check_id = $sql_entry['id'];

//		if( strlen( $channel1 ) > intval(FORMAT_WIDTH/2) )
$channel1 = substr( $channel1, 0, intval(FORMAT_WIDTH/2)-3 ).'...';
		$query_update = 'UPDATE ices_session SET
title="'.$channel1.'" ';
		$query_update .= 'WHERE id="'.$check_id.'" ';
		$result_update = query( $query_update );

		$transcode = check_listeners( 'dv8_live', 'mp3' );
		$cur_dv8live = check_listeners( 'dv8_live', 'ogg' );
		if( $cur_dv8live == "-1" ) $cur_dv8live = 0;
		$cur_listeners = $cur_dv8live;
		if( $transcode >= 0 ) {
			$cur_listeners -= 1;
			$cur_listeners += $transcode;
		}
		$query_update = 'UPDATE ices_session SET
listeners="'.$cur_listeners.'" ';
		$query_update .= 'WHERE id="'.$check_id.'" ';
		$result_update = query( $query_update );
	}
}
if( $streams['1'] > 0 ) {
	$sql_result = query("SELECT *, archive.title as show_title,
ices_session.id as ices_id FROM ices_session LEFT JOIN archive ON
ices_session.listenid=archive.id WHERE archive.id=\"".$channel1_id."\"
ORDER BY ices_id DESC ");
	$add = false;
	while( $sql_entry = mysql_fetch_array($sql_result)) {
		$channel1 = $sql_entry['show_title'];
		$check_id = $sql_entry['ices_id'];
		$query_update = 'UPDATE ices_session SET
title="'.$channel1.'" ';
		$query_update .= 'WHERE listenid="'.$channel1_id.'" AND
id="'.$check_id.'" ';
		$result_update = query( $query_update );
		if( !$add ) {
			$transcode = check_listeners( '1', 'mp3' );
			$cur_dialup = check_listeners( '1', 'dialup' );
			$cur_hispeed = check_listeners( '1', 'hispeed' );
			if( $cur_dialup == "-1" ) $cur_dialup = 0;
			if( $cur_hispeed == "-1" ) $cur_hispeed = 0;
			$cur_listeners = $cur_dialup + $cur_hispeed;
			if( $transcode >= 0 ) {
				$cur_listeners -= 1;
				$cur_listeners += $transcode;
			}
			$add=true;
		}
		$query_update = 'UPDATE ices_session SET
listeners="'.$cur_listeners.'" ';
		$query_update .= 'WHERE listenid="'.$channel1_id.'" AND
channel="1" AND id="'.$check_id.'" ';
		$result_update = query( $query_update );
	}
}
if( $streams['2'] > 0 ) {
	$sql_result = query("SELECT *, archive.title as show_title,
ices_session.id as ices_id FROM archive LEFT JOIN ices_session ON
ices_session.listenid=archive.id WHERE archive.id=\"".$channel2_id."\"
ORDER BY ices_id DESC ");
	$add = false;
	while( $sql_entry = mysql_fetch_array($sql_result)) {
		$channel2 = $sql_entry['show_title'];
		$check_id = $sql_entry['ices_id'];
		$query_update = 'UPDATE ices_session SET
title="'.$channel2.'" ';
		$query_update .= 'WHERE listenid="'.$channel2_id.'" ';
		$result_update = query( $query_update );
		if( !$add ) {
			$transcode = check_listeners( '2', 'mp3' );
			$cur_dialup = check_listeners( '2', 'dialup' );
			$cur_hispeed = check_listeners( '2', 'hispeed' );
			if( $cur_dialup == "-1" ) $cur_dialup = 0;
			if( $cur_hispeed == "-1" ) $cur_hispeed = 0;
			$cur_listeners = $cur_dialup + $cur_hispeed;
			if( $transcode >= 0 ) {
				$cur_listeners -= 1;
				$cur_listeners += $transcode;
			}
			$add = true;
		}
		$query_update = 'UPDATE ices_session SET
listeners="'.$cur_listeners.'" ';
		$query_update .= 'WHERE listenid="'.$channel2_id.'" AND
channel="2" AND id="'.$check_id.'" ';
		$result_update = query( $query_update );
	}
}

<p><p>--

ices_session sql ..

<p># Table structure for table `ices_session`
#
CREATE TABLE ices_session (
  id int(11) NOT NULL auto_increment,
  listenid int(11) NOT NULL default '0',
  channel enum('1','2') NOT NULL default '1',
  playlist varchar(64) NOT NULL default '',
  timestamp timestamp(14) NOT NULL,
  listeners int(11) NOT NULL default '0',
  title varchar(64) NOT NULL default '',
  KEY id (id)
) TYPE=MyISAM;

--- >8 ----
List archives:  http://www.xiph.org/archives/
icecast project homepage: http://www.icecast.org/
To unsubscribe from this list, send a message to 'icecast-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.




More information about the Icecast mailing list