Class: MPV::Server

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

Overview

Represents an active mpv process.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: File.join('/tmp', Utils.tmpsock), user_args: []) ⇒ Server

Returns a new instance of Server.

Parameters:

  • path (String) (defaults to: File.join('/tmp', Utils.tmpsock))

    the path of the socket to be created (defaults to a tmpname in /tmp)

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

    additional arguments to use when spawning mpv



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mpv/server.rb', line 55

def initialize(path: File.join('/tmp', Utils.tmpsock), user_args: [])
  @socket_path = path
  @args = [
    "--idle",
    "--terminal=no",
    "--input-ipc-server=%<path>s" % { path: @socket_path },
  ].concat(user_args).uniq

  @args.each { |arg| self.class.ensure_flag! arg }

  @pid = Process.spawn("mpv", *@args)
end

Instance Attribute Details

#argsArray<String> (readonly)

Returns the command-line arguments used when spawning mpv.

Returns:

  • (Array<String>)

    the command-line arguments used when spawning mpv



9
10
11
# File 'lib/mpv/server.rb', line 9

def args
  @args
end

#pidFixnum (readonly)

Returns the process id of the mpv process.

Returns:

  • (Fixnum)

    the process id of the mpv process



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

def pid
  @pid
end

#socket_pathString (readonly)

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

Returns:

  • (String)

    the path to the socket used by this mpv process



12
13
14
# File 'lib/mpv/server.rb', line 12

def socket_path
  @socket_path
end

Class Method Details

.available?Boolean

Returns whether mpv is executable within the system path.

Returns:

  • (Boolean)

    whether mpv is executable within the system path



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

def self.available?
  Utils.which?("mpv")
end

.ensure_available!Object

Ensures that a binary named mpv can be executed.

Raises:



39
40
41
# File 'lib/mpv/server.rb', line 39

def self.ensure_available!
  raise MPVNotAvailableError unless available?
end

.ensure_flag!(flag) ⇒ Object

Ensures that that the mpv being executed supports the given flag.

Raises:



46
47
48
49
# File 'lib/mpv/server.rb', line 46

def self.ensure_flag!(flag)
  ensure_available!
  raise MPVUnsupportedFlagError, flag unless flag?(flag)
end

.flag?(flag) ⇒ Boolean

Note:

returns false if mpv is not available

Returns whether mpv supports the given flag.

Returns:

  • (Boolean)

    whether mpv supports the given flag



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mpv/server.rb', line 24

def self.flag?(flag)
  return false unless available?

  # MPV allows flags to be suffixed with =yes or =no, but doesn't
  # include these variations in their list. They also allow a --no-
  # prefix that isn't included in the list, so we normalize these out.
  # Additionally, we need to remove trailing arguments.
  normalized_flag = flag.sub(/^--no-/, "--").sub(/=\S*/, "")

  flags = `mpv --list-options`.split.select { |s| s.start_with?("--") }
  flags.include?(normalized_flag)
end

Instance Method Details

#running?Boolean

Returns whether or not the mpv process is running.

Returns:

  • (Boolean)

    whether or not the mpv process is running



69
70
71
72
73
# File 'lib/mpv/server.rb', line 69

def running?
  !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil?
rescue Errno::ECHILD
  false
end