Class: VNCRec::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/vncrec/recorder.rb

Overview

A recorder itself.

Constant Summary collapse

DEFAULTS =
{
  :pix_fmt      => :BGR8,
  :debug        => nil,
  :encoding     => VNCRec::ENC_RAW,
  :filename     => nil,
  :fps          => 6,
  :input        => nil,
  :port         => 5900
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Recorder


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
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vncrec/recorder.rb', line 46

def initialize(options = {})
  options = VNCRec::Recorder::DEFAULTS.merge(options)
  @logging = options[:logging] || options[:logger] || options[:log] || false
  if @logging
    @logger = Logger.new STDERR
    @logger.datetime_format = '% d_%m  %H-%M-%S.%6N'
    @logger.info options.inspect
  end

  @debug = options[:debug]
  $stderr.puts 'Debug mode' if @debug

  @port = options[:port]
  fail ArgumentError, 'Invalid port value' unless @port.is_a?(Numeric) && (1024..65_535).include?(@port)

  @host = options[:host]

  @client = nil

  @framerate = options[:fps]
  fail ArgumentError if !@framerate.is_a?(Numeric) || @framerate <= 0

  @filename = options[:filename] || (options[:port].to_s + '.raw')
  fail "Cannot create file #{@filename}" unless system "touch #{@filename}"

  if options[:geometry]
    @geometry = options[:geometry]
    fail ArgumentError, "Geometry is invalid, expected: <x>x<y>, \
      got: #{@geometry.inspect}" unless valid_geometry?(@geometry)
  end

  @enc = options[:encoding]

  pf = options[:pix_fmt].to_s.dup.prepend('PIX_FMT_').upcase.to_sym
  fail ArgumentError, "Unknown pix_fmt #{options[:pix_fmt]}" unless VNCRec.const_defined? pf
  @pix_fmt = VNCRec.const_get(pf)

  @ffmpeg_iv_opts = options[:ffmpeg_iv_opts]
  @ffmpeg_ia_opts = options[:ffmpeg_ia_opts]
  @ffmpeg_out_opts = options[:ffmpeg_out_opts]
  Thread.abort_on_exception = true
  @on_exit = [:close_file, :close_proxy]

  @file = nil
  @sleep_time = 0.01
  @recording_starttime = nil
end

Instance Attribute Details

#on_exitObject


44
45
46
# File 'lib/vncrec/recorder.rb', line 44

def on_exit
  @on_exit
end

Instance Method Details

#filesizeObject

Return current size of file


117
118
119
# File 'lib/vncrec/recorder.rb', line 117

def filesize
  @file.size
end

#runObject

Start routine: wait for connection, perform handshake, get data, write data. Non-blocking.


97
98
99
100
101
# File 'lib/vncrec/recorder.rb', line 97

def run
  @loop = Thread.new do
    routine
  end
end

#running?Boolean


127
128
129
# File 'lib/vncrec/recorder.rb', line 127

def running?
  @loop && @loop.alive?
end

#stopObject

Safely stop any interaction with VNC server, close file. Execute all on_exit hooks.


107
108
109
110
111
112
113
# File 'lib/vncrec/recorder.rb', line 107

def stop
  @loop.kill unless Thread.current == @loop
  @on_exit.each do |bl|
    send bl if bl.is_a? Symbol
    bl.call if bl.respond_to?(:call)
  end
end

#stopped?bool

Find out if main loop thread is alive.


123
124
125
# File 'lib/vncrec/recorder.rb', line 123

def stopped?
  !(@loop.nil? && @loop.alive?)
end