From 4ec91a10f0f8a227699159e8fee509e38f187b90 Mon Sep 17 00:00:00 2001 From: Lakshya Kapoor <4314581+kapoorlakshya@users.noreply.github.com> Date: Fri, 19 Jul 2024 08:27:00 +0000 Subject: [PATCH] [wip] make screenshots specs pass --- lib/screen-recorder/common.rb | 20 ++++++++++++++------ lib/screen-recorder/process.rb | 2 +- lib/screen-recorder/screenshot.rb | 8 ++++---- spec/screen-recorder/screenshot_spec.rb | 6 ++---- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/screen-recorder/common.rb b/lib/screen-recorder/common.rb index 28cfbaf..ecd97b0 100644 --- a/lib/screen-recorder/common.rb +++ b/lib/screen-recorder/common.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/screen-recorder/process.rb b/lib/screen-recorder/process.rb index ca761b6..49b66c2 100644 --- a/lib/screen-recorder/process.rb +++ b/lib/screen-recorder/process.rb @@ -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 diff --git a/lib/screen-recorder/screenshot.rb b/lib/screen-recorder/screenshot.rb index 048d978..79d0f55 100644 --- a/lib/screen-recorder/screenshot.rb +++ b/lib/screen-recorder/screenshot.rb @@ -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 diff --git a/spec/screen-recorder/screenshot_spec.rb b/spec/screen-recorder/screenshot_spec.rb index 0d0d25f..143f330 100644 --- a/spec/screen-recorder/screenshot_spec.rb +++ b/spec/screen-recorder/screenshot_spec.rb @@ -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