Added prefetch logic
This commit is contained in:
@@ -444,11 +444,13 @@ class LibremsonicApp(Gtk.Application):
|
||||
self.state.song_progress = 0
|
||||
|
||||
def on_song_download_complete(_):
|
||||
# This will now return the local media.
|
||||
downloaded_filename = CacheManager.get_song_filename_or_stream(
|
||||
song)[0]
|
||||
self.player.play_media(downloaded_filename,
|
||||
self.state.song_progress, song)
|
||||
# Switch to the local media if the player can hotswap (MPV can,
|
||||
# Chromecast cannot hotswap without lag).
|
||||
if self.player.can_hotswap_source:
|
||||
downloaded_filename = (
|
||||
CacheManager.get_song_filename_or_stream(song)[0])
|
||||
self.player.play_media(downloaded_filename,
|
||||
self.state.song_progress, song)
|
||||
GLib.idle_add(self.update_window)
|
||||
|
||||
# If streaming, also download the song, unless configured not to,
|
||||
@@ -470,6 +472,23 @@ class LibremsonicApp(Gtk.Application):
|
||||
self.state.play_queue = play_queue
|
||||
self.save_play_queue()
|
||||
|
||||
# Prefetch songs
|
||||
if self.state.repeat_type != RepeatType.REPEAT_SONG:
|
||||
song_idx = self.state.play_queue.index(song.id)
|
||||
prefetch_idxs = []
|
||||
for i in range(self.state.config.prefetch_amount):
|
||||
prefetch_idx = song_idx + 1 + i
|
||||
play_queue_len = len(self.state.play_queue)
|
||||
if (prefetch_idx < play_queue_len or
|
||||
self.state.repeat_type == RepeatType.REPEAT_QUEUE):
|
||||
prefetch_idxs.append(prefetch_idx % play_queue_len)
|
||||
CacheManager.batch_download_songs(
|
||||
[self.state.play_queue[i] for i in prefetch_idxs],
|
||||
before_download=lambda: GLib.idle_add(self.update_window),
|
||||
on_song_download_complete=lambda _: GLib.idle_add(
|
||||
self.update_window),
|
||||
)
|
||||
|
||||
song_details_future = CacheManager.get_song_details(song)
|
||||
song_details_future.add_done_callback(
|
||||
lambda f: GLib.idle_add(do_play_song, f.result()), )
|
||||
|
@@ -53,7 +53,7 @@ class ArtistsPanel(Gtk.Paned):
|
||||
self.pack2(self.artist_detail_panel, True, False)
|
||||
|
||||
def update(self, state: ApplicationState):
|
||||
self.artist_list.update()
|
||||
self.artist_list.update(state)
|
||||
if self.artist_id:
|
||||
self.artist_detail_panel.update(self.artist_id)
|
||||
|
||||
@@ -99,7 +99,7 @@ class ArtistList(Gtk.Box):
|
||||
list_scroll_window.add(self.list)
|
||||
self.pack_start(list_scroll_window, True, True, 0)
|
||||
|
||||
def update(self, force=False):
|
||||
def update(self, state=None, force=False):
|
||||
self.update_list(force=force)
|
||||
|
||||
@util.async_callback(
|
||||
|
@@ -48,6 +48,10 @@ class Player:
|
||||
def song_loaded(self):
|
||||
return self._song_loaded
|
||||
|
||||
@property
|
||||
def can_hotswap_source(self):
|
||||
return self._can_hotswap_source
|
||||
|
||||
@property
|
||||
def volume(self):
|
||||
return self._get_volume()
|
||||
@@ -104,6 +108,7 @@ class MPVPlayer(Player):
|
||||
self.mpv = mpv.MPV()
|
||||
self.progress_value_lock = threading.Lock()
|
||||
self.progress_value_count = 0
|
||||
self._can_hotswap_source = True
|
||||
|
||||
@self.mpv.property_observer('time-pos')
|
||||
def time_observer(_name, value):
|
||||
@@ -229,6 +234,8 @@ class ChromecastPlayer(Player):
|
||||
super().__init__(*args)
|
||||
self._timepos = None
|
||||
self.time_incrementor_running = False
|
||||
self._can_hotswap_source = False
|
||||
|
||||
ChromecastPlayer.cast_status_listener.on_new_cast_status = self.on_new_cast_status
|
||||
ChromecastPlayer.media_status_listener.on_new_media_status = self.on_new_media_status
|
||||
|
||||
|
@@ -234,7 +234,6 @@ class PlaylistsPanel(Gtk.Paned):
|
||||
|
||||
self.playlist_songs = Gtk.TreeView(
|
||||
model=self.playlist_song_model,
|
||||
headers_visible=False, # TODO use the config value for this
|
||||
reorderable=True,
|
||||
margin_top=15,
|
||||
enable_search=True,
|
||||
@@ -460,6 +459,8 @@ class PlaylistsPanel(Gtk.Paned):
|
||||
else:
|
||||
self.playlist_action_buttons.hide()
|
||||
|
||||
self.playlist_songs.set_headers_visible(state.config.show_headers)
|
||||
|
||||
def set_playlist_list_loading(self, loading_status):
|
||||
if loading_status:
|
||||
self.playlist_list_loading.show()
|
||||
|
Reference in New Issue
Block a user