From ba6a1df423fd1d91bb4a105af86bf4278bdb3a36 Mon Sep 17 00:00:00 2001 From: Kacper Kasper Date: Sun, 17 Oct 2021 12:35:47 +0200 Subject: [PATCH] MediaPlayer: cache file duration in memory * SetAttribute() will fail for read-only volumes. * Duration() is called in playlist's Draw() method. Since _CalculateDuration() is very expensive because it parses file metadata, it causes visible drawing glitches and playlist is almost unusable. * Mitigates #15221. I don't consider this a fix because first draw is still going to block for a long time, but at least scrolling works smoothly now. Ideally, file metadata would be parsed asynchronously in a separate thread and drawn as it becomes available. Change-Id: If198d61c77a7746bcc0e19b7caeed89ce829c247 Reviewed-on: https://review.haiku-os.org/c/haiku/+/4597 Reviewed-by: Adrien Destugues Tested-by: Commit checker robot --- src/apps/mediaplayer/playlist/PlaylistItem.cpp | 13 ++++++++++--- src/apps/mediaplayer/playlist/PlaylistItem.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.cpp b/src/apps/mediaplayer/playlist/PlaylistItem.cpp index bb96c22f90..c68b1b623a 100644 --- a/src/apps/mediaplayer/playlist/PlaylistItem.cpp +++ b/src/apps/mediaplayer/playlist/PlaylistItem.cpp @@ -45,7 +45,8 @@ static vint32 sInstanceCount = 0; PlaylistItem::PlaylistItem() : fPlaybackFailed(false), - fTrackSupplier(NULL) + fTrackSupplier(NULL), + fDuration(-1) { #ifdef DEBUG_INSTANCE_COUNT atomic_add(&sInstanceCount, 1); @@ -143,8 +144,14 @@ PlaylistItem::Duration() { bigtime_t duration; if (GetAttribute(ATTR_INT64_DURATION, duration) != B_OK) { - duration = this->_CalculateDuration(); - SetAttribute(ATTR_INT64_DURATION, duration); + if (fDuration == -1) { + duration = this->_CalculateDuration(); + if (SetAttribute(ATTR_INT64_DURATION, duration) != B_OK) { + fDuration = duration; + } + } else { + duration = fDuration; + } } return duration; diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.h b/src/apps/mediaplayer/playlist/PlaylistItem.h index 3b1a4bb067..d3abe96f12 100644 --- a/src/apps/mediaplayer/playlist/PlaylistItem.h +++ b/src/apps/mediaplayer/playlist/PlaylistItem.h @@ -127,6 +127,7 @@ private: BList fListeners; bool fPlaybackFailed; TrackSupplier* fTrackSupplier; + bigtime_t fDuration; }; typedef BReference PlaylistItemRef;