#!/usr/bin/ruby1.8

require 'tk' 
require 'open3'
require 'net/telnet'
require 'fileutils'
require 'date'


DESTINATION_DIR = "/tmp"
MAXIMUM_DURATION= 5
VLC_COMMAND=%{vlc -IRC  --rc-host localhost:4444 --v4l-vdev "/dev/video0" --v4l-norm 3 --sout '#transcode{vcodec=mp4v,vb=3072,scale=1,acodec=mpga,ab=192,channels=2}:duplicate{dst=display,dst=std{access=file,mux=mp4,dst="/tmp/test.mpg"}}'  --contrast 1.9 --brightness 1.7    --saturation 2.3 }



class Recorder
    def initialize
        start_vlc

        @recording = false
        @root = TkRoot.new { title "Ex1" } 
        @telnet = nil
        begin
            @telnet = Net::Telnet.new( "Host" => "localhost", "Port" => 4444, "TelnetMode" => false, "Prompt" => //)
        rescue Exception => e
            sleep 1
            retry
        end
        font = TkFont.new('courier')
        font.configure('size'=>48) 
        button = nil
        @label = TkLabel.new(@root)  do 
            text 'test'
            font font
            pack('side' => 'top' , 'padx' =>  10 , 'pady' =>  10)
        end
        
       button = TkButton.new(@root) do 
                    text 'Start!' 
                    font font
                    foreground 'black' 
                    background 'green' 
                    pack('side' => 'top' , 'padx' =>  150 , 'pady' =>  150)
                end
        button.command { start_stop }
        button.pack { padx 15 ; pady 15; side 'left' } 
         
        @button = button
        @root.bind('Return') {  start_stop }
        Tk.mainloop
    end


    def update_button
        if !@recording
            start_recording
        else
            stop_recording
        end
    end
    def start_vlc
        @vlc_thread = Thread.new do 
            system VLC_COMMAND
        end
        @vlc_started = true
    end
    
    def start_recording
        @recording = true
        @button.background = "red"
        @button.text = "STOP"
        @telnet.cmd("add v4l://")
        @start_time = Time.now
        @counter_thread = Thread.new(@label, @start_time) do |label, start|
            while ((duration = (Time.now - start).to_i)<5)
                sleep 1
                label.text %{reste #{MAXIMUM_DURATION - duration -1}s over}
            end
            label.text %{reste #{MAXIMUM_DURATION }s over}
            stop_recording
        end
        #output window has WM_NAME =  VLC (XVideo output)
        #defwinprop { title ="VLC XVideo ouput"), target = "vlc"}
        #defwinprop { class ="Webcamrecorder.rb" , instance = "webcamrecorder.rb" , target = "app"}
        #META1-F3 -> exec code: mod_query.query_renameframe(_)
        #META1-M -> window-info to get class, instance, etc
    end
    def stop_recording
        @recording = false
        @telnet.cmd("stop")
        @button.background = "green"
        @button.text = "Start"
        file = DateTime.now.strftime("%Y-%m-%dT%H:%M:%S")
        FileUtils.mv("/tmp/test.mpg", DESTINATION_DIR+"/"+file+".mpg")
        @counter_thread.kill
    end

    def start_stop
       update_button 
    end
    def terminate
        @telnet.cmd("quit")
    end
end

r = Recorder.new
r.terminate

