Skip to content

Commit

Permalink
privatize a method, allow it to return the empty string [whoops] from…
Browse files Browse the repository at this point in the history
… prompts, fixup FFmpegHelpers to use new device semantics a bit
  • Loading branch information
rdp committed Oct 23, 2013
1 parent 7e53d55 commit a87ef70
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
85 changes: 49 additions & 36 deletions lib/simple_gui_creator/ffmpeg_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module FFmpegHelpers

FFmpegNameToUse = 'ffmpeg' # can change it to whatever you want, like 'ffmpeg.exe' or 'full/path/ffmpeg.exe' or libav.exe

# returns like {:audio => [['audio name 1', 0]], ['audio name 2', 0]], :video => ...}
# returns like {:audio => [['audio name 1', 0]], ['audio name 1', 1], ['audio name 2', 0], :video => ...}
# use like vid_names = enumerate_directshow_devices[:video]
# then could use like name = DropDownSelector.new(nil, vid_names, "Select audio device to capture and stream").go_selected_value
def self.enumerate_directshow_devices
Expand All @@ -27,9 +27,9 @@ def self.enumerate_directshow_devices
video = enum.split('DirectShow')[1]
audio = enum.split('DirectShow')[2]

audio_names = parse_with_indexes audio
video_names = parse_with_indexes video
out = {:audio => audio_names, :video => video_names}
audio_devices = parse_with_indexes audio
video_devices = parse_with_indexes video
out = {:audio => audio_devices, :video => video_devices}
out
end

Expand All @@ -38,26 +38,6 @@ def self.video_device_present? device_and_idx
all.include?(device_and_idx)
end

def self.parse_with_indexes string
names = []
for line in string.lines
if line =~ /"(.+)"\n/
index = 0
names << [$1, index]
elsif line =~ /repeated (\d+) times/
$1.to_i.times {
previous_name = names[-1][0]
index += 1
names << [previous_name, index]
}
end
end
names
end
def self.escape_for_input name
name.gsub('"', '\\"')
end

# name is a non-escaped name, like video-screen-capture-device
def self.get_options_video_device name, idx = 0
ffmpeg_get_options_command = "#{FFmpegNameToUse} -list_options true -f dshow -i video=\"#{escape_for_input name}\" -video_device_number #{idx} 2>&1"
Expand All @@ -70,42 +50,75 @@ def self.get_options_video_device name, idx = 0
}.uniq # LODO actually starts with some duplicates ever? huh?
end

def self.warmup_ffmpeg_so_itll_be_disk_cached # and hopefully faster LODO hackey
system "#{FFmpegNameToUse} -list_devices true -f dshow -i dummy 2>&1" # outputs to stdout but...that's informative sometimes
def self.warmup_ffmpeg_so_itll_be_disk_cached # and hopefully faster later LODO this feels hackey
Thread.new {
system "#{FFmpegNameToUse} -list_devices true -f dshow -i dummy 2>&1" # outputs to stdout but...that's informative at times
}
end

def self.wait_for_ffmpeg_close out_handle, expected_time=0 # like the result of an IO.popen("ffmpeg ...", "w")
# requires some funky version of jruby to work...since it lacks Process.waitpid currently
# out_handle like the result of an IO.popen("ffmpeg ...", "w")
# raises if it closes in less than expected_time (if set to something greater than 0, that is, obviously)
def self.wait_for_ffmpeg_close out_handle, expected_time=0
# requires updated version of jruby to work...Jruby lacks Process.waitpid currently for doze JRUBY-4354
start_time = Time.now
while !out_handle.closed?
begin
if OS.jruby?
raise 'need jruby 1.7.0 for working Process.kill 0 (which we use) in windows...' unless JRUBY_VERSION >= '1.7.0'
end
Process.kill 0, out_handle.pid # ping it
sleep 0.2
Process.kill 0, out_handle.pid # ping it for liveness
sleep 0.1
rescue Errno::EPERM => e
puts 'detected ffmpeg is done wait_for_ffmpeg_close method'
# this can output twice in the case of one process piped to another? huh wuh?
puts 'detected ffmpeg is done [ping said so] in wait_for_ffmpeg_close method'
out_handle.close
end
end
elapsed = Time.now - start_time
if (expected_time > 0) && (elapsed < expected_time) # -1 ?
raise "ffmpeg possibly exited early!"
if (expected_time > 0) && (elapsed < expected_time) # XXX -1 default?
message = "ffmpeg failed too quickly, in #{elapsed}s"
puts message
raise message
else
puts "ffmpeg exited apparently gracefully in #{elapsed}s > #{expected_time}"
end
end

# who knows if I even use this anymore...
# screen capturer uses this
def self.combine_devices_for_ffmpeg_input audio_device, video_device
# XXX combine into same line??
if audio_device
audio_device="-f dshow -i audio=\"#{FFmpegHelpers.escape_for_input audio_device}\""
audio_device="-f dshow -audio_device_number #{audio_device[1]} -i audio=\"#{escape_for_input audio_device[0]}\" "
end
if video_device
video_device="-f dshow -i video=\"#{FFmpegHelpers.escape_for_input video_device}\""
video_device="-f dshow -video_device_number #{video_device[1]} -i video=\"#{escape_for_input video_device[0]}\" "
end
"#{video_device} #{audio_device}"
end

private

def self.parse_with_indexes string
names = []
for line in string.lines
if line =~ /"(.+)"\n/
index = 0
names << [$1, index]
elsif line =~ /repeated (\d+) times/
$1.to_i.times {
previous_name = names[-1][0]
index += 1
names << [previous_name, index]
}
end
end
names
end

def self.escape_for_input name
name.gsub('"', '\\"') # for shell :)
end

end

if $0 == __FILE__
Expand Down
6 changes: 3 additions & 3 deletions lib/simple_gui_creator/simple_gui_creator_main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ def self.new_previously_existing_file_selector_and_go title, use_this_dir = nil
end

# prompts for user input, raises if they cancel the prompt or if they enter nothing
# returns nil if cancel or if blank and cancel_or_blank_ok=true
# returns nil if cancelled or empty string if empty, both requiring cancel_or_blank_ok=true
def self.get_user_input(message, default = '', cancel_or_blank_ok = false)
p 'please enter the information in the prompt:' + message[0..50] + '...'
received = javax.swing.JOptionPane.showInputDialog(nil, message, default)
p 'received answer:' + received.to_s
p 'received answer:' + received.to_s # a string, an empty string, or a nil
if !received.present?
if !cancel_or_blank_ok
raise 'user cancelled input prompt ' + message unless received
raise 'did not enter anything?' + message unless received.present?
end
received = nil
#received = nil # allow empty string to pass through
end
received
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_gui_creator/swing_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def go_selected_index
alias go_selected_idx go_selected_index

def go_selected_value
puts 'select from dropdown window ' + @drop_down_elements[-1] + ' ...' if $simple_creator_show_console_prompts
puts 'select from dropdown window ' + @drop_down_elements[-1].to_s + ' ...' if $simple_creator_show_console_prompts
show # blocks...
raise 'did not select, exited early ' + @prompt unless @selected_idx
@drop_down_elements[@selected_idx]
Expand Down

0 comments on commit a87ef70

Please sign in to comment.