Class: Muzak::Player::MPV

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

Overview

Exposes MPV's IPC to muzak for playback control.

Constant Summary collapse

DEFAULT_MPV_ARGS =
[
  "--no-osc",
  "--no-osd-bar",
  "--no-input-default-bindings",
  "--no-input-cursor",
  "--load-scripts=no", # autoload and other scripts clobber our mpv management
].freeze

Instance Attribute Summary

Attributes inherited from StubPlayer

#instance

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StubPlayer

#initialize, player_name

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Constructor Details

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

Class Method Details

.available?Boolean

Returns whether or not MPV is available for execution.

Returns:

  • (Boolean)

    whether or not MPV is available for execution


22
23
24
# File 'lib/muzak/player/mpv.rb', line 22

def self.available?
  ::MPV::Server.available?
end

Instance Method Details

#activate!void

This method returns an undefined value.

Activate mpv by executing it and preparing for event processing.


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/muzak/player/mpv.rb', line 33

def activate!
  return if running?

  debug "activating #{self.class}"

  args = DEFAULT_MPV_ARGS + configured_mpv_args

  @mpv = ::MPV::Session.new(user_args: args)
  @mpv.callbacks << method(:dispatch_event!)

  instance.event :player_activated
end

#clear_queuevoid

This method returns an undefined value.

Clears mpv's internal queue.


160
161
162
163
# File 'lib/muzak/player/mpv.rb', line 160

def clear_queue
  return unless running?
  @mpv.command "stop"
end

#configured_mpv_argsObject


171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/muzak/player/mpv.rb', line 171

def configured_mpv_args
  args = []

  args.concat ["--no-force-window", "--no-video"] if Config.mpv_no_art

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

  # this is an experimental flag, but it could improve
  # muzak's load times substantially when used with a network
  # mounted music library
  args << "--prefetch-playlist" if ::MPV::Server.flag?("--prefetch-playlist")

  args
end

#deactivate!void

This method returns an undefined value.

Deactivate mpv by killing it and cleaning up.


48
49
50
51
52
53
54
55
56
57
# File 'lib/muzak/player/mpv.rb', line 48

def deactivate!
  return unless running?

  debug "deactivating #{self.class}"

  @mpv.quit!
ensure
  @_now_playing = nil
  instance.event :player_deactivated
end

#dispatch_event!(event) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Dispatch the given event to the active Instance.

Parameters:

  • event (String)

    the event


202
203
204
205
206
207
208
209
210
# File 'lib/muzak/player/mpv.rb', line 202

def dispatch_event!(event)
  case event
  when "file-loaded"
    instance.event :song_loaded, now_playing
  when "end-file"
    instance.event :song_unloaded
    @_now_playing = nil
  end
end

#enqueue_album(album) ⇒ void

Note:

Activates mpv if not already activated.

This method returns an undefined value.

Tell mpv to add the given album to its queue.

Parameters:

  • album (Album)

    the album to add


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

def enqueue_album(album)
  activate! unless running?

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

#enqueue_playlist(playlist) ⇒ void

Note:

Activates mpv if not already activated.

This method returns an undefined value.

Tell mpv to add the given playlist to its queue.

Parameters:

  • playlist (Playlist)

    the playlist to add


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

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) ⇒ void

Note:

Activates mpv if not already activated.

This method returns an undefined value.

Tell mpv to add the given song to its queue.

Parameters:

  • song (Song)

    the song to add


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_queueArray<Song>

Note:

This includes songs already played.

Get mpv's internal queue.

Returns:

  • (Array<Song>)

    all songs in mpv's queue


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/muzak/player/mpv.rb', line 135

def list_queue
  entries = @mpv.get_property "playlist/count"

  playlist = []

  entries.times do |i|
    # TODO: this is slow and should be avoided at all costs,
    # since we have access to these Song instances earlier
    # in the object's lifecycle.
    playlist << Song.new(@mpv.get_property("playlist/#{i}/filename"))
  end

  playlist
end

#load_song(song, art) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Load a song and optional album art into mpv.

Parameters:

  • song (Song)

    the song to load

  • art (String)

    the art file to load


191
192
193
194
195
196
# File 'lib/muzak/player/mpv.rb', line 191

def load_song(song, art)
  append_type = Config.autoplay ? "append-play" : "append"
  cmds = ["loadfile", song.path, append_type]
  cmds << "external-file=\"#{art}\"" if art
  @mpv.command(*cmds)
end

#next_songvoid

Note:

Does nothing if the current song is the last.

This method returns an undefined value.

Tell mpv to play the next song in its queue.


87
88
89
# File 'lib/muzak/player/mpv.rb', line 87

def next_song
  @mpv.command "playlist-next"
end

#now_playingSong?

Get mpv's currently loaded song.

Returns:

  • (Song, nil)

    the currently loaded song


167
168
169
# File 'lib/muzak/player/mpv.rb', line 167

def now_playing
  @_now_playing ||= Song.new @mpv.get_property("path")
end

#pausevoid

Note:

Does nothing is playback is already paused.

This method returns an undefined value.

Tell mpv to pause playback.


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

def pause
  return unless running?

  @mpv.set_property "pause", true
end

#playvoid

Note:

Does nothing is playback is already in progress.

This method returns an undefined value.

Tell mpv to begin playback.


62
63
64
65
66
# File 'lib/muzak/player/mpv.rb', line 62

def play
  return unless running?

  @mpv.set_property "pause", false
end

#playing?Boolean

Returns Whether or not mpv is currently playing.

Returns:

  • (Boolean)

    Whether or not mpv is currently playing.


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

def playing?
  return false unless running?

  !@mpv.get_property "pause"
end

#previous_songvoid

Note:

Does nothing if the current song is the first.

This method returns an undefined value.

Tell mpv to play the previous song in its queue.


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

def previous_song
  @mpv.command "playlist-prev"
end

#running?Boolean

Returns whether or not the current instance is running.

Returns:

  • (Boolean)

    whether or not the current instance is running.


27
28
29
# File 'lib/muzak/player/mpv.rb', line 27

def running?
  !!@mpv&.running?
end

#shuffle_queuevoid

This method returns an undefined value.

Shuffle mpv's internal queue.


152
153
154
155
156
# File 'lib/muzak/player/mpv.rb', line 152

def shuffle_queue
  return unless running?

  @mpv.command "playlist-shuffle"
end