class MPV::Server

Represents an active mpv process.

Attributes

args[R]

@return [Array<String>] the command-line arguments used when spawning mpv

pid[R]

@return [Fixnum] the process id of the mpv process

socket_path[R]

@return [String] the path to the socket used by this mpv process

Public Class Methods

available?() click to toggle source

@return [Boolean] whether `mpv` is executable within the system path

# File lib/mpv/server.rb, line 18
def self.available?
  Utils.which?("mpv")
end
ensure_available!() click to toggle source

Ensures that a binary named `mpv` can be executed. @raise [MPVNotAvailableError] if no `mpv` executable in the system path

# File lib/mpv/server.rb, line 39
def self.ensure_available!
  raise MPVNotAvailableError unless available?
end
ensure_flag!(flag) click to toggle source

Ensures that that the `mpv` being executed supports the given flag. @raise [MPVNotAvailableError] if no `mpv` executable in the system path @raise [MPVUnsupportedFlagError] if `mpv` does not support the given flag

# File lib/mpv/server.rb, line 46
def self.ensure_flag!(flag)
  ensure_available!
  raise MPVUnsupportedFlagError, flag unless flag?(flag)
end
flag?(flag) click to toggle source

@return [Boolean] whether `mpv` supports the given flag @note returns false if `mpv` is not available

# 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
new(path: File.join('/tmp', Utils.tmpsock), user_args: []) click to toggle source

@param path [String] the path of the socket to be created

(defaults to a tmpname in `/tmp`)

@param user_args [Array<String>] additional arguments to use when

spawning mpv
# 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

Public Instance Methods

running?() click to toggle source

@return [Boolean] whether or not the mpv process is running

# File lib/mpv/server.rb, line 69
def running?
  !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil?
rescue Errno::ECHILD
  false
end