Class: MobyUtil::TDriverLinuxCam

Inherits:
Object
  • Object
show all
Defined in:
lib/tdriver/util/video/camera_linux.rb

Overview

Linux streamer webcam implementation Requires that the streamer application is installed

Constant Summary collapse

STARTUP_TIMEOUT =
60
DEFAULT_OPTIONS =
{ :device => '/dev/video0', :width => 320, :height => 240, :fps => 5, :max_time => '99:00:00' }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(video_file, user_options = {}) ⇒ TDriverLinuxCam

Creates a new recroding object witdh the given recording options

params

video_file: String, path and name of file where the recorded video is stored user_options: (optional) Hash, keys :fps, :width, :height can be used to overwrite defaults



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tdriver/util/video/camera_linux.rb', line 43

def initialize( video_file, user_options = {} )      

  @_device = nil
  @_video_file = nil
  @_recording = false
  @_rec_options = nil
  @_owcc_startex = nil
  @_owcc_stop = nil
  
  @_control_id = nil
  @_video_file = video_file
  @_rec_options = DEFAULT_OPTIONS.merge user_options
      
  @rec_app = 'streamer'
end

Class Method Details

.new_cam(*args) ⇒ Object

TODO: document me



33
34
35
36
37
# File 'lib/tdriver/util/video/camera_linux.rb', line 33

def self.new_cam( *args )

  MobyUtil::TDriverLinuxCam.new( *args )

end

Instance Method Details

#start_recordingObject

Starts recording based on options given during initialization

raises

RuntimeError: No filename has been defined or recording initialization failed due to timeout.

Raises:

  • (RuntimeError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tdriver/util/video/camera_linux.rb', line 62

def start_recording

  raise RuntimeError.new( "No video file defined, unable to start recording." ) if @_video_file.nil?

  if File.exists?( @_video_file )        
    begin
      File.delete( @_video_file )
        rescue
      # no reaction to failed file ops, unless recording fails
    end
  end  
  
  rec_opts = ' -q -c ' + @_rec_options[ :device ].to_s + ' -f rgb24 -r ' + @_rec_options[ :fps ].to_s + ' -t ' + 
    @_rec_options[:max_time] + ' -o ' +  @_video_file.to_s + ' -s ' + @_rec_options[ :width ].to_s + 'x' + @_rec_options[ :height ].to_s 
  begin         
    @stdin, @stdout, @stderr = Open3.popen3(@rec_app + rec_opts)
  rescue => e
    raise RuntimeError.new( "An error was encountered while launching streamer:\n" << e.inspect )
  end

  file_timed_out = false

  file_timeout = Time.now + STARTUP_TIMEOUT

  while File.size?( @_video_file ).nil? && !file_timed_out do

    #wait for recording to start, ie. filesize > 0
    sleep 0.1

    file_timed_out = true if Time.now > file_timeout

  end
  
  if file_timed_out

    # make sure recording is not initializing, clean up any failed file        
    stop_recording

    if File.exists?( @_video_file )
      begin
        File.delete( @_video_file )
      rescue
      end
    end

    raise RuntimeError.new( "Failed to start recording. Timeout: #{STARTUP_TIMEOUT} second(s). File: \"#{@_video_file}\" " )

  end

  @_recording = true
  
  nil
end

#stop_recordingObject

Stops ongoing recording



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tdriver/util/video/camera_linux.rb', line 119

def stop_recording 

  if @_recording      

    @_recording = false            
    
    begin
      @stdin.close unless @stdin.closed?
      @stdout.close unless @stdout.closed?
      @stderr.close unless @stderr.closed?
      
      sleep 10 # allow some time to close (yes, it can take up to 10 seconds)
      pid = `pidof #{@rec_app}`.to_i
      begin 
        if pid != 0
          Process.kill(9,pid)
        end
      rescue => e
        puts "Got error " + e.inspect
      end
    rescue => e
      raise RuntimeError.new( "Failed to end recording. Errror:\n " << e.inspect)
    end
  end
  
  nil
  
end