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
SPEED_STEPS =

Selectable playback speeds, from half to double, centered on 1.0.

[0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0].freeze
DEFAULT_SPEED_INDEX =
SPEED_STEPS.index(1.0)

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.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tucue/player.rb', line 23

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
  @speed_index = DEFAULT_SPEED_INDEX
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



35
36
37
# File 'lib/tucue/player.rb', line 35

def file
  @file
end

Instance Method Details

#durationObject

Total duration in seconds, or nil if unavailable.



108
109
110
# File 'lib/tucue/player.rb', line 108

def duration
  get_property("duration")
end

#pauseObject



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

def pause
  set_property("pause", true)
end

#paused?Boolean

Returns:

  • (Boolean)


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

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

#playObject



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

def play
  set_property("pause", false)
end

#reset_speedObject

Reset the speed back to 1.0.



97
98
99
100
# File 'lib/tucue/player.rb', line 97

def reset_speed
  @speed_index = DEFAULT_SPEED_INDEX
  apply_speed
end

#seek(seconds) ⇒ Object

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



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

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

#speedObject

Current playback speed multiplier (e.g. 1.0, 1.25).



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

def speed
  SPEED_STEPS[@speed_index]
end

#speed_downObject

Step the speed down one notch (capped at the slowest step).



91
92
93
94
# File 'lib/tucue/player.rb', line 91

def speed_down
  @speed_index = [@speed_index - 1, 0].max
  apply_speed
end

#speed_upObject

Step the speed up one notch (capped at the fastest step).



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

def speed_up
  @speed_index = [@speed_index + 1, SPEED_STEPS.size - 1].min
  apply_speed
end

#startObject

Launch mpv and connect to its IPC socket.

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tucue/player.rb', line 38

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.



113
114
115
116
117
118
119
# File 'lib/tucue/player.rb', line 113

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.



103
104
105
# File 'lib/tucue/player.rb', line 103

def time_pos
  get_property("time-pos")
end

#toggle_pauseObject

Toggle between playing and paused.



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

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