#A sample Perl script showing some of the features
#in TSReader's control server

use Telnet ();

#Connect to TSReader's control server
$t = new Net::Telnet (Timeout => 1, Port => 1399);
eval{$t->open("127.0.0.1");};
if ($@)
{
 die("Unable to connect to TSReader - is the control server running?\n$@\n");
};

#Display what TSReader sent us
@line = $t->getline();
print "TSReader connected.\nResponse: @line\n";

#Display functions in this program
print "Functions supported are:\n";
print " 1 - print programs in mux\n";
print " 2 - display graph\n";
print " 3 - switch to a multicast UDP mux\n";
print " 4 - play channel via VLC\n";
print "Function? ";
$function =<STDIN>
chop $function;

if ($function eq 1)
{
 #Get and print a list of programs
 $t->print("PROGRAM");
 print "These programs are defined:\n";
 $doit = 1;
 while($doit eq 1)
 {
  eval {@line = $t->getline(Timeout => 1);};
  if ($@)
  {
   $doit=0;
  }
  else
  {
   print @line;
  }
 }
}
elsif ($function eq 2)
{
 #Draw a graph
 $dont_send = 0;
 print "Available graphs are:\n";
 print " 0 - close any existing graph\n";
 print " 1 - Active PIDs by rate\n";
 print " 2 - Active PIDs by PID\n";
 print " 3 - PID usage 2D pie\n";
 print " 4 - PID usage 3D pie\n";
 print " 5 - Mux usage stacked area\n";
 print " 6 - Mux usage line\n";
 print " 7 - Video rate area\n";
 print " 8 - Video rate line\n";
 print " 9 - Program usage\n";
 print " 10 - Video composition\n";
 print "Which chart? ";
 $chart = <STDIN>
 chop $chart;
 if ($chart eq 10)
 {
  print "Program number? ";
  $program_number = <STDIN>
  chop $program_number;
  $t->print("PROGRAM $program_number");
  @line = $t->getline(Timeout => 1);
  if (@line =~ /502/)
  {
   print("?Invalid program number!\n");
   $dont_send = 1;
  }
 }
 if ($dont_send eq 0)
 {
  $t->print("GRAPH $chart");
  @line = $t->getline(Timeout => 1);
 }
}
elsif ($function eq 3)
{
 #Switch to the multicast source and restart TSReader
 print "Multicast address to listen to? ";
 $multicast_address = <STDIN>
 chop $multicast_address;
 print "UDP port to listen to? ";
 $multicast_port = <STDIN>
 chop $multicast_port;

 $t->print("SOURCE TSReader_UDPMulticast.dll");
 @line = $t->getline(Timeout => 1);
 print "Response to SOURCE: @line";

 $t->print("TUNE $multicast_address $multicast_port");
 @line = $t->getline(Timeout => 1);
 print "Response to TUNE: @line";
}
elsif ($function eq 4)
{
 $dont_send = 0;
 print "Program number? ";
 $program_number = ;
 chop $program_number;

 $t->print("PROGRAM $program_number");
 @line = $t->getline(Timeout => 1);
 if (@line =~ /502/)
 {
  print("?Invalid program number!\n");
  $dont_send = 1;
 }
 if ($dont_send eq 0)
 {
  $t->print("PLAY VLC1");
  @line = $t->getline(Timeout => 1);
  print "Response to PLAY: @line";
 }
}

#all done
$t->print("quit");