capture_range.php 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/php
<?php
/*!*******************************************************************************
*! FILE NAME  : capture_range.php
*! DESCRIPTION: Return frame number when the specified timestamp whil be reached 
*               or expected timestamp for the specified frame.
*               Useful to synchronize multiple camera triggered by one of them
*               when the master timestamp is sent to each camera. Normally
*               used with just the master sensor port.
*               
*! Copyright (C) 2021 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!
*!  This program is free software: you can redistribute it and/or modify
*!  it under the terms of the GNU General Public License as published by
*!  the Free Software Foundation, either version 3 of the License, or
*!  (at your option) any later version.
*!
*!  This program is distributed in the hope that it will be useful,
*!  but WITHOUT ANY WARRANTY; without even the implied warranty of
*!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*!  GNU General Public License for more details.
*!
*!  You should have received a copy of the GNU General Public License
*!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*!
*/
29
   define('CAPTURE_RANGE_STATE',        '/var/state/capture_range.state');
Andrey Filippov's avatar
Andrey Filippov committed
30 31 32
   set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
   include 'show_source_include.php';
   include "elphel_functions_include.php"; // includes curl functions
33 34
   $minahead = 2;
   $PARS_FRAMES = 16;
Andrey Filippov's avatar
Andrey Filippov committed
35
   $maxahead = $PARS_FRAMES - 4; // 3;
36 37 38 39 40 41 42 43 44
   $flags = 0;
   
   if (!($_SERVER ['REQUEST_METHOD'] == "GET")){
       //Maybe re-write this in C or as a PHP function? 
       // CLI mode will be spawned bu the CGI and run in a background after request will be over
       $sensor_port = $argv[1]; // $argv[0] - script name
       $port_mask =   $argv[2];
       $frame =       $argv[3]; // absolute frame to start compressor
       $duration =    $argv[4]; // number of frames to run compressor
45
       $notify =      $argv[5]; // double-quoted URL 
46 47 48 49 50 51 52 53 54
       
       if ($frame > 0){
           $frame +=1; // seem there is a bug - actual compressed frames numbers (and last compressed frame number) are 1 less than expected
       }
       // If 'duration'==0 - just stop compressor, if 'duration' <0 - only start, if duration >0 - start+stop
       
//       $f = fopen ( "/var/log/capture_range.log", 'a' );
//       fwrite($f,"elphel_capture_range($sensor_port, $port_mask, $frame, $duration);\n");
//       fclose ( $f );
Andrey Filippov's avatar
Andrey Filippov committed
55 56 57 58 59 60 61

       /**
        * Program multiple channels (according to 'port_mask') to start compressor at absolute (or relative) 'frame', run it for 'duration' frames
        * and then stop. If 'duration'==0 - just stop compressor, if 'duration' <0 - only start.
        * This function is intended to be executed in a background process (such as in capture_range.php) not to hold http client
        * BUG: frame number after stop is 2 less than expected (ahead applied in wrong place?)
        */
62 63 64
       elphel_capture_range($sensor_port, $port_mask, $frame, $duration);
//       $f = fopen ( "/var/log/capture_range.log", 'a' );
//       fwrite($f,"elphel_capture_range DONE\n");
65 66 67
       if ($notify){ // complete url with http://
           file_get_contents($notify);
       }
68
//       fclose ( $f );
69 70 71 72
       // can still get stuck for a long time, but testing file exists is faster that find a process
       if (file_exists(CAPTURE_RANGE_STATE)) {
           unlink(CAPTURE_RANGE_STATE);
       }
73 74 75 76 77 78
       exit(0);
   }
   $retval=-1; // not used
   
   if (count($_GET) < 1) {
       print ("CGI mode");
79 80 81
       // Be carefull watching that USAGE is not indented (after auto-indenting). That leads to errors such as (at much later line number)!
       // Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE)
       // or number (T_NUM_STRING) in /www/pages/capture_range.php on line 143
82
       echo <<<USAGE
83 84 85 86 87
<p>This script returns the timestamp for specified frame (frame=?) or expected frame number
  when the specified timestamp (timestamp = ?.?) will be reached. If none of the frame number
  and timestamp are specified - return last compressed frame number and timestamp.</p>
<p>Uses TRIG_PERIOD (in 10ns increments) for calculations.</p>
<p>sensor_port=0..3 - specify which sensor port to use, default is sensor_port=0</p>
88 89 90 91 92 93 94 95
USAGE;
      exit (0);
    }
    $sensor_port=0;
    $frame = 0;
    $timestamp = 0.0;
    $PARS_FRAMES = 16;
    $minahead = 2;
96 97
    $notify_args=array();
    $notify_url="";
98 99 100 101 102 103 104
//    $port_mask = 15; // not set - same as ts2frame.php
//    $duration =  1; // not set - end, set - start 
//    $wait = false;
    $extra = 2; // wait extra frames after stop to be sure compressor is flashed?
    foreach($_GET as $key=>$value) {
        if ($key == 'sensor_port'){
            $sensor_port = (integer) $value;
105 106
        } else if (($key == 'state')) {
            $read_state = 1; // all other parameters will not be used, maybe in the future - use multi request and combine responses?
107 108
        } else if (($key == 'a')  || ($key == 'ahead')){ // will overwrite timestamp with this value (in seconds) from now
            $ahead_now = (double) $value;
109 110 111 112 113
        } else if (($key == 'ts')  || ($key == 'timestamp')){
            $timestamp = (double) $value;
        } else if (($key == 'f')   || ($key == 'frame')){
            $frame = (integer) $value;
        } else if (($key == 'm')   || ($key == 'port_mask')){
114 115
//            $port_mask = (integer) $value;
            $masklist = $value;
116
        } else if (($key == 'd')   || ($key == 'duration')){
117 118 119 120
// TODO: make durations optionally a list, same as port mask (for EO - different)            
 // wait - apply to the first IP in a list? or to the last?           
//            $duration = (integer) $value;
            $duration_list = $value;
121 122 123 124 125 126
        } else if (($key == 'mxa') || ($key == 'maxahead')){
            $maxahead = (integer) $value;
        } else if (($key == 'mna') || ($key == 'minahead')){
            $minahead = (integer) $value;
        } else if (($key == 'w')   || ($key == 'wait')){ // wait all done
            $wait = true;
127 128 129 130
        } else if (($key == 'e')   || ($key == 'extra')){
            $extra = (integer) $value; // not used?
        } else if (($key == 'ip') || ($key == 'ips')){ //  multicamera operation
            $ips = explode(',',$value);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        } else if (substr($key,0,7) == "notify_") {
            $nk = substr($key,7);
            if ($nk == 'cmd'){
                $notify_cmd=$value;
            } else if (substr($nk,0,3) == 'key') {
                $ni = intval(substr($nk,3));
                if (!isset($notify_args[$ni])) $notify_args[$ni]=array();
                $notify_args[$ni][0] = $value;
            } else if (substr($nk,0,3) == 'val') {
                $ni = intval(substr($nk,3));
                if (!isset($notify_args[$ni])) $notify_args[$ni]=array();
                $notify_args[$ni][1] = $value;
            }
        }
    }
    //capture_range.php?sensor_port=0&state&frame=20 # will respond and block if there are fewer than 20 frames
    if (isset($read_state)) { // if $frame >0 wait if less frames are left
        $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><capture_range/>");
        // does the state file exist?
        $this_frame = 0;
        $left =       0;
        if (file_exists(CAPTURE_RANGE_STATE)) {
           $ini =  parse_ini_file(CAPTURE_RANGE_STATE);
           $end_frame = intval($ini['end_frame']); 
           $this_frame=elphel_get_frame($sensor_port);
           $left = $end_frame - $this_frame;
           if (($frame > 0) && ($left > 0) && ($left < $frame)){
               elphel_wait_frame_abs($sensor_port, $end_frame); // wait and block client
           }
           $this_frame=elphel_get_frame($sensor_port);
           $left = $end_frame - $this_frame;
        }
        $xml->addChild ('frame',$this_frame);
        $xml->addChild ('end_frame',$end_frame);
        $xml->addChild ('left',$left);
        $rslt=$xml->asXML();
        header("Content-Type: text/xml");
        header("Content-Length: ".strlen($rslt)."\n");
        header("Pragma: no-cache\n");
        printf($rslt);
        exit(0);
    }
    if (isset($notify_cmd)){ 
        $wait = false;
        $notify_url='http://'.$notify_cmd; // $notify_cmd includes full url
        $i=0;
        foreach ($notify_args as $kv) {
            if (isset($kv[0])) {
                $notify_url .= ($i ? '&' : '?');
//                $notify_url .= ($i ? '&amp;' : '?');
                $notify_url .= $kv[0];
                if (isset($kv[1])) {
                    $notify_url .= '=' . $kv[1];
                }
                $i ++;
            }
187 188
        }
    }
189 190
    
    if (isset($ahead_now)){  
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        $this_frame=elphel_get_frame($sensor_port);
        $this_timestamp=elphel_frame2ts($sensor_port,$this_frame);
        $timestamp = $this_timestamp + $ahead_now;
    }
    // if ips are provided, treat port mask as commaseparated list
    //            $port_mask = (integer) $value;
    if (isset($ips)){
        $pml = explode(',', $masklist);
        $port_mask = array ();
        for ($i = 0; $i<count($ips); $i++){
            if ($i < count($pml)){
                $port_mask[] = (integer) $pml[$i];
            } else {
                $port_mask[] = 15; // default - all sensors
            }
        }
        $durl = explode(',', $duration_list);
        $duration = array();
        for ($i = 0; $i<count($ips); $i++){
            if ($i < count($durl)){
                $duration[] = (integer) $durl[$i];
            } else {
                $duration[] = $duration[0]; // at least one duration should be provided
            }
        }
    } else {
        $port_mask = (integer) $masklist;      // single valude
        $duration =  (integer) $duration_list; // single valude
    }
    
Andrey Filippov's avatar
Andrey Filippov committed
221
    // convert provided timestamp to integer number of frame timestamp
222 223 224 225 226 227 228
    if (($frame !=0) || ($timestamp !=0.0)) {
        if (($frame <=0) && ($timestamp > 0.0)){
            $frame = elphel_ts2frame($sensor_port,$timestamp);
        }
        $timestamp = elphel_frame2ts($sensor_port,$frame); // update, even if provided to fit better integer number of frames
    }
    
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    if (isset($ips)){ // start parallel requests to all cameras ($ips should include this one too), collect responses and exit
        // prepare URLs
        $urls = array();
        for ($i = 0; $i<count($ips); $i++){
            // $_SERVER[SCRIPT_NAME] STARTS WITH '/'
            $url = 'http://'.$ips[$i].$_SERVER[SCRIPT_NAME].'?sensor_port='.$sensor_port; //
            $url .= '&ts='.$timestamp; // &timestamp" -> ×tamp
            $url .= '&port_mask='.$port_mask[$i];
            $url .= '&duration='. $duration[$i];
            $url .= '&maxahead='. $maxahead;
            $url .= '&minahead='. $minahead;
            $url .= '&extra='.    $extra;
            if ($wait && ($i == (count($ips) - 1))){ // addd to the last ip in a list
                $url .= '&wait';
            }
            $urls[] = $url;
        }
//        print_r($urls);
//        exit(0);
        $curl_data = curl_multi_start ($urls);
        $enable_echo = false;
        $results =  curl_multi_finish($curl_data, true, 0, $enable_echo); // Switch true -> false if errors are reported (other output damaged XML)
        $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><capture_range/>");
        for ($i = 0; $i<count($ips); $i++){
            $xml_ip = $xml->addChild ('ip_'.$ips[$i]);
            foreach ($results[$i] as $key=>$value){
                $xml_ip->addChild($key,$value);
            }
        }
        $rslt=$xml->asXML();
        header("Content-Type: text/xml");
        header("Content-Length: ".strlen($rslt)."\n");
        header("Pragma: no-cache\n");
        printf($rslt);
        exit(0);
    }
    
    
267
    $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><capture_range/>");
268 269 270
        $flags = 0;
    $use_daemon = ($frame != 0);
    if (isset($port_mask)) { // start or stop compressor
271
        $this_frame = elphel_get_frame($sensor_port);
272
        if (! $use_daemon) { // e.g. to stop ASAP $frame=0, $duration !=0 - start ASAP
Andrey Filippov's avatar
Andrey Filippov committed
273
            if ($duration < 0) {
274
                elphel_compressor_run($sensor_port, 0, $flags, $port_mask); // turn on ASAP
275 276 277 278
                $xml->addChild('compressor', 1);
            } else { // frame = 0; duration ==0 - stop ASAP
                elphel_compressor_stop($sensor_port, 0, $flags, $port_mask); // turn off ASAP
                $xml->addChild('compressor', 0);
279 280
            }
            $frame = $this_frame + $minahead;
Andrey Filippov's avatar
Andrey Filippov committed
281
        } 
282
        $ahead = $frame - $this_frame;
Andrey Filippov's avatar
Andrey Filippov committed
283
        if ($use_daemon && ($ahead < $minahead) && (!isset($ahead_now))) {
284 285
            $xml->addChild('error', 'TOO LATE');
            $xml->addChild('ahead', $ahead);
Andrey Filippov's avatar
Andrey Filippov committed
286
        } else { // OK, enough time to program or no-daemon mode, or ASAP
287
            if (! isset($duration)) {
288 289
                $duration = 0;
            }
290 291 292 293 294 295
            // kill CLI mode if it was running (e.g. waiting for 100 years) first check file exists (faster)
//            if (file_exists(CAPTURE_RANGE_STATE) || getPIDByName('capture_range.php', 'php', $active_only = false)) {
            if (file_exists(CAPTURE_RANGE_STATE)) { // getPIDByName('capture_range.php', 'php', $active_only = false) is slower
               exec("killall capture_range.php", $output, $retval); // grows lighttpd_stderr.log, need to check before kill
               unlink(CAPTURE_RANGE_STATE);
            }
296
            // spawn CLI program in background duration <0 - start only, ==0 - stop only, >0 - start+stop
297 298 299 300 301 302 303
            // $f = fopen ( "/var/log/capture_range.log", 'a' );
            // fwrite($f,"exec(/www/pages/capture_range.php $sensor_port $port_mask $frame $duration > /dev/null 2>&1 &\n");
            // fclose ( $f );
            // if ($notify_url) {
            // $xml->addChild ('notify_url', $notify_url); // urlencode($notify_url)); // dry run, do nothing
            // } else {
            if ($use_daemon) {
Andrey Filippov's avatar
Andrey Filippov committed
304 305 306
                if (isset($ahead_now) && ($ahead_now == 0)){
                    $frame = 0; // ASAP
                }
307 308 309 310 311
                $end_frame = $frame;
                if ($duration > 0) {
                    $end_frame += $duration; // xml will contain end frame
                }
                file_put_contents(CAPTURE_RANGE_STATE, "end_frame = $end_frame\n");
312 313 314 315 316 317 318 319 320 321 322 323
                exec("/www/pages/capture_range.php $sensor_port $port_mask $frame $duration '$notify_url'> /dev/null 2>&1 &");
                // }
                // $f = fopen ( "/var/log/capture_range.log", 'a' );
                // fwrite($f,"DONE exec\n");
                // fclose ( $f );
                if ($wait) {
                    elphel_wait_frame_abs($sensor_port, $frame + 1);
                }
            } else {
                if (file_exists(CAPTURE_RANGE_STATE)) {
                    unlink(CAPTURE_RANGE_STATE);
                }
324 325 326
            }
        }
    } else { // just ts2frame.php mode
327 328 329
        if ($frame == 0) {
            $timestamp = elphel_frame2ts($sensor_port, 0);
            $frame = elphel_ts2frame($sensor_port, 0.0);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        }
    }
    $xml->addChild ('retval', $retval);
    $xml->addChild ('frame',$frame);
    $xml->addChild ('timestamp',$timestamp);
    $this_frame=elphel_get_frame($sensor_port);
    $xml->addChild ('this_frame',$this_frame);
    $xml->addChild ('this_timestamp',elphel_frame2ts($sensor_port,$this_frame));
//    $xml->addChild ('frame',$frame);
//    $xml->addChild ('timestamp',$timestamp);
    
    $rslt=$xml->asXML();
    header("Content-Type: text/xml");
    header("Content-Length: ".strlen($rslt)."\n");
    header("Pragma: no-cache\n");
    printf($rslt);
?>