#!/usr/bin/perl -w
#  $Id$
# -----------------------------------------------------------------------------
#  Purpose:
#    - probe the camera
#    - pre-tune camera befor for retriever (RC and Audio)
# -----------------------------------------------------------------------------
#  Call:
#    probe DEVID=123
#    probe DEVID=123 PROBE=RESET
#    probe DEVID=123 DEVIP=192.168.17.177 [ ... ]
#    probe DEVIP=192.168.17.177 USRNAME=admin PASSWD=pass PROBE=DEFINE
# -----------------------------------------------------------------------------
#  Does:
#   0. can be called without DEVID argument.
#      In this case DEVIP,USRNAME,PASSWD should be provided in command line
#   1. load $APL_CONF/<DEVID>/conf if DEVID is provided
#   2. combine conf and args into one hash
#   3. connect to camera over http and read MODELID and FIRMWARE
#   4. report MODELID|FIRMWARE|STATUS to $APL_CONF/<DEVID>/conf.probe and stdout
#   5. if PROBE=DEFINE then get
#         IMAGESIZE_LIST,MEDIA_FORMAT_LIST
#         AUDIO_LIST,AUDIO_FORMAT_LIST
#         SNAPSHOT (picture)
#   6. set RC camera attributes    if mpeg4 | h264
#   7. set AUDIO camera attributes if AUDIO is on
#   8. example output:
#       AUDIO_SET=OK
#       FIRMWARE=5.02
#       MODELID=Q1755
#       RC_SET=OK
#       STATUS=OK
#       -------------
#       AUDIO_SET=NONE
#       FIRMWARE=5.02
#       MODELID=Q1755
#       RC_SET=NONE
#       STATUS=OK
#       SNAPSHOT=/tmp/probe/192.168.17.177-12367576123.jpg
#       IMAGESIZE_LIST=640x480,480x360,320x240,240x180,176x144,160x120
#       MEDIA_FORMAT_LIST=mjpeg,h264
#       AUDIO_LIST=off,on
#       AUDIO_FORMAT_LIST=g711,g726,aac
#   9. sample errors:
#       STATUS=ERROR: PCE-0001 [101] configuration is not found
#       STATUS=ERROR: PCE-0002 [101] USRNAME and PASSWD should provided
#       STATUS=ERROR: PCE-0003 [101] DEVIP is not defined
#       STATUS=ERROR: PCE-0500 [101] Device does not respond (http://207.107.163.123:80)
#       STATUS=ERROR: PCE-0401 [101] Authorization error
#       STATUS=ERROR: PCE-0403 [101] Forbidden
#       STATUS=ERROR: PCE-0030 [101] Cannot get MODELID
#       note: [101] is DEVID
#  10. warnings:
#       STATUS=WARNING: PCW-0001 [101] MODELID does not match configuration
#       STATUS=WARNING: PCW-0002 [101] FIRMWARE does not match configuration
#
# -----------------------------------------------------------------------------
#  Note:
#   1. CAMERAMODEL file is obsolite. ptz_axisv2.pl & ptz_udp.pl has to be modified
#   2. Script mast be in the directory .../camera/<BRAND>/bin/
# -----------------------------------------------------------------------------
#  Author: jscott, 07/07/12
#  Edited by:
#  QA by:
#  Copyright: videoNEXT Network Solutions, Inc, 2012
# -----------------------------------------------------------------------------
# SentryView probe

use strict;
use Device::Conf qw(:all);
use XML::Simple;
use JSON "to_json";
#use Data::Dumper;
# cons -------------------------------------------------------------------------

# --------- check required parameters: DEVID,USRNAME,PASSWD,
my $conf=ProbeInit();  # uses <BRAND> from path and ARGV
my $APL = $ENV{APL} || '/opt/sarch';
ProbeErr("PCE-0001","configuration is not found") if not defined $conf->{DEVID};
ProbeErr("PCE-0003","DEVIP is not defined")       if not defined $conf->{DEVIP};
ProbeErr("PCE-0002","USRNAME and PASSWD should be provided")
                 if not defined $conf->{USRNAME} or not defined $conf->{PASSWD};
#------------------------------------------------------------------------------
# Probe camera MODELID & FIRMWARE
#------------------------------------------------------------------------------
my %result=(MODELID=>'0',FIRMWARE=>'0.0',STATUS=>'OK',RC_SET=>'NONE',AUDIO_SET=>'NONE');

# Checking model
# VNE:  /nvc-cgi/admin/param.cgi?action=list&group=Brand
#Brand.brand=VNE
#Brand.prodfullname=VNE-C1000 Network Video Encoder
#Brand.prodshortname=VNE-C1000
#Brand.prodnbr=0
#Brand.prodtype=
#Brand.weburl=www.videonext.com
#Brand.authenkey=8BA4D163B633007248531BA8CB6D2F07

UA()->timeout(30); # Slow camera, increase timeout
my $respond=ProbeRequest("/nvc-cgi/admin/param.cgi?action=list&group=Brand");
if($respond!~ /^HTTP ERROR/) {
  $result{MODELID} = $1 if $respond=~/^Brand\.prodshortname\s*=\s*(Sentry.+)$/im;
}else {
  my ($err)= $respond=~/^HTTP ERROR \[(\d+)\]/;
  ProbeErr("PCE-0500","Device does not respond",$respond) if $err=~/^5\d\d/;
  ProbeErr("PCE-0401","Authorization error",    $respond) if $err==401;
}
ProbeErr("PCE-0030","Cannot get MODELID or FIRMWARE",$respond) if ! $result{MODELID};

# Get firmware
$respond=ProbeRequest("/nvc-cgi/admin/param.cgi?action=list&group=Version.firmware");
if($respond!~ /^HTTP ERROR/) {
  $result{FIRMWARE} = $1 if $respond =~ /^Version.firmware=(.+?)\s*$/im;
}else{
  ProbeErr("PCE-0030","Cannot get MODELID or FIRMWARE",$respond);
}

# Check MODELID & FIRMWARE ---------------------------------------------------
if(defined $conf->{MODELID} and $result{MODELID} ne $conf->{MODELID}) {
  $result{STATUS}="WARNING: PCW-0001 [$conf->{DEVID}] MODELID does not match configuration";
}elsif(defined $conf->{CAMERAFIRMWARE} and $result{FIRMWARE} ne $conf->{CAMERAFIRMWARE}) {
  $result{STATUS}="WARNING: PCW-0001 [$conf->{DEVID}] FIRMWARE does not match configuration";
}

#------------------------------------------------------------------------------
# get Sample picture
#------------------------------------------------------------------------------
if($conf->{PROBE} eq 'DEFINE') {
   $result{SNAPSHOT}=ProbeSamplePicture();
}

#------------------------------------------------------------------------------
# get video and audio properties
#------------------------------------------------------------------------------

# Get list of Inputs and Outputs
if ($conf->{PROBE} eq 'DEFINE') {
    $respond = ProbeRequest("/nvc-cgi/admin/param.fcgi?action=list&group=DIDO");
    if ($respond !~ /^HTTP_ERROR/) {
	my ($di_nbr) = $respond=~/DI\.nbrofchannel=(\d+)\s*$/im;
	my ($do_nbr) = $respond=~/DO\.nbrofchannel=(\d+)\s*$/im;
	$result{AE_DESCR} = to_json({
	    relay => { NUMBER => $do_nbr, PORT_NUMBERS => join(',',0..$do_nbr-1), TEMPLATE => 'GENERIC' },
	    sensor_skm => { NUMBER => $di_nbr, PORT_NUMBERS => join(',',0..$di_nbr-1), TEMPLATE => 'GENERIC' },
	});
    }
}

#------------------------------------------------------------------------------
# interim report & exit
#------------------------------------------------------------------------------
ProbeResult(\%result)  if $conf->{PROBE} ne 'RESET';
ProbeResult(\%result)  if $conf->{DEVID}==0;

#------------------------------------------------------------------------------
# set RC (rate control) parameters + qality + imagesize
#------------------------------------------------------------------------------
my $err=0;
my $postmortem='';
my $camera=$conf->{CAMERA}-1;
$camera=0 if $camera < 0;
my $channel=$conf->{CHANNEL};
$channel=(defined($channel) and $channel ne "")?$channel:'0';

# 1. Setting codec
my $codec="";
for ($conf->{MEDIA_FORMAT}) {
	/mjpg/i  && do { $codec="mjpeg"; last };
	/mpeg4/i && do { $codec="mpeg4"; last };
	/h264/i  && do { $codec="h264" ; last };
}

# reboot encoder
$respond = ProbeRequest("/nvc-cgi/admin/param.fcgi?action=update&group=System.Power&control=coldboot");
if ($respond =~ /^HTTP ERROR/)
{
    $err++;
    $postmortem .= "Fail to reboot encoder: $respond\n";
} else {
    my $test_req = "/nvc-cgi/admin/param.cgi?action=list&group=Brand";
    for(;;sleep 2) {
        last if ProbeRequest($test_req) =~ /^HTTP ERROR/;
    }
    #print "#Rebooted, time=".time."\n";
    for(;;sleep 5) {
        last if ProbeRequest($test_req) !~ /^HTTP ERROR/;
    }
    #print "#Recovered after reboot, time=".time."\n";

    sleep 20; # let's wait while encoder is fully loaded
}

# Let's check up some encoder parameters whose
# change demands the considerable expense of time.
# Probably it is not necessary to change them.
$respond = ProbeRequest("/nvc-cgi/admin/param.cgi?action=list&group=VideoCodec.Ch$channel.St$camera.standard");
if($respond !~ /^HTTP ERROR/ and not $respond =~ /=$codec\s*$/im) {
    my $update_codec_url = "/nvc-cgi/admin/param.cgi?action=update&group=VideoCodec.Ch$channel.St$camera&enable=yes&standard=$codec";
    $respond = ProbeRequest($update_codec_url);
    if ($respond =~ /error/i)
    {
        $err++;
        $postmortem .= "Could not set codec: $respond\n";
    }
}
my $meta = 'yes';
$respond=ProbeRequest("/nvc-cgi/admin/param.cgi?action=list&group=Server");
if($respond=~/error/i
   or not $respond =~ /^Server.RTSP.S0.Unicast.RTP$camera.enable=yes\s*$/im
   or not $respond =~ /^Server.RTSP.S0.Unicast.RTP$camera.name=$conf->{CAMERA}\s*$/im
   or not $respond =~ /^Server.RTSP.S0.Unicast.RTP$camera.includemeta=$meta\s*$/im) {
    # changing stream name corresponding to camera number
    $respond=ProbeRequest(
        "/nvc-cgi/admin/param.cgi?action=update&group=Server.RTSP.S0.Unicast.RTP$camera&enable=yes&name=$conf->{CAMERA}&includemeta=$meta"
    );
    if ($respond =~ /error/i)
    {
        $err++;
        $postmortem .= "Could not set stream name: $respond\n";
    }
}
if($meta eq 'yes') {
    $respond=ProbeRequest("/nvc-cgi/admin/vca.cgi?action=list&group=VCA.Ch$channel");
    if($respond =~ /error/i
       or not $respond =~ /^VCA.Ch$channel.enable=yes\s*$/im
       or not $respond =~ /^VCA.Ch$channel.enablestb=yes\s*$/im
       or not $respond =~ /^VCA.Ch$channel.enablemovobj=yes\s*$/im
       or not $respond =~ /^VCA.Ch$channel.metafmt=object,event,/im) {
        # enable VCAsys engine
        $respond=ProbeRequest(
            "/nvc-cgi/admin/vca.cgi?action=update&group=VCA.Ch$channel&enable=yes&enablestb=yes&enablemovobj=yes&metafmt=object,event,"
        );
        if ($respond =~ /error/i)
        {
            $err++;
            $postmortem .= "Could not set stream name: $respond\n";
        }
    }
}

# 2. Setting framerate, bitrate, resolution
my $update_codec_params_url;
my $fps = $conf->{FRAMERATE};
$fps = 1 if ($fps < 1);
my $resolution = $conf->{IMAGESIZE};
$resolution = "vga" if ($resolution =~ /hugesize/i);
$resolution = "cif" if ($resolution =~ /fullsize/i);
$resolution = "qcif" if ($resolution =~ /halfsize/i);

my $rc_mode          = $conf->{RC_MODE};
my $rc_maxbitrate    = $conf->{RC_MAXBITRATE};
my $rc_targetbitrate = $conf->{RC_TARGETBITRATE};
my $compression      = $conf->{CAMCOMPRESSION};
my $qvalue           = $conf->{QVALUE};
my $pcount           = $conf->{GOP}; # GOP settings
if ($conf->{MEDIA_FORMAT} =~ /mjpg/i)
{
	my $quality = 100 - $conf->{CAMCOMPRESSION};

	if ($quality <= 0) { $quality = 5; }
	elsif ($quality >= 100) { $quality = 95; }

	$update_codec_params_url =
	    "/nvc-cgi/admin/param.cgi?action=update&group=VideoCodec.Ch$channel.St$camera.mjpeg"
	   ."&resolution=$resolution&quality=$quality&maxfps=$fps";
}
elsif ($conf->{MEDIA_FORMAT} =~ /mpeg4/i or $conf->{MEDIA_FORMAT} =~ /h264/i)
{

	if ($rc_maxbitrate ne "cam-defined")
	{
	    if ($rc_maxbitrate =~ /unlimited/i)
	    {
		$rc_maxbitrate = $rc_mode eq "cbr" ? 4096 : 0;
	    }
	    elsif ($rc_maxbitrate > 4096)
	    {
		$rc_maxbitrate = 4096;
	    }
	    elsif ($rc_maxbitrate < 128)
	    {
		$rc_maxbitrate = 128;
	    }
	}

	# Normalize Target bitrate
	if ($rc_targetbitrate ne "cam-defined")
	{
	    if ($rc_targetbitrate =~ /unlimited/i)
	    {
		$rc_targetbitrate = 4096;
	    }
	    elsif ($rc_targetbitrate > 4096)
	    {
		$rc_targetbitrate = 4096;
	    }
	    elsif ($rc_targetbitrate < 128)
	    {
		$rc_targetbitrate = 128;
	    }
	}

	# Normalize qvalue
	$qvalue = 1 if not defined $qvalue or $qvalue < 1;
	$qvalue = 255 if $qvalue > 255;

	# Normalize pcount
	$pcount = 0 if $pcount < 1;
	$pcount = 127 if $pcount > 127;
	$pcount = "cam-defined" if not defined $pcount;

	my $codec =  "mpeg4" if ($conf->{MEDIA_FORMAT} =~ /mpeg4/i);
	$codec = "h264" if ($conf->{MEDIA_FORMAT} =~ /h264/i);

	$update_codec_params_url =
	  "/nvc-cgi/admin/param.cgi?action=update&group=VideoCodec.Ch$channel.St$camera.$codec"
	. "&resolution=$resolution&maxfps=$fps&bitratectrl=$rc_mode&qvalue=$qvalue"
	. ($rc_maxbitrate ne "cam-defined" ? "&maxbitrate=$rc_maxbitrate" : "")
	. ($rc_targetbitrate ne "cam-defined" ? "&bitrate=$rc_targetbitrate" : "")
	. ($pcount ne "cam-defined" ? "&pcount=$pcount" : "");
}

$respond=ProbeRequest($update_codec_params_url);
if ($respond =~ /error/i)
{
	$err++;
	$postmortem .= "Could not set codec bitrate: $respond\n";
}

$respond = `$APL/analytics/bin/nvcprog $conf->{DEVID} 2>/dev/null`;
if($respond =~ /Status=FAIL/i) {
	$err++;
	$postmortem .= "Fail to reset encoder\n";
}

ProbeErr("PCE-0040","Cannot update video settings",$postmortem) if $err;
$result{RC_SET}="OK";
#------------------------------------------------------------------------------
# set AudioParams
#------------------------------------------------------------------------------
if ( $conf->{AUDIO} eq 'on' ) {
# no code for Videonext
}
# final report-----------------------------------------------------------------------
ProbeResult(\%result);

