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<Proc>
Callback procs 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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mpv/client.rb', line 23 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<Proc>
Returns callback procs that will be invoked whenever mpv emits an event.
20 21 22 |
# File 'lib/mpv/client.rb', line 20 def callbacks @callbacks end |
#socket_path ⇒ String (readonly)
Returns the path of the socket used to communicate with mpv.
16 17 18 |
# File 'lib/mpv/client.rb', line 16 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.
42 43 44 |
# File 'lib/mpv/client.rb', line 42 def alive? @alive end |
#command(*args) ⇒ Hash
Sends a command to the mpv process.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mpv/client.rb', line 51 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.
79 80 81 82 83 |
# File 'lib/mpv/client.rb', line 79 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.
88 89 90 91 92 93 94 |
# File 'lib/mpv/client.rb', line 88 def quit! command "quit" if alive? 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.
68 69 70 71 72 |
# File 'lib/mpv/client.rb', line 68 def set_property(*args) return unless alive? command "set_property", *args end |