Class: Puppeteer::ScreenRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/screen_recorder.rb,
sig/puppeteer/screen_recorder.rbs

Constant Summary collapse

DEFAULT_FPS =

Returns:

  • (::Integer)
30
DEFAULT_QUALITY =

Returns:

  • (::Integer)
30
STOP =

Returns:

  • (Object)
Object.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, width, height, options = {}) ⇒ Object

Parameters:

  • page (Puppeteer::Page)
  • width (Numeric)
  • height (Numeric)
  • options (Hash[Symbol, untyped]) (defaults to: {})


28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/puppeteer/screen_recorder.rb', line 28

def initialize(page, width, height, options = {})
  @page = page
  @fps = options.fetch(:fps, DEFAULT_FPS)
  @format = (options[:format] || 'webm').to_s
  @path = options[:path]
  @stopped = false
  @stop_mutex = Mutex.new
  @finished = false
  @finish_mutex = Mutex.new
  @frame_queue = Queue.new
  @output = +''.b
  @last_buffer = ''.b
  @last_written_at = monotonic_time
  @start_timestamp = nil
  @previous_timestamp = nil
  @previous_buffer = nil

  ensure_output_directory
  command = build_command(width, height, options)
  @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(*command)
  @stdin.binmode
  @stdout.binmode

  @writer_thread = Thread.new { write_frames }
  @stdout_thread = Thread.new { read_output }
  @stderr_thread = Thread.new { read_stderr }

  @client = @page.main_frame.client
  @frame_listener_id = @client.add_event_listener('Page.screencastFrame') do |event|
    handle_frame(event)
  end
  @disconnect_listener_id = @client.once(CDPSessionEmittedEvents::Disconnected) do
    stop
  rescue StandardError => error
    warn(error.message) if ENV['DEBUG']
  end
end

Class Method Details

.count_frames(start_timestamp, previous_timestamp, timestamp, fps) ⇒ Integer

Parameters:

  • start_timestamp (Numeric)
  • previous_timestamp (Numeric)
  • timestamp (Numeric)
  • fps (Numeric)

Returns:

  • (Integer)


18
19
20
21
22
# File 'lib/puppeteer/screen_recorder.rb', line 18

def self.count_frames(start_timestamp, previous_timestamp, timestamp, fps)
  finish = ((timestamp - start_timestamp) * fps).round
  start = ((previous_timestamp - start_timestamp) * fps).round
  [0, finish - start].max
end

Instance Method Details

#dataString

Returns:

  • (String)


67
68
69
# File 'lib/puppeteer/screen_recorder.rb', line 67

def data
  @output.dup
end

#stopvoid

This method returns an undefined value.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppeteer/screen_recorder.rb', line 72

def stop
  should_stop = @stop_mutex.synchronize do
    next false if @stopped

    @stopped = true
    true
  end
  unless should_stop
    finish_process
    return
  end

  begin
    @page._stop_screencast
  rescue StandardError
    # The page or its CDP session may already be gone.
  end
  enqueue_last_frame
  finish_process
end