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: Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock"), user_args: []) ⇒ Server

Returns a new instance of Server.

Parameters:

  • path (String) (defaults to: Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock"))

    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



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

def initialize(path: Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock"),
               user_args: [])

  @socket_path = path
  @args = [
    "--idle",
    "--terminal=no",
    "--input-ipc-server=%{path}" % { 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



7
8
9
# File 'lib/mpv/server.rb', line 7

def args
  @args
end

#pidFixnum (readonly)

Returns the process id of the mpv process.

Returns:

  • (Fixnum)

    the process id of the mpv process



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

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



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

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



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

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

.ensure_available!Object

Ensures that a binary named mpv can be executed.

Raises:



37
38
39
# File 'lib/mpv/server.rb', line 37

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:



44
45
46
47
# File 'lib/mpv/server.rb', line 44

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

.has_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



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

def self.has_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
74
75
# File 'lib/mpv/server.rb', line 69

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