#!/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=FAST  
#    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=mjpg,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
#       STATUS=WARNING: PCW-0009 [101] PROBE=RESET is not supported
#       
# -----------------------------------------------------------------------------
#  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: teetov, 03/22/10
#  Edited by:
#  QA by:
#  Copyright: videoNEXT Network Solutions, Inc, 2010
# -----------------------------------------------------------------------------
#
use strict;
use Device::Conf ":all";
use XML::Simple;
use XML::LibXML;
use Data::Dumper;

# cons -------------------------------------------------------------------------
# no code for Cisco

# --------- check required parameters: DEVID,USRNAME,PASSWD,
my $conf=ProbeInit(20);  # uses <BRAND> from path and ARGV
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};
ProbeWarn("PCW-0009","PROBE=RESET is not supported") if $conf->{PROBE} eq 'RESET';
#------------------------------------------------------------------------------
# Probe camera MODELID & FIRMWARE 
#------------------------------------------------------------------------------
my %result=(MODELID=>'0',FIRMWARE=>'0.0',STATUS=>'OK',RC_SET=>'NONE',AUDIO_SET=>'NONE');

my $rsp = ProbeRequest("/adm/file.cgi?next_file=status_system.htm&random_num=726201015195");
if ($rsp !~ /^HTTP ERROR/) {
    $result{MODELID} = $1 if $rsp =~ /Product Identifier:<\/td>.*?<td nowrap> (.*?)<\/td>/is;
    chomp($result{MODELID});
    $result{FIRMWARE} = $1 if $rsp =~ /Firmware Version:<\/td>.*?<td nowrap> (.*?)<\/td>/is;
} else {
    my ($err) = $rsp =~ /^HTTP ERROR \[(\d+)\]/;
    ProbeErr("PCE-0500","Device does not respond",$rsp) if $err=~/^5\d\d/;
    ProbeErr("PCE-0401","Authorization error",    $rsp) if $err==401;
}

#-- throw out result and exit if it has old firmware 1.*
ProbeResult(\%result) if ($result{FIRMWARE} =~ /^1.*/);
#print "Predetect firmware is $result{FIRMWARE}\n";


#-- Suppose firmware 2.1, Probe camera MODELID & FIRMWARE 
$rsp=ProbeSession('/System.xml','POST','',"version=1.0&action=login&userName=$conf->{USRNAME}&password=$conf->{PASSWD}");
ProbeErr('PCE-0600','Can not get sessionID') unless $rsp;

my $session = $rsp;
$rsp = ProbeRequest("/DeviceBasicInfo.xml?version=1.0&sessionID=$session&action=get");
ProbeErr('PCE-0500','Device respond error',$rsp) if $rsp =~ /^HTTP ERROR \[(\d+)\]/;

my $xml = XMLin($rsp);
$result{MODELID} = $xml->{DeviceBasicInfo}->{model};
$result{FIRMWARE} = $xml->{DeviceBasicInfo}->{firmwareVersion};
ProbeErr("PCE-0030","Cannot get MODELID or FIRMWARE") if ! $result{MODELID};

# 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') {
   if ($result{MODELID} ne 'CIVS-IPC-4300' and $result{MODELID} ne 'CIVS-IPC-6020') {
       $result{SNAPSHOT}=ProbeSamplePicture();
   }
}

my $parser = XML::LibXML->new();
my %codecRef  = (1=>'mpeg4', 2=>'mjpg', 3=>'3gp', 4=>'h264', 5=>'mpng');
my %statusCodes = (
    '1' => 'OK',
    '2' => 'Device Busy',
    '3' => 'Device Error',
    '4' => 'Invalid Operation',
    '5' => 'Invalid XML Format',
    '6' => 'Invalid XML Content'
);

#------------------------------------------------------------------------------
# interim report & exit 
#------------------------------------------------------------------------------
my ( $doc, @entries ) = GetEntries();
if (($conf->{PROBE} =~ /^(DEFINE|FAST)$/) or ($conf->{DEVID}==0)) {
    $rsp = ProbeRequest("/System.xml?version=1.0&sessionID=$session&action=logout");
    if ($result{MODELID} eq 'CIVS-IPC-4300') {
        my %channels = map {
            ($_->getElementsByTagName('channelID'))[0]->firstChild->nodeValue,
            ($_->getElementsByTagName('channelName'))[0]->firstChild->nodeValue,
        } @entries;
        $result{CAMERA_LIST} = EscapeList( %channels );
        $result{SNAPSHOT_LIST} = ProbeSnapshotList( keys %channels );
    }
    ProbeResult(\%result);
}

#print "MEDIA_FORMAT=$conf->{MEDIA_FORMAT}\n";
#print "IMAGESIZE=$conf->{IMAGESIZE}\n";
#print "RC_MODE=$conf->{RC_MODE}\n";
#print "CAMCOMPRESSION=$conf->{CAMCOMPRESSION}\n";
#print "RC_TARGETBITRATE=$conf->{RC_TARGETBITRATE}\n";
#print "FRAMERATE=$conf->{FRAMERATE}\n";
#print "AUDIO=$conf->{AUDIO}\n";
#print "AUDIO_TWO_WAY=$conf->{AUDIO_TWO_WAY}\n";

configure_IPC_6020() if $conf->{MODELID} eq 'CIVS-IPC-6020';

my $done = 0;

#--- loop until done
until ($done) {
    ( $doc, @entries ) = GetEntries();
    my $action = 'set';

    if( $conf->{MODELID} eq 'CIVS-IPC-4300' ) {
        for( my $i = 0; $i < scalar @entries; $i++ ) {
            my $chID = ($entries[$i]->getElementsByTagName('channelID'))[0]->firstChild->nodeValue;
            #my $chName = ($entries[$i]->getElementsByTagName('channelName'))[0]->firstChild->nodeValue;
            #my $codec = $codecRef{($entries[$i]->getElementsByTagName('videoCodecType'))[0]->firstChild->nodeValue};
            if( $chID == $conf->{CAMERA} ) {
                my $entry = $entries[ $i ];

                my $chID = ($entry->getElementsByTagName('channelID'))[0]->firstChild->nodeValue;
                my $chName = ($entry->getElementsByTagName('channelName'))[0]->firstChild->nodeValue;
                my $codec = $codecRef{($entry->getElementsByTagName('videoCodecType'))[0]->firstChild->nodeValue};
                my $el;
                my $tbr = (defined($conf->{RC_TARGETBITRATE}) and $conf->{RC_TARGETBITRATE} ne "cam-defined") ? $conf->{RC_TARGETBITRATE} : '512';
                
                $result{CHANNELID} = $chID;
                $result{CHANNELNAME} = $chName;
                $result{SESSIONID} = $session;


                if ($codec eq $conf->{MEDIA_FORMAT}) {
                    #print "keep current codec $conf->{MEDIA_FORMAT}\n";
                } else {
                    #print "switch to codec $conf->{MEDIA_FORMAT}\n";
                }

                # no MEDIA_FORMAT changes
                $result{MEDIA_FORMAT} = $codecRef{($entry->getElementsByTagName('videoCodecType'))[0]->firstChild->nodeValue};
                $result{MEDIA_FORMAT_LIST} = $result{MEDIA_FORMAT};

                if ($result{MEDIA_FORMAT} eq 'h264') {
                    $el = ($entry->getElementsByTagName('videoQualityControlType'))[0];
                    $el->removeChild($el->firstChild);
                    if ($conf->{RC_MODE} eq 'vbr') {
                        $el->appendTextNode('2');
                    } else {
                        $el->appendTextNode('1');
                        $el = ($entry->getElementsByTagName('constantBitRate'))[0];
                        if (defined($el)) {
                            $el->removeChild($el->firstChild);
                            $el->appendTextNode($tbr);
                        } else {
                            my $node = XML::LibXML::Element->new('constantBitRate');
                            $node->appendTextNode($tbr);
                            $el = ($entry->getElementsByTagName('VideoSetting'))[0];
                            $el->appendChild($node);
                        }
                    }
                }#-- h264
                $el = ($entry->getElementsByTagName('fixedQuality'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode($conf->{CAMCOMPRESSION});
                $el = ($entry->getElementsByTagName('maxFrameRate'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode($conf->{FRAMERATE});

                my ($width,$height) = ($1,$2) if ($conf->{IMAGESIZE} =~ /^(\d+)x(\d+)$/);

                if (defined $width) {
                    $el = ($entry->getElementsByTagName('videoResolutionWidth'))[0];
                    $el->removeChild($el->firstChild);
                    $el->appendTextNode($width);
                }
                if (defined $height) {
                    $el = ($entry->getElementsByTagName('videoResolutionHeight'))[0];
                    $el->removeChild($el->firstChild);
                    $el->appendTextNode($height);
                }

                if( $chID < 2 ) {
                    my $as = ($entry->getChildrenByTagName('AudioSetting'))[0];
                    if ($conf->{AUDIO} eq 'on') {
                        $el = ($as->getChildrenByTagName('enabled'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('1');
                        $el = ($as->getChildrenByTagName('audioCompressionType'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('3');
                        $el = ($as->getChildrenByTagName('audioInboundCompressionType'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('3');
                        $el = ($as->getChildrenByTagName('audioResolution'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('8');
                        $el = ($as->getChildrenByTagName('audioSamplingRate'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('8000');
                    } else {
                        $el = ($as->getChildrenByTagName('enabled'))[0]; 
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode('0');
                    }
                }
            }
        }
        $done = 1;
    } else {
        if (defined $entries[1] && $conf->{MODELID} ne 'CIVS-IPC-4300') { #--- remove one channel if where are two
            my $req = $parser->parse_string('<?xml version="1.0" encoding="UTF-8"?><DeviceInfo><version>0.0.1</version><StreamingSetting><StreamingChannelList><StreamingChannelEntry><channelID></channelID><channelName></channelName></StreamingChannelEntry></StreamingChannelList></StreamingSetting></DeviceInfo>');
            my $chID = ($entries[1]->getElementsByTagName('channelID'))[0]->firstChild->nodeValue;
            my $chName = ($entries[1]->getElementsByTagName('channelName'))[0]->firstChild->nodeValue;
            my $el = ($req->getElementsByTagName('channelID'))[0];
            $el->appendTextNode($chID);
            $el = ($req->getElementsByTagName('channelName'))[0];
            $el->appendTextNode($chName);
            #print "remove chID{".$chID."} chName{".$chName."}\n";
            $doc = $req;
            $action = 'delete';
        } else { 			#--- setup single channel
            my $chID = ($entries[0]->getElementsByTagName('channelID'))[0]->firstChild->nodeValue;
            my $chName = ($entries[0]->getElementsByTagName('channelName'))[0]->firstChild->nodeValue;
            my $codec = $codecRef{($entries[0]->getElementsByTagName('videoCodecType'))[0]->firstChild->nodeValue};
            my $el;
            my $tbr = (defined($conf->{RC_TARGETBITRATE}) and $conf->{RC_TARGETBITRATE} ne "cam-defined") ? $conf->{RC_TARGETBITRATE} : '512';
            
            $result{CHANNELID} = $chID;
            $result{CHANNELNAME} = $chName;
            $result{SESSIONID} = $session;


            if ($codec eq $conf->{MEDIA_FORMAT}) {
                #print "keep current codec $conf->{MEDIA_FORMAT}\n";
            } else {
                #print "switch to codec $conf->{MEDIA_FORMAT}\n";
            }
            
            if ($conf->{MEDIA_FORMAT} eq 'mjpg') {
                $el = ($entries[0]->getElementsByTagName('videoCodecType'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('2');
                $el = ($entries[0]->getElementsByTagName('videoQualityControlType'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('2');	 
            } else { #-- mpeg4 and h264
                $el = ($entries[0]->getElementsByTagName('videoCodecType'))[0];
                $el->removeChild($el->firstChild);
                if ($conf->{MEDIA_FORMAT} eq 'h264') {
                    $el->appendTextNode('4');
                }
                else {
                    $el->appendTextNode('1');
                }
                $el = ($entries[0]->getElementsByTagName('videoQualityControlType'))[0];
                $el->removeChild($el->firstChild);
                if ($conf->{RC_MODE} eq 'vbr') {
                    $el->appendTextNode('2');
                } else {
                    $el->appendTextNode('1');
                    $el = ($entries[0]->getElementsByTagName('constantBitRate'))[0];
                    if (defined($el)) {
                        $el->removeChild($el->firstChild);
                        $el->appendTextNode($tbr);
                    } else {
                        my $node = XML::LibXML::Element->new('constantBitRate');
                        $node->appendTextNode($tbr);
                        $el = ($entries[0]->getElementsByTagName('VideoSetting'))[0];
                        $el->appendChild($node);
                    }
                }
            }#-- mpeg4
            $el = ($entries[0]->getElementsByTagName('fixedQuality'))[0];
            $el->removeChild($el->firstChild);
            $el->appendTextNode($conf->{CAMCOMPRESSION});
            $el = ($entries[0]->getElementsByTagName('maxFrameRate'))[0];
            $el->removeChild($el->firstChild);
            $el->appendTextNode($conf->{FRAMERATE});

            my ($width,$height) = ($1,$2) if ($conf->{IMAGESIZE} =~ /^(\d+)x(\d+)$/);

            if (defined $width) {
                $el = ($entries[0]->getElementsByTagName('videoResolutionWidth'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode($width);
            }
            if (defined $height) {
                $el = ($entries[0]->getElementsByTagName('videoResolutionHeight'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode($height);
            }

            my $as = ($entries[0]->getChildrenByTagName('AudioSetting'))[0];
            if ($conf->{AUDIO} eq 'on') {
                $el = ($as->getChildrenByTagName('enabled'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('1');
                $el = ($as->getChildrenByTagName('audioCompressionType'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('3');
                $el = ($as->getChildrenByTagName('audioInboundCompressionType'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('3');
                $el = ($as->getChildrenByTagName('audioResolution'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('8');
                $el = ($as->getChildrenByTagName('audioSamplingRate'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('8000');
            } else {
                $el = ($as->getChildrenByTagName('enabled'))[0];
                $el->removeChild($el->firstChild);
                $el->appendTextNode('0');
            }

            $done = 1;
        }#-- setup single channel
    }
    #print "Request action=$action ->\n".$doc->toString."\n";
    $rsp = ProbeRequest("/StreamingSetting.xml?version=1.0&sessionID=$session&action=$action",'POST',['Content-Type'=>'application/xml'],$doc->toString);
    ProbeErr('PCE-0500','Device respond error',$rsp) if $rsp =~ /^HTTP ERROR \[(\d+)\]/;
    #print "Response action=$action ->\n".$rsp."\n";
    sleep 1;
}#-- until done
$result{RC_SET} = 'YES';
$result{AUDIO_SET} = 'YES';

if( $conf->{MODELID} eq 'CIVS-IPC-4300' ) {
    # Use direct DB call instead of PHP API (unless got trusted IP support)
    require SKM::DB;
    SKM::DB->import();
    my $dbm;
    eval {
    $dbm = DBMaster({PrintError=>0,RaiseError=>1,AutoCommit=>0});
    $dbm->do("update _obj_attr set val=? where obj=? and attr='MEDIA_FORMAT'",
        undef,$result{MEDIA_FORMAT},$conf->{DEVID}
        );
    #     $dbm->do("update _obj_attr set val=? where obj=? and attr='POSITIONCTL'",
    #         undef,$positionctl,$conf->{DEVID}
    #     );
        $dbm->do("update _objs set rtime=null where obj=?",undef,$conf->{DEVID});
        $dbm->commit;
    };
    if ($@) {
        $result{RC_SET} = "ERROR\n#DB error";
        eval { $dbm->rollback } if $dbm;
    }
    else {
        $result{RC_SET} = "OK";
    }
    eval { $dbm->disconnect } if $dbm;
}

#--- final report -------------------------------------------------------------
#$rsp = ProbeRequest("/System.xml?version=1.0&sessionID=$session&action=logout");
ProbeResult(\%result);

sub GetEntries {
    my $rsp = ProbeRequest("/StreamingSetting.xml?version=1.0&sessionID=$session&action=get");
    ProbeErr('PCE-0500','Device respond error',$rsp) if $rsp =~ /^HTTP ERROR \[(\d+)\]/;
    #print "Response on action=get ->\n$rsp\n";

    my $doc = $parser->parse_string($rsp);
    return ($doc, $doc->getElementsByTagName('StreamingChannelEntry'));
}

sub getVal {
    my ($parent, $tagName) = @_;
    my @el = $parent->getElementsByTagName($tagName);
    return undef if not @el;
    return $el[0]->firstChild->nodeValue;
}

sub setVal {
    my ($parent, $tagName, $val, $parentTagName) = @_;
    my $el = ($parent->getElementsByTagName($tagName))[0];
    
    if (defined($el)) {
        $el->removeChild($el->firstChild);
        $el->appendTextNode($val);
    } elsif (defined $parentTagName) {
        my $node = XML::LibXML::Element->new($tagName);
        $node->appendTextNode($val);
        $el = ($parent->getElementsByTagName($parentTagName))[0];
        $el->appendChild($node);
    }
}

# Dedicated procedure for configuring Cisco CIVS-IPC-6020
sub configure_IPC_6020 {
    ($doc, @entries) = GetEntries;
    my $entry;
    my ($chid, $chname, $codec);
    # Find channel that corresponds to our camera
    for( my $i = 0; $i < scalar @entries; $i++ ) {
        $chid = getVal($entries[$i], 'channelID');
        if( $chid == $conf->{CAMERA} ) {
            $entry = $entries[$i];
            last;
        }
    }
    ProbeErr('PCE-0500',"Cannot find channel for CAMERA=$conf->{CAMERA}") if not $entry;
    $chname = getVal($entry, 'channelName');
    
    # Setup codec
    foreach (keys %codecRef) {
        $codec = $_,last if $codecRef{$_} eq $conf->{MEDIA_FORMAT};
    }
    setVal($entry, 'videoCodecType', $codec);
    
    $result{CHANNELID} = $chid;
    $result{CHANNELNAME} = $chname;
    $result{SESSIONID} = $session;
    
    # Enable our channel and disable others
    
    # Setup bitrate
    # For h264 both variable and constant bitrate available
    # For mjpg only variable bitrate can be set
    # If variable bitrate set <fixedQuality> parameter should be specified. It is a percentage 
    # value that indicates the average bitrate between <vbrLowerCap> and <vbrUpperCap> 
    
    if ($conf->{MEDIA_FORMAT} eq 'h264') {
        setVal($entry, 'videoQualityControlType', $conf->{RC_MODE} eq 'vbr' ? '2' : '1');
        setVal($entry, 'constantBitRate', $conf->{RC_TARGETBITRATE}, 'VideoSetting') 
            if $conf->{RC_MODE} eq 'cbr' and $conf->{RC_TARGETBITRATE} ne 'cam-defined';
        setVal($entry, 'vbrVerifyCap', '0', 'VideoSetting'); 
    }
    else {
        setVal($entry, 'videoQualityControlType', '2');
    }
    setVal($entry, 'fixedQuality', $conf->{CAMCOMPRESSION});
    # Set framerate
    setVal($entry, 'maxFrameRate', $conf->{FRAMERATE});
    # Set resolution
    my ($width, $height) = ($1, $2) if $conf->{IMAGESIZE} =~ /^(\d+)x(\d+)$/;
    setVal($entry, 'videoResolutionWidth', $width);
    setVal($entry, 'videoResolutionHeight', $height);
    # Set audio
    my $as = ($entry->getChildrenByTagName('AudioSetting'))[0];
    setVal($as, 'enabled', $conf->{AUDIO} eq 'on' ? '1' : '0');
    setVal($as, 'audioCompressionType', '2');
    setVal($as, 'audioInboundCompressionType', '7');
    setVal($as, 'audioSamplingRate', '8');
    setVal($as, 'audioResolution', '8');
    
    # Send video settings to the camera
    #print "Request action=set ->\n".$doc->toString."\n";
    $rsp = ProbeRequest("/StreamingSetting.xml?version=1.0&sessionID=$session&action=set",'POST',['Content-Type'=>'application/xml'],$doc->toString);
    ProbeErr('PCE-0500','Device respond error',$rsp) if $rsp =~ /^HTTP ERROR \[(\d+)\]/;
    #print "Response action=set ->\n".$rsp."\n";
    my $rsp_parsed = $parser->parse_string($rsp);
    my ($statusCode, $statusString) = (getVal($rsp_parsed, 'statusCode'), getVal($rsp_parsed, 'statusString'));
    if ($statusCode ne '1') {
        my $msg = exists $statusCodes{$statusCode} ? $statusCodes{$statusCode} : 'Unknown error';
        ProbeErr('PCE-0500',$msg, $rsp);
    }
    sleep 1;
    $result{RC_SET} = 'YES';
    $result{AUDIO_SET} = 'YES';
    
    ProbeResult(\%result);
}
