class MPV::Client

Represents a connection to a mpv process that has been spawned with an IPC socket. @see mpv.io/manual/stable/#json-ipc

MPV's IPC docs

@see mpv.io/manual/master/#properties

MPV's property docs

Attributes

callbacks[RW]

@return [Array<Proc>] callback procs that will be invoked

whenever mpv emits an event
socket_path[R]

@return [String] the path of the socket used to communicate with mpv

Public Class Methods

new(path) click to toggle source

@param path [String] the domain socket for communication with mpv

# 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

Public Instance Methods

alive?() click to toggle source

@return [Boolean] whether or not the player is currently active @note When false, most methods will cease to function.

# File lib/mpv/client.rb, line 42
def alive?
  @alive
end
command(*args) click to toggle source

Sends a command to the mpv process. @param args [Array] the individual command arguments to send @return [Hash] mpv's response to the command @example

client.command "loadfile", "mymovie.mp4", "append-play"
# 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) click to toggle source

Retrieves a property from the mpv process. @param args [Array] the individual property arguments to send @return [Object] the value of the property @example

client.get_property "pause" # => true
# File lib/mpv/client.rb, line 79
def get_property(*args)
  return unless alive?

  command("get_property", *args)["data"]
end
quit!() click to toggle source

Terminates the mpv process. @return [void] @note this object becomes garbage once this method is run

# 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) click to toggle source

Sends a property change to the mpv process. @param args [Array] the individual property arguments to send @return [Hash] mpv's response @example

client.set_property "pause", true
# File lib/mpv/client.rb, line 68
def set_property(*args)
  return unless alive?

  command "set_property", *args
end

Private Instance Methods

dispatch_events!() click to toggle source

Takes events from the event queue and dispatches them to callbacks. @api private

# File lib/mpv/client.rb, line 136
def dispatch_events!
  loop do
    event = @event_queue.pop

    callbacks.each do |callback|
      Thread.new do
        callback.call event
      end
    end
  end
end
distribute_results!() click to toggle source

Distributes results to the event and result queues. @api private

# File lib/mpv/client.rb, line 121
def distribute_results!
  response = JSON.parse(@socket.readline)

  if response["event"]
    @event_queue << response
  else
    @result_queue << response
  end
rescue
  @alive = false
  Thread.exit
end
pump_commands!() click to toggle source

Pumps commands from the command queue to the socket. @api private

# File lib/mpv/client.rb, line 100
def pump_commands!
  loop do
    begin
      @socket.puts(@command_queue.pop)
    rescue # the player is deactivating
      @alive = false
      Thread.exit
    end
  end
end
pump_results!() click to toggle source

Distributes results in a nonterminating loop. @api private

# File lib/mpv/client.rb, line 113
def pump_results!
  loop do
    distribute_results!
  end
end