Class: Tucue::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/tucue/player.rb

Overview

Playback engine that controls mpv over –input-ipc-server.

Spawns mpv as a child process and talks to it through a Unix domain socket, sending JSON IPC commands to seek and read the playback position. mpv.io/manual/stable/#json-ipc

Defined Under Namespace

Classes: Error

Constant Summary collapse

SOCKET_TIMEOUT =

Max seconds to wait for the IPC socket to appear.

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: []) ⇒ Player

Returns a new instance of Player.



19
20
21
22
23
24
25
26
27
28
# File 'lib/tucue/player.rb', line 19

def initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: [])
  @file = file
  @socket_path = socket_path
  @start_at = start_at
  @extra_args = extra_args
  @pid = nil
  @socket = nil
  @request_id = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



30
31
32
# File 'lib/tucue/player.rb', line 30

def file
  @file
end

Instance Method Details

#durationObject

Total duration in seconds, or nil if unavailable.



80
81
82
# File 'lib/tucue/player.rb', line 80

def duration
  get_property("duration")
end

#pauseObject



61
62
63
# File 'lib/tucue/player.rb', line 61

def pause
  set_property("pause", true)
end

#paused?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/tucue/player.rb', line 65

def paused?
  get_property("pause") == true
end

#playObject



57
58
59
# File 'lib/tucue/player.rb', line 57

def play
  set_property("pause", false)
end

#seek(seconds) ⇒ Object

Seek relative to the current position (seconds; negative rewinds).



70
71
72
# File 'lib/tucue/player.rb', line 70

def seek(seconds)
  command("seek", seconds, "relative")
end

#startObject

Launch mpv and connect to its IPC socket.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tucue/player.rb', line 33

def start
  raise Error, "mpv not found in PATH" unless mpv_available?

  File.unlink(@socket_path) if File.exist?(@socket_path)

  @pid = spawn(
    "mpv",
    "--no-video",
    "--no-terminal",
    "--input-ipc-server=#{@socket_path}",
    *(@start_at ? ["--start=#{@start_at}"] : []),
    *@extra_args,
    @file
  )

  connect
  self
end

#stopObject

Quit mpv and close the socket.



85
86
87
88
89
90
91
# File 'lib/tucue/player.rb', line 85

def stop
  command("quit") if @socket
rescue Error
  # Already gone; ignore.
ensure
  close
end

#time_posObject

Current playback position in seconds, or nil if unavailable.



75
76
77
# File 'lib/tucue/player.rb', line 75

def time_pos
  get_property("time-pos")
end

#toggle_pauseObject

Toggle between playing and paused.



53
54
55
# File 'lib/tucue/player.rb', line 53

def toggle_pause
  command("cycle", "pause")
end