Class: MPV::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/mpv_ipc/server.rb

Overview

Note:

Resources are released by calling #wait after termination. At exit, all running mpv processes are automatically stopped.

Represents an active mpv process started with an IPC socket.

Constant Summary collapse

@@existing_servers =
Concurrent::Array.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind: nil, mpv: nil, options: [], chdir: nil) ⇒ Server

Creates a new MPV::Server instance and starts the mpv process.

Parameters:

  • bind (String) (defaults to: nil) —

    the path of the IPC socket to be used for the communication (defaults to a temporary path in /tmp)

  • mpv (String) (defaults to: nil) —

    the path of the mpv executable (defaults to searching in system $PATH)

  • options (Array<String>) (defaults to: []) —

    additional initial options to pass to the mpv process

  • chdir (String) (defaults to: nil) —

    custom working directory for mpv

Raises:



41
42
43
44
45
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
# File 'lib/mpv_ipc/server.rb', line 41

def initialize(bind: nil, mpv: nil, options: [], chdir: nil)
  @mpv_path = mpv || Utils.which("mpv")
  raise NotAvailableError unless @mpv_path

  @ipc_path = bind || File.join("/tmp", Utils.tmpsock)
  File.delete(@ipc_path) if File.exist?(@ipc_path)

  @options = options.map{ |option| option.sub(/\A-{1,2}/, "") }
  @options << "terminal=no" << "input-ipc-server=#{@ipc_path}" << "idle=yes"

  stdout, status = Open3.capture2(@mpv_path, "--list-options") rescue nil
  raise NotAvailableError unless status&.success?
  available_options = stdout.scan(/^\s*--(\S+)/).map(&:first)
  @options.each{ |option| ensure_option!(option, available_options) }

  args = @options.map{ |option| "--#{option}" }
  params = {pgroup: true}.merge(({chdir: chdir} if chdir).to_h)
  @pid = Process.spawn(@mpv_path, *args, **params)

  timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC) + STARTUP_TIMEOUT
  until File.exist?(@ipc_path)
    if status = Process.wait2(@pid, Process::WNOHANG)&.last
      raise StartupError, "unexpected termination with " \
        "#{status.to_s.delete_prefix("pid #{status.pid} ")}"
    end
    if Process.clock_gettime(Process::CLOCK_MONOTONIC) > timeout
      Process.kill(:KILL, -@pid)
      Process.wait(@pid)
      raise StartupError, "timed out"
    end
    sleep 0.02
  end

  @@existing_servers << self
end

Instance Attribute Details

#ipc_path ⇒ String (readonly)

Returns the path to the IPC socket used by this mpv process.

Returns:

  • (String) —

    the path to the IPC socket used by this mpv process



21
22
23
# File 'lib/mpv_ipc/server.rb', line 21

def ipc_path
  @ipc_path
end

#mpv_path ⇒ String (readonly)

Returns the path to the currently used mpv.

Returns:

  • (String) —

    the path to the currently used mpv



18
19
20
# File 'lib/mpv_ipc/server.rb', line 18

def mpv_path
  @mpv_path
end

#options ⇒ Array<String> (readonly)

Returns command-line options used when spawning mpv.

Returns:

  • (Array<String>) —

    command-line options used when spawning mpv



15
16
17
# File 'lib/mpv_ipc/server.rb', line 15

def options
  @options
end

Instance Method Details

#alive? ⇒ Boolean

Checks whether the mpv process is still alive.

Returns:

  • (Boolean) —

    true if the mpv process is alive



79
80
81
82
# File 'lib/mpv_ipc/server.rb', line 79

def alive?
  @pid = nil if wait_pid(@pid, false)
  !!@pid
end

#kill!(signal = :TERM) ⇒ Boolean

Sends a signal to the mpv process.

Parameters:

  • signal (Object) (defaults to: :TERM) —

    the name (String/Symbol) or code (Integer) of the signal to send

Returns:

  • (Boolean) —

    true if the signal was sent successfully



97
98
99
100
101
102
103
# File 'lib/mpv_ipc/server.rb', line 97

def kill!(signal=:TERM)
  pid = @pid
  Process.kill(signal, -pid) if pid
  !!pid
rescue Errno::ESRCH
  false
end

#wait ⇒ void

This method returns an undefined value.

Blocks until the mpv process is terminated, then releases all resources.



86
87
88
89
90
91
# File 'lib/mpv_ipc/server.rb', line 86

def wait
  wait_pid(@pid, true)
  File.delete(@ipc_path) rescue nil if @ipc_path
  @pid = @ipc_path = nil
  @@existing_servers.delete(self)
end