Class: MPV::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Client

Returns a new instance of Client.

Parameters:

  • path (String)

    the domain socket for communication with mpv



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

#callbacksArray<Proc>

Returns callback procs that will be invoked whenever mpv emits an event.

Returns:

  • (Array<Proc>)

    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_pathString (readonly)

Returns the path of the socket used to communicate with mpv.

Returns:

  • (String)

    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

Note:

When false, most methods will cease to function.

Returns whether or not the player is currently active.

Returns:

  • (Boolean)

    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.

Examples:

client.command "loadfile", "mymovie.mp4", "append-play"

Parameters:

  • args (Array)

    the individual command arguments to send

Returns:

  • (Hash)

    mpv's response to the command



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.

Examples:

client.get_property "pause" # => true

Parameters:

  • args (Array)

    the individual property arguments to send

Returns:

  • (Object)

    the value of the property



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

Note:

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.

Examples:

client.set_property "pause", true

Parameters:

  • args (Array)

    the individual property arguments to send

Returns:

  • (Hash)

    mpv's response



68
69
70
71
72
# File 'lib/mpv/client.rb', line 68

def set_property(*args)
  return unless alive?

  command "set_property", *args
end