Class: MPV::Slave

Inherits:
Object
  • Object
show all
Defined in:
lib/mpv-slave/mpv.rb

Instance Method Summary collapse

Constructor Details

#initializeSlave

Returns a new instance of Slave.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mpv-slave/mpv.rb', line 8

def initialize
  socket = Dir::Tmpname.make_tmpname "/tmp/mpv-slave", ".socket"

  @pid = Process.spawn(
    "mpv",
    "--idle",
    "--really-quiet",
    "--no-input-terminal",
    "--input-unix-socket", socket,
  )
  puts "waiting for mpv to start up..."
  until File.exists?(socket) or not running?
    sleep 0.1
  end
  raise "failed to start mpv" if not running?

  @socket = Socket.new(socket)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd, *args, &block) ⇒ Object



95
96
97
# File 'lib/mpv-slave/mpv.rb', line 95

def method_missing cmd, *args, &block
  command cmd.to_s, *args, &block
end

Instance Method Details

#add(file) ⇒ Object



49
50
51
# File 'lib/mpv-slave/mpv.rb', line 49

def add file
  play file, mode: "append-play", wait: false
end

#closeObject



99
100
101
102
103
# File 'lib/mpv-slave/mpv.rb', line 99

def close
  Process.kill :SIGTERM, @pid
  Process.wait @pid
  @socket.close
end

#command(*args) ⇒ Object

generic commands



36
37
38
39
# File 'lib/mpv-slave/mpv.rb', line 36

def command *args
  response = @socket.command(*args)
  response
end

#disable_event(event) ⇒ Object



83
84
85
# File 'lib/mpv-slave/mpv.rb', line 83

def disable_event event
  command "disable_event", event
end

#disable_eventsObject



91
92
93
# File 'lib/mpv-slave/mpv.rb', line 91

def disable_events
  disable_event "all"
end

#enable_event(event) ⇒ Object



79
80
81
# File 'lib/mpv-slave/mpv.rb', line 79

def enable_event event
  command "enable_event", event
end

#enable_eventsObject



87
88
89
# File 'lib/mpv-slave/mpv.rb', line 87

def enable_events
  enable_event "all"
end

#get(prop) ⇒ Object



57
58
59
# File 'lib/mpv-slave/mpv.rb', line 57

def get prop
  command "get_property", prop
end

#pauseObject



53
54
55
# File 'lib/mpv-slave/mpv.rb', line 53

def pause
  toggle "pause"
end

#play(file, mode: "replace", wait: true) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mpv-slave/mpv.rb', line 41

def play file, mode: "replace", wait: true
  file = File.expand_path file
  raise "no such file: #{file}" unless File.exists? file
  command "loadfile", file, mode

  wait_for "playback-restart" if wait
end

#running?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/mpv-slave/mpv.rb', line 27

def running?
  begin
    Process.waitpid(@pid, Process::WNOHANG) == nil
  rescue
    false
  end
end

#set(prop, value) ⇒ Object



61
62
63
# File 'lib/mpv-slave/mpv.rb', line 61

def set prop, value
  command "set_property", prop, value
end

#toggle(prop) ⇒ Object



65
66
67
# File 'lib/mpv-slave/mpv.rb', line 65

def toggle prop
  set(prop, !get(prop))
end

#wait_for(event) ⇒ Object



69
70
71
# File 'lib/mpv-slave/mpv.rb', line 69

def wait_for event
  @socket.wait_for event
end

#wait_while(prop, val = true) ⇒ Object



73
74
75
76
77
# File 'lib/mpv-slave/mpv.rb', line 73

def wait_while prop, val=true
  while get(prop) == val
    sleep 0.01
  end
end