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, #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
# 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",
    "--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=#{instance.config["art-geometry"]}" if instance.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



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

def clear_queue
  command "playlist-clear"
end

#deactivate!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/muzak/player/mpv.rb', line 55

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



104
105
106
107
108
109
110
# File 'lib/muzak/player/mpv.rb', line 104

def enqueue_album(album)
  activate! unless running?

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

#enqueue_playlist(playlist) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/muzak/player/mpv.rb', line 112

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



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

def enqueue_song(song)
  activate! unless running?

  load_song song, song.best_guess_album_art
end

#list_queueObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/muzak/player/mpv.rb', line 120

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



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

def next_song
  command "playlist-next"
end

#now_playingObject



140
141
142
# File 'lib/muzak/player/mpv.rb', line 140

def now_playing
  Song.new(get_property "path")
end

#pauseObject



78
79
80
81
82
# File 'lib/muzak/player/mpv.rb', line 78

def pause
  return unless running?

  set_property "pause", true
end

#playObject



72
73
74
75
76
# File 'lib/muzak/player/mpv.rb', line 72

def play
  return unless running?

  set_property "pause", false
end

#playing?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/muzak/player/mpv.rb', line 84

def playing?
  return false unless running?

  !get_property "pause"
end

#previous_songObject



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

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



132
133
134
# File 'lib/muzak/player/mpv.rb', line 132

def shuffle_queue
  command "playlist-shuffle"
end