Skip to content

Commit

Permalink
[wip] make screenshots specs pass
Browse files Browse the repository at this point in the history
  • Loading branch information
kapoorlakshya committed Jul 19, 2024
1 parent 1418c3d commit 4ec91a1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
20 changes: 14 additions & 6 deletions lib/screen-recorder/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def start
def stop
ScreenRecorder.logger.debug 'Stopping ffmpeg...'
@process.stop

if @process.alive?
ScreenRecorder.logger.error "Failed to stop ffmpeg (pid: #{@process.pid}). Please kill it manually."
return
end

ScreenRecorder.logger.debug 'Stopped ffmpeg.'
ScreenRecorder.logger.info 'Preparing video...'
@video = prepare_video
Expand Down Expand Up @@ -128,13 +134,15 @@ def lines_from_log(position = :last, count = 2)
#
# Executes the given command and outputs to the
#
def execute_command(cmd)
def execute_command(cmd, log = options.log)
ScreenRecorder.logger.debug "Log: #{log}"
ScreenRecorder.logger.debug "Executing command: #{cmd}"
@process = ScreenRecorder::Process.build(cmd)
@process.detach = true
@process.io = options.log
@process.start
@process
process = ScreenRecorder::Process.build(cmd)
process.detach = true
FileUtils.touch(log)
process.io = log
process.start
process
end
end
end
2 changes: 1 addition & 1 deletion lib/screen-recorder/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def stop(timeout = 3)
ScreenRecorder.logger.debug(" -> stopped #{@pid}")
rescue TimeoutError, Errno::EINVAL
ScreenRecorder.logger.debug(" -> sending KILL to process: #{@pid}")
kill(@pid)
kill(-@pid)
wait
ScreenRecorder.logger.debug(" -> killed #{@pid}")
end
Expand Down
8 changes: 4 additions & 4 deletions lib/screen-recorder/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module Screenshot
#
# Takes a screenshot in the current context (input) - desktop or current window
#
def screenshot(filename, resolution = nil)
def screenshot(filename, resolution = nil, log = 'ffmpeg-screenshot.log')
ScreenRecorder.logger.debug "Screenshot filename: #{filename}, resolution: #{resolution}"
cmd = screenshot_cmd(filename: filename, resolution: resolution)
process = execute_command(cmd)
exit_code = wait_for_process_exit(process) # 0 (success) or 1 (fail)
if exit_code&.zero?
process = execute_command(cmd, log)
process.poll_for_exit(5)
if process.exited?
ScreenRecorder.logger.info "Screenshot: #{filename}"
return filename
end
Expand Down
6 changes: 2 additions & 4 deletions spec/screen-recorder/screenshot_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
RSpec.describe 'ScreenRecorder::Screenshot' do
describe '.screenshot' do
let(:recorder) { ScreenRecorder::Desktop.new(input: test_input, output: test_output) }
let(:image_file) { 'screenshot.png' }

after { delete_file(image_file) }
let(:image_file) { "screenshot-#{Time.now.to_i}.png" }

context 'when recording a desktop' do
it 'can take a screenshot' do
recorder.start
recorder.screenshot(image_file)
recorder.screenshot(image_file, "ffmpeg-#{Time.now.to_i}.log")
recorder.stop
expect(File).to exist(image_file)
end
Expand Down

0 comments on commit 4ec91a1

Please sign in to comment.