--
--	fastvid.occ -- video streaming test for occam-pi
--	Copyright (C) 2008  Carl Ritson <cgr@kent.ac.uk>
--
--	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 2 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, write to the Free Software
--	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
--

#INCLUDE "course.module"
#INCLUDE "sdlraster.module"
#INCLUDE "video.module"

VAL []BYTE DEVICE.NAME IS "/dev/video0":


PROC fastvid.main (CT.VIDEO! dev, CHAN BYTE kyb?, scr!)
  VIDEO.FRAMEINFO finfo:
  SEQ
    --{{{  frame info
    out.string ("fastvid: getting frame info...*n", 0, scr!)

    dev[req] ! get.frameinfo
    dev[resp] ? CASE
      frameinfo; finfo
        SEQ
          out.string ("fastvid: frames are ", 0, scr!)
          out.int (finfo[width], 0, scr!)
          out.string ("x", 0, scr!)
          out.int (finfo[height], 0, scr!)
          out.string (", palette ", 0, scr!)
          out.int (finfo[format], 0, scr!)
          VAL INT pal IS INT finfo[format]:
          IF
            (pal >= 0) AND (pal < (SIZE VIDEO.PAL.NAMES))
              SEQ
                out.string (" ", 0, scr!)
                out.string (VIDEO.PAL.NAMES[pal], 0, scr!)
            TRUE
              SKIP
          out.string ("*n", 0, scr!)
      MOBILE []BYTE msg:
      error; msg
        SEQ
          out.string ("fastvid: failed to get frame info: ", 0, scr!)
          out.string (msg, 0, scr!)
          out.string ("*n", 0, scr!)
    --}}}
    #PRAGMA DEFINED finfo
    CHAN RASTER rc1, rc2:
    PAR
      --{{{  create raster
      raster.display.simple ("video grab test", finfo[width], finfo[height], 2, rc1?, rc2!)

      --}}}
      --{{{  loop grabbing frames from the input, put them on the screen!
      SEQ
        out.string ("fastvid: starting capture...*n", 0, scr!)
        dev[req] ! capture; FALSE

        INITIAL BOOL run IS TRUE:
        INITIAL INT frameno IS 0:
        WHILE run
          RASTER r:
          SEQ
            rc2 ? r
            
            out.string ("frame: ", 0, scr!)
            out.int (frameno, 0, scr!)
            out.string ("*c", 0, scr!)
            scr ! FLUSH

            dev[resp] ? CASE
              RASTER frame:
              raster; frame
                INT mx, my:
                SEQ
                  IF
                    (SIZE frame) > (SIZE r)
                      my := SIZE r
                    TRUE
                      my := SIZE frame
                  IF
                    (SIZE frame[0]) > (SIZE r[0])
                      mx := SIZE r[0]
                    TRUE
                      mx := SIZE frame[0]

                  SEQ y = 0 FOR my
                    SEQ x = 0 FOR mx
                      r[y][x] := INT frame[y][x]

              MOBILE []BYTE msg:
              error; msg
                SEQ
                  out.string ("fastvid: failed to grab frame: ", 0, scr!)
                  out.string (msg, 0, scr!)
                  out.string ("*n", 0, scr!)
            --}}}

            frameno := frameno + 1
            
            rc1 ! r

      --}}}
:

PROC fastvid (CHAN BYTE kyb?, scr!, err!)
  CT.VIDEO! vcli:
  CT.VIDEO? vsvr:
  SEQ
    out.string ("fastvid: hello, opening [", 0, scr!)
    out.string (DEVICE.NAME, 0, scr!)
    out.string ("]*n", 0, scr!)

    vcli, vsvr := MOBILE CT.VIDEO
    PAR
      video.server (vsvr, DEVICE.NAME)

      SEQ
        vcli[req] ! get.current.input
        vcli[resp] ? CASE
          INT n:
          input.number; n
            fastvid.main (vcli, kyb?, scr!)
          MOBILE []BYTE str:
          error; str
            SEQ
              out.string ("fastvid: failed to start video server: ", 0, scr!)
              out.string (str, 0, scr!)
              out.string ("*n", 0, scr!)
        vcli[return] ! vcli
:


