Class: Muzak::Player::MPV

Inherits:
StubPlayer show all
Defined in:
lib/muzak/player/mpv.rb

Instance Attribute Summary

Attributes inherited from StubPlayer

#instance

Instance Method Summary collapse

Methods inherited from StubPlayer

#initialize

Methods included from Utils

#album_art?, #debug, #debug?, #error, #fail_arity, #info, #music?, #output, #pretty, resolve_command, resolve_method, #verbose, #verbose?, #warn, #warn_arity

Constructor Details

This class inherits a constructor from Muzak::Player::StubPlayer

Instance Method Details

#activate!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/muzak/player/mpv.rb', line 17

def activate!
  return if running?

  debug "activating #{self.class}"

  @sock_path = Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock")
  mpv_args = [
    "--idle",
    # if i get around to separating album art from playback,
    # these two flags disable mpv's video output entirely
    # "--no-force-window",
    # "--no-video",
    "--no-osc",
    "--no-osd-bar",
    "--no-input-default-bindings",
    "--no-input-cursor",
    "--no-terminal",
    "--load-scripts=no", # autoload and other scripts with clobber our mpv management
    "--input-ipc-server=%{socket}" % { socket: @sock_path }
  ]

  mpv_args << "--geometry=#{Config.art_geometry}" if Config.art_geometry

  @pid = Process.spawn("mpv", *mpv_args)

  until File.exists?(@sock_path)
    sleep 0.1
  end

  @socket = UNIXSocket.new(@sock_path)

  @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! }

  instance.event :player_activated
end

#clear_queueObject



142
143
144
145
146
# File 'lib/muzak/player/mpv.rb', line 142

def clear_queue
  return unless running?

  command "playlist-clear"
end

#deactivate!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/muzak/player/mpv.rb', line 59

def deactivate!
  return unless running?

  debug "deactivating #{self.class}"

  command "quit"

  Process.kill :TERM, @pid
  Process.wait @pid
  @pid = nil

  @socket.close
ensure
  instance.event :player_deactivated
  File.delete(@sock_path) if @sock_path && File.exists?(@sock_path)
end

#enqueue_album(album) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/muzak/player/mpv.rb', line 108

def enqueue_album(album)
  activate! unless running?

  album.songs.each do |song|
    load_song song, album.cover_art
  end
end

#enqueue_playlist(playlist) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/muzak/player/mpv.rb', line 116

def enqueue_playlist(playlist)
  activate! unless running?

  playlist.songs.each do |song|
    load_song song, song.best_guess_album_art
  end
end

#enqueue_song(song) ⇒ Object



102
103
104
105
106
# File 'lib/muzak/player/mpv.rb', line 102

def enqueue_song(song)
  activate! unless running?

  load_song song, song.best_guess_album_art
end

#list_queueObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/muzak/player/mpv.rb', line 124

def list_queue
  entries = get_property "playlist/count"

  playlist = []

  entries.times do |i|
    playlist << Song.new(get_property("playlist/#{i}/filename"))
  end

  playlist
end

#next_songObject



94
95
96
# File 'lib/muzak/player/mpv.rb', line 94

def next_song
  command "playlist-next"
end

#now_playingObject



148
149
150
151
152
# File 'lib/muzak/player/mpv.rb', line 148

def now_playing
  return unless running? && playing?

  Song.new(get_property "path")
end

#pauseObject



82
83
84
85
86
# File 'lib/muzak/player/mpv.rb', line 82

def pause
  return unless running?

  set_property "pause", true
end

#playObject



76
77
78
79
80
# File 'lib/muzak/player/mpv.rb', line 76

def play
  return unless running?

  set_property "pause", false
end

#playing?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/muzak/player/mpv.rb', line 88

def playing?
  return false unless running?

  !get_property "pause"
end

#previous_songObject



98
99
100
# File 'lib/muzak/player/mpv.rb', line 98

def previous_song
  command "playlist-prev"
end

#running?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/muzak/player/mpv.rb', line 9

def running?
  begin
    !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil?
  rescue Errno::ECHILD
    false
  end
end

#shuffle_queueObject



136
137
138
139
140
# File 'lib/muzak/player/mpv.rb', line 136

def shuffle_queue
  return unless running?

  command "playlist-shuffle"
end