Class: MPV::Client
- Inherits:
-
Object
- Object
- MPV::Client
- Defined in:
- lib/mpv/client.rb
Overview
Represents a connection to a mpv process that has been spawned with an IPC socket.
Instance Attribute Summary collapse
-
#callbacks ⇒ Array<MPV::Callback>
Callback objects that will be invoked whenever mpv emits an event.
-
#socket_path ⇒ String
readonly
The path of the socket used to communicate with mpv.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Whether or not the player is currently active.
-
#command(*args) ⇒ Hash
Sends a command to the mpv process.
-
#get_property(*args) ⇒ Object
Retrieves a property from the mpv process.
-
#initialize(path) ⇒ Client
constructor
A new instance of Client.
-
#quit! ⇒ void
Terminates the mpv process.
-
#set_property(*args) ⇒ Hash
Sends a property change to the mpv process.
Constructor Details
#initialize(path) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mpv/client.rb', line 21 def initialize(path) @socket_path = path @socket = UNIXSocket.new(@socket_path) @alive = true @callbacks = [] @command_queue = Queue.new @result_queue = Queue.new @event_queue = Queue.new @command_thread = Thread.new { pump_commands! } @results_thread = Thread.new { pump_results! } @events_thread = Thread.new { dispatch_events! } end |
Instance Attribute Details
#callbacks ⇒ Array<MPV::Callback>
Returns callback objects that will be invoked whenever mpv emits an event.
18 19 20 |
# File 'lib/mpv/client.rb', line 18 def callbacks @callbacks end |
#socket_path ⇒ String (readonly)
Returns the path of the socket used to communicate with mpv.
14 15 16 |
# File 'lib/mpv/client.rb', line 14 def socket_path @socket_path end |
Instance Method Details
#alive? ⇒ Boolean
When false, most methods will cease to function.
Returns whether or not the player is currently active.
40 41 42 |
# File 'lib/mpv/client.rb', line 40 def alive? @alive end |
#command(*args) ⇒ Hash
Sends a command to the mpv process.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mpv/client.rb', line 49 def command(*args) return unless alive? payload = { "command" => args } @command_queue << JSON.generate(payload) @result_queue.pop end |
#get_property(*args) ⇒ Object
Retrieves a property from the mpv process.
77 78 79 80 81 |
# File 'lib/mpv/client.rb', line 77 def get_property(*args) return unless alive? command("get_property", *args)["data"] end |
#quit! ⇒ void
this object becomes garbage once this method is run
This method returns an undefined value.
Terminates the mpv process.
86 87 88 89 90 91 92 93 |
# File 'lib/mpv/client.rb', line 86 def quit! return unless alive? command "quit" ensure @alive = false @socket = nil File.delete(@socket_path) if File.exist?(@socket_path) end |
#set_property(*args) ⇒ Hash
Sends a property change to the mpv process.
66 67 68 69 70 |
# File 'lib/mpv/client.rb', line 66 def set_property(*args) return unless alive? command "set_property", *args end |