Fixed performance problems with mediaSessionManager

* Built on top of Redirons changes
* MediaSessionManager-Metadata get's only update when metadata changes - not every second as it only changes when the metadata changes
* Reworked mediasessionmanager
This commit is contained in:
litetex 2021-09-24 23:40:12 +02:00
parent b2e2551e33
commit c0d41661e8
2 changed files with 106 additions and 66 deletions

View File

@ -140,6 +140,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.core.Observable;
@ -1622,16 +1623,6 @@ public final class Player implements
if (isQueueVisible) { if (isQueueVisible) {
updateQueueTime(currentProgress); updateQueueTime(currentProgress);
} }
final boolean showThumbnail = prefs.getBoolean(
context.getString(R.string.show_thumbnail_key), true);
// setMetadata only updates the metadata when any of the metadata keys are null
if (showThumbnail) {
mediaSessionManager.setMetadata(getVideoTitle(), getUploaderName(), getThumbnail(),
duration);
} else {
mediaSessionManager.setMetadata(getVideoTitle(), getUploaderName(), duration);
}
} }
private void startProgressLoop() { private void startProgressLoop() {
@ -2950,6 +2941,16 @@ public final class Player implements
tag.getMetadata().getPreviewFrames()); tag.getMetadata().getPreviewFrames());
NotificationUtil.getInstance().createNotificationIfNeededAndUpdate(this, false); NotificationUtil.getInstance().createNotificationIfNeededAndUpdate(this, false);
final boolean showThumbnail = prefs.getBoolean(
context.getString(R.string.show_thumbnail_key), true);
mediaSessionManager.setMetadata(
getVideoTitle(),
getUploaderName(),
showThumbnail ? Optional.ofNullable(getThumbnail()) : Optional.empty(),
tag.getMetadata().getDuration()
);
notifyMetadataUpdateToListeners(); notifyMetadataUpdateToListeners();
if (areSegmentsVisible) { if (areSegmentsVisible) {

View File

@ -21,6 +21,8 @@ import org.schabi.newpipe.player.mediasession.MediaSessionCallback;
import org.schabi.newpipe.player.mediasession.PlayQueueNavigator; import org.schabi.newpipe.player.mediasession.PlayQueueNavigator;
import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController; import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController;
import java.util.Optional;
public class MediaSessionManager { public class MediaSessionManager {
private static final String TAG = MediaSessionManager.class.getSimpleName(); private static final String TAG = MediaSessionManager.class.getSimpleName();
public static final boolean DEBUG = MainActivity.DEBUG; public static final boolean DEBUG = MainActivity.DEBUG;
@ -30,6 +32,9 @@ public class MediaSessionManager {
@NonNull @NonNull
private final MediaSessionConnector sessionConnector; private final MediaSessionConnector sessionConnector;
private int lastTitleHashCode;
private int lastArtistHashCode;
private long lastDuration;
private int lastAlbumArtHashCode; private int lastAlbumArtHashCode;
public MediaSessionManager(@NonNull final Context context, public MediaSessionManager(@NonNull final Context context,
@ -65,80 +70,114 @@ public class MediaSessionManager {
return mediaSession.getSessionToken(); return mediaSession.getSessionToken();
} }
public void setMetadata(final String title, public void setMetadata(@NonNull final String title,
final String artist, @NonNull final String artist,
final long duration) { @NonNull final Optional<Bitmap> optAlbumArt,
final long duration
) {
if (DEBUG) {
Log.d(TAG, "setMetadata called:"
+ " t: " + title
+ " a: " + artist
+ " thumb: " + (
optAlbumArt.isPresent()
? optAlbumArt.get().hashCode()
: "<none>")
+ " d: " + duration);
}
if (!mediaSession.isActive()) { if (!mediaSession.isActive()) {
if (DEBUG) {
Log.d(TAG, "setMetadata: mediaSession not active - exiting");
}
return; return;
} }
if (DEBUG) { if (checkIfMetadataShouldBeSet(title, artist, optAlbumArt, duration)) {
if (getMetadataTitle() == null) {
Log.d(TAG, "N_getMetadataTitle: title == null");
}
if (getMetadataArtist() == null) {
Log.d(TAG, "N_getMetadataArtist: artist == null");
}
if (getMetadataDuration() <= 1) {
Log.d(TAG, "N_getMetadataDuration: duration <= 1; " + getMetadataDuration());
}
}
if (getMetadataTitle() == null || getMetadataArtist() == null || getMetadataDuration() <= 1
|| !getMetadataTitle().equals(title)) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "setMetadata: N_Metadata update: t: " + title + " a: " + artist Log.d(TAG, "setMetadata: N_Metadata update:"
+ " t: " + title
+ " a: " + artist
+ " thumb: " + (
optAlbumArt.isPresent()
? optAlbumArt.get().hashCode()
: "<none>")
+ " d: " + duration); + " d: " + duration);
} }
mediaSession.setMetadata(new MediaMetadataCompat.Builder() final MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist) .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration).build()); .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
if (optAlbumArt.isPresent()) {
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, optAlbumArt.get());
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, optAlbumArt.get());
}
mediaSession.setMetadata(builder.build());
lastTitleHashCode = title.hashCode();
lastArtistHashCode = artist.hashCode();
lastDuration = duration;
if (optAlbumArt.isPresent()) {
lastAlbumArtHashCode = optAlbumArt.get().hashCode();
}
} }
} }
public void setMetadata(final String title, private boolean checkIfMetadataShouldBeSet(
final String artist, @NonNull final String title,
final Bitmap albumArt, @NonNull final String artist,
final long duration) { @NonNull final Optional<Bitmap> optAlbumArt,
if (albumArt == null || !mediaSession.isActive()) { final long duration
return; ) {
} // Check if the values have changed since the last time
if (title.hashCode() != lastTitleHashCode
if (DEBUG) { || artist.hashCode() != lastArtistHashCode
if (getMetadataAlbumArt() == null) { || duration != lastDuration
Log.d(TAG, "N_getMetadataAlbumArt: thumb == null"); || (optAlbumArt.isPresent() && optAlbumArt.get().hashCode() != lastAlbumArtHashCode)
} ) {
if (getMetadataTitle() == null) {
Log.d(TAG, "N_getMetadataTitle: title == null");
}
if (getMetadataArtist() == null) {
Log.d(TAG, "N_getMetadataArtist: artist == null");
}
if (getMetadataDuration() <= 1) {
Log.d(TAG, "N_getMetadataDuration: duration <= 1; " + getMetadataDuration());
}
}
if (getMetadataAlbumArt() == null || getMetadataTitle() == null
|| getMetadataArtist() == null || getMetadataDuration() <= 1
|| albumArt.hashCode() != lastAlbumArtHashCode) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "setMetadata: N_Metadata update: t: " + title + " a: " + artist Log.d(TAG,
+ " thumb: " + albumArt.hashCode() + " d: " + duration); "checkIfMetadataShouldBeSet: true - reason: changed values since last");
} }
return true;
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt)
.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, albumArt)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration).build());
lastAlbumArtHashCode = albumArt.hashCode();
} }
// Check if the current metadata is valid
if (getMetadataTitle() == null
|| getMetadataArtist() == null
|| getMetadataDuration() <= 1
) {
if (DEBUG) {
if (getMetadataTitle() == null) {
Log.d(TAG,
"N_getMetadataTitle: title == null");
} else if (getMetadataArtist() == null) {
Log.d(TAG,
"N_getMetadataArtist: artist == null");
} else if (getMetadataDuration() <= 1) {
Log.d(TAG,
"N_getMetadataDuration: duration <= 1; " + getMetadataDuration());
}
}
return true;
}
// If we got an album art check if the current set AlbumArt is null
if (optAlbumArt.isPresent() && getMetadataAlbumArt() == null) {
if (DEBUG) {
if (getMetadataAlbumArt() == null) {
Log.d(TAG, "N_getMetadataAlbumArt: thumb == null");
}
}
return true;
}
// Default - no update required
return false;
} }
private Bitmap getMetadataAlbumArt() { private Bitmap getMetadataAlbumArt() {
return mediaSession.getController().getMetadata() return mediaSession.getController().getMetadata()
.getBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART); .getBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART);