Class: MPV::Server
- Inherits:
-
Object
- Object
- MPV::Server
- Defined in:
- lib/mpv_ipc/server.rb
Overview
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
-
#ipc_path ⇒ String
readonly
The path to the IPC socket used by this ‘mpv` process.
-
#mpv_path ⇒ String
readonly
The path to the currently used ‘mpv`.
-
#options ⇒ Array<String>
readonly
Command-line options used when spawning ‘mpv`.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Checks whether the ‘mpv` process is still alive.
-
#initialize(bind: nil, mpv: nil, options: [], chdir: nil) ⇒ Server
constructor
Creates a new MPV::Server instance and starts the ‘mpv` process.
-
#kill!(signal = :TERM) ⇒ Boolean
Sends a signal to the ‘mpv` process.
-
#wait ⇒ void
Blocks until the ‘mpv` process is terminated, then releases all resources.
Constructor Details
#initialize(bind: nil, mpv: nil, options: [], chdir: nil) ⇒ Server
Creates a new MPV::Server instance and starts the ‘mpv` process.
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 = .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? = stdout.scan(/^\s*--(\S+)/).map(&:first) @options.each{ |option| ensure_option!(option, ) } 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.
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`.
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`.
15 16 17 |
# File 'lib/mpv_ipc/server.rb', line 15 def @options end |
Instance Method Details
#alive? ⇒ Boolean
Checks whether the ‘mpv` process is still 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.
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 |