Class: Tucue::Player
- Inherits:
-
Object
- Object
- Tucue::Player
- 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
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#duration ⇒ Object
Total duration in seconds, or nil if unavailable.
-
#initialize(file, socket_path: "/tmp/tucue.sock", start_at: nil, extra_args: []) ⇒ Player
constructor
A new instance of Player.
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #play ⇒ Object
-
#reset_speed ⇒ Object
Reset the speed back to 1.0.
-
#seek(seconds) ⇒ Object
Seek relative to the current position (seconds; negative rewinds).
-
#speed ⇒ Object
Current playback speed multiplier (e.g. 1.0, 1.25).
-
#speed_down ⇒ Object
Step the speed down one notch (capped at the slowest step).
-
#speed_up ⇒ Object
Step the speed up one notch (capped at the fastest step).
-
#start ⇒ Object
Launch mpv and connect to its IPC socket.
-
#stop ⇒ Object
Quit mpv and close the socket.
-
#time_pos ⇒ Object
Current playback position in seconds, or nil if unavailable.
-
#toggle_pause ⇒ Object
Toggle between playing and paused.
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
35 36 37 |
# File 'lib/tucue/player.rb', line 35 def file @file end |
Instance Method Details
#duration ⇒ Object
Total duration in seconds, or nil if unavailable.
108 109 110 |
# File 'lib/tucue/player.rb', line 108 def duration get_property("duration") end |
#pause ⇒ Object
66 67 68 |
# File 'lib/tucue/player.rb', line 66 def pause set_property("pause", true) end |
#paused? ⇒ Boolean
70 71 72 |
# File 'lib/tucue/player.rb', line 70 def paused? get_property("pause") == true end |
#play ⇒ Object
62 63 64 |
# File 'lib/tucue/player.rb', line 62 def play set_property("pause", false) end |
#reset_speed ⇒ Object
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 |
#speed ⇒ Object
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_down ⇒ Object
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_up ⇒ Object
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 |
#start ⇒ Object
Launch mpv and connect to its IPC socket.
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 |
#stop ⇒ Object
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_pos ⇒ Object
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_pause ⇒ Object
Toggle between playing and paused.
58 59 60 |
# File 'lib/tucue/player.rb', line 58 def toggle_pause command("cycle", "pause") end |