#!/usr/bin/perl
#
# record_session
#
# An interface between sdr and the MBone VCR.
# sdr launches this script with an SDP session description as input.
# This script creates an MBone VCR .vcr file out of the session description,
# and creates a .cmd file that starts recording immediately, and starts
# the MBone VCR with the .cmd file on the command line.
#
# Bill Fenner <fenner@parc.xerox.com> 22 Apr 1996
#
#
# Set the next line to the MBone VCR executable location
$vcr = "mbone_vcr";

# You should not need to change anything below this line.
require 'ctime.pl';

$ver = "0.9";

$fn = $ARGV[0];
$fn =~ s/\.vcr$//;

$vcrfn = $fn . ".vcr";
$cmdfn = $fn . ".cmd";

open(VCR,">$vcrfn") || die "$vcrfn: $!\n";
print VCR "<MBONE_VCR 1.0>\n";
print VCR "# This file was generated by record_session v$ver\n";
print VCR "session_date=", &ctime(time);
$medianum = 1;

# I don't know if MBone VCR requires any particular ordering of its file.
# Given the SDP fields in the order that sdr outputs them, this loop should
# create the .vcr file in the same order as MBone VCR does.

while (<STDIN>) {
	chop;
	($key,$data) = split(/=/,$_,2);
	if ($key eq "s") {
		print VCR "session_name=$data\n";
	} elsif ($key eq "i") {
		print VCR "session_desc=$data\n";
	} elsif ($key eq "c") {
		if ($medianame) {
			($mediaip,$mediattl) = ($data =~ m|([0-9.]+)/(\d+)$|);
		} else {
			($ip,$ttl) = ($data =~ m|([0-9.]+)/(\d+)$|);
			if ($ttl) {
				print VCR "session_max_ttl=$ttl\n";
			} else {
				print VCR "session_max_ttl=0\n";	# XXX
			}
		}
	} elsif ($key eq "m") {
		if ($medianame) {
			print VCR "${medianum}=media_port=$mediaport\n";
			print VCR "${medianum}=media_ip=$mediaip\n";
			print VCR "${medianum}=media_name=$medianame\n";
			print VCR "${medianum}=media_type=$mediatype\n";
			print VCR "${medianum}=media_mute=0\n";
			print VCR "${medianum}=media_desc=$medianame\n";
			print VCR "${medianum}=media_cmd=$mediacmd\n";
			print VCR "${medianum}=media_cid=$mediacid\n";
			print VCR "${medianum}=media_ttl=$mediattl\n";
			$medianum++;
			undef $medianame;
		}
		($medianame, $mediaport, $type, $fmt) = split(/\s+/, $data);
		$mediacid = 0;
		undef $mediatype;
		undef $mediacmd;
		undef $mediattl;
		$mediaip = $ip;
		if ($type eq "vat") {
			$mediatype = 2;
		} elsif ($type =~ /^rtp$/io || $type =~ m|^rtp/avp$|io ) {
			$mediatype = 1;
		} else {
			print STDERR "Warning: don't know about media type $type so ";
			print STDERR "can't record $medianame media...\n";
			undef $medianame;
			next;
		}
		if ($medianame eq "audio") {
			$mediacmd = "vat";
		} elsif ($medianame eq "video") {
			$mediacmd = "vic";
		} else {
			$mediacmd = "unknown";
		}
	} elsif ($key eq "a") {
		if ($medianame && $data =~ /id:(\d+)/) {
			$mediacid = $1;
		}
	}
}
if ($medianame) {
	print VCR "${medianum}=media_port=$mediaport\n";
	print VCR "${medianum}=media_ip=$mediaip\n";
	print VCR "${medianum}=media_name=$medianame\n";
	print VCR "${medianum}=media_type=$mediatype\n";
	print VCR "${medianum}=media_mute=0\n";
	print VCR "${medianum}=media_desc=$medianame\n";
	print VCR "${medianum}=media_cmd=$mediacmd\n";
	print VCR "${medianum}=media_cid=$mediacid\n";
	print VCR "${medianum}=media_ttl=$mediattl\n";
}
close(VCR);

open(CMD,">$cmdfn") || die "$cmdfn: $!\n";
print CMD "# Load the session description file\n";
print CMD "load $vcrfn\n";
print CMD "# Just in case we're being asked to add to an existing file\n";
print CMD "goto end\n";
print CMD "# Allow recording\n";
print CMD "rec_enabled on\n";
print CMD "# and start recording immediately!\n";
print CMD "rec\n";
close(CMD);

exec "$vcr -c $cmdfn &";
