Class: MPV::Server
- Inherits:
-
Object
- Object
- MPV::Server
- Defined in:
- lib/mpv/server.rb
Overview
Represents an active mpv process.
Instance Attribute Summary collapse
-
#args ⇒ Array<String>
readonly
The command-line arguments used when spawning mpv.
-
#pid ⇒ Fixnum
readonly
The process id of the mpv process.
-
#socket_path ⇒ String
readonly
The path to the socket used by this mpv process.
Class Method Summary collapse
-
.available? ⇒ Boolean
Whether
mpv
is executable within the system path. -
.ensure_available! ⇒ Object
Ensures that a binary named
mpv
can be executed. -
.ensure_flag!(flag) ⇒ Object
Ensures that that the
mpv
being executed supports the given flag. -
.flag?(flag) ⇒ Boolean
Whether
mpv
supports the given flag.
Instance Method Summary collapse
-
#initialize(path: File.join('/tmp', Utils.tmpsock), user_args: []) ⇒ Server
constructor
A new instance of Server.
-
#running? ⇒ Boolean
Whether or not the mpv process is running.
Constructor Details
#initialize(path: File.join('/tmp', Utils.tmpsock), user_args: []) ⇒ Server
Returns a new instance of Server.
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
#args ⇒ Array<String> (readonly)
Returns the command-line arguments used when spawning mpv.
9 10 11 |
# File 'lib/mpv/server.rb', line 9 def args @args end |
#pid ⇒ Fixnum (readonly)
Returns the process id of the mpv process.
15 16 17 |
# File 'lib/mpv/server.rb', line 15 def pid @pid end |
#socket_path ⇒ String (readonly)
Returns 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.
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.
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.
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
returns false if mpv
is not available
Returns 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.
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 |