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:

  • the domain socket for communication with mpv



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

#callbacksArray<MPV::Callback>

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

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

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

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

Note:

When false, most methods will cease to function.

Returns whether or not the player is currently active.

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.

Examples:

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

Parameters:

  • the individual command arguments to send

Returns:

  • mpv's response to the command



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.

Examples:

client.get_property "pause" # => true

Parameters:

  • the individual property arguments to send

Returns:

  • the value of the property



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

Note:

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.

Examples:

client.set_property "pause", true

Parameters:

  • the individual property arguments to send

Returns:

  • mpv's response



66
67
68
69
70
# File 'lib/mpv/client.rb', line 66

def set_property(*args)
  return unless alive?

  command "set_property", *args
end