Class: MobyUtil::TDriverWinCam

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

Overview

Windows DirectShow webcam implementation

Constant Summary collapse

STARTUP_TIMEOUT =
60
DEFAULT_OPTIONS =
{ :device => nil, :width => 640, :height => 480, :fps => 30 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tdriver/util/video/camera_windows.rb', line 40

def initialize( video_file, user_options = {} )

  @_device = nil
  @_video_file = nil
  @_recording = false
  @_rec_options = nil
  @_owcc_startex = nil
  @_owcc_stop = nil
    
  require 'Win32API'      

  begin     
  
    @_owcc_startex = Win32API.new( 'TDriverWebCamControl', 'OscarWebCamControlStartEx', [ 'P', 'N', 'N', 'N', 'L', 'L', 'L', 'I', 'I'  ], 'L' )
    @_owcc_stop = Win32API.new( 'TDriverWebCamControl', 'OscarWebCamControlStop', [ 'L' ], 'V' )
   
  rescue Exception => e
  
    begin 
        
      #OSCARWEBCAMCONTROL_API long OscarWebCamControlStartEx(char* captureFile, double compQuality, DWORD dwBitsPerSec, long lFramesPerSec, long lCapWidth, long lCapHeight, bool bFlipV, bool bFlipH)
      @_owcc_startex = Win32API.new( 'OscarWebCamControl', 'OscarWebCamControlStartEx', [ 'P', 'N', 'N', 'N', 'L', 'L', 'L', 'I', 'I'  ], 'L' )
      #OSCARWEBCAMCONTROL_API void OscarWebCamControlStop(long pTargetMediaControl)
      @_owcc_stop = Win32API.new( 'OscarWebCamControl', 'OscarWebCamControlStop', [ 'L' ], 'V' )
      
    rescue Exception => ee
      raise RuntimeError.new( "Failed to connect to video recording DLL file (TDriverWebCamControl.dll or OscarWebCamControl.dll). Details:\n" + ee.message )
    end
    
  end

  if user_options.has_key? :device

    puts "WARNING: TDriverWinCam does not support the :device option. This setting is ignored."

  end
  
  @_control_id = nil
  @_video_file = video_file
  @_rec_options = DEFAULT_OPTIONS.merge user_options
      
end

Class Method Details

.new_cam(*args) ⇒ Object

TODO: document me



30
31
32
33
34
# File 'lib/tdriver/util/video/camera_windows.rb', line 30

def self.new_cam( *args )

  MobyUtil::TDriverWinCam.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)


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
115
116
117
118
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
147
148
149
150
151
152
153
154
155
# File 'lib/tdriver/util/video/camera_windows.rb', line 86

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

  @_control_id = @_owcc_startex.call( @_video_file, 0, 0, 0, @_rec_options[ :fps ].to_i, @_rec_options[ :width ].to_i, @_rec_options[ :height ].to_i, 0, 0 )
  
  if @_control_id == 0

    raise RuntimeError.new( "Failed to start video recording.\nFile: " + @_video_file + "\nFPS: " + @_rec_options[ :fps ].to_s + "\nWidth: " + @_rec_options[ :width ].to_s + "\nHeight: " + @_rec_options[ :height ].to_s )

  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 1

    # force refresh file size
    begin

      if File.exists?( @_video_file ) 
        File.open( @_video_file, 'r' ) do 
        end      
      end

    rescue

    end      
    
    if Time.now > file_timeout
      file_timed_out = true
    end

  end
  
  if file_timed_out

    # make sure recording is not initializing, clean up any failed file        
    begin
      @_owcc_stop.call( @_control_id )
    rescue
    end
    
    if File.exists?( @_video_file )
      begin
        File.delete( @_video_file )
      rescue
      end
    end      

    raise RuntimeError.new( "Failed to start recording. Timeout: #{STARTUP_TIMEOUT} File: \"#{@_video_file}\" " )
  end
  
  @_recording = true
  
  nil
  
end

#stop_recordingObject

Stops ongoing recording



158
159
160
161
162
163
164
165
166
167
# File 'lib/tdriver/util/video/camera_windows.rb', line 158

def stop_recording 

  if @_recording      
    @_recording = false            
    @_owcc_stop.call( @_control_id )
  end

  nil

end