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,79 +70,113 @@ 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,
if (!mediaSession.isActive()) { final long duration
return; ) {
}
if (DEBUG) { if (DEBUG) {
if (getMetadataTitle() == null) { Log.d(TAG, "setMetadata called:"
Log.d(TAG, "N_getMetadataTitle: title == null"); + " t: " + title
} + " a: " + artist
if (getMetadataArtist() == null) { + " thumb: " + (
Log.d(TAG, "N_getMetadataArtist: artist == null"); optAlbumArt.isPresent()
} ? optAlbumArt.get().hashCode()
if (getMetadataDuration() <= 1) { : "<none>")
Log.d(TAG, "N_getMetadataDuration: duration <= 1; " + getMetadataDuration());
}
}
if (getMetadataTitle() == null || getMetadataArtist() == null || getMetadataDuration() <= 1
|| !getMetadataTitle().equals(title)) {
if (DEBUG) {
Log.d(TAG, "setMetadata: N_Metadata update: t: " + title + " a: " + artist
+ " d: " + duration); + " d: " + duration);
} }
mediaSession.setMetadata(new MediaMetadataCompat.Builder() if (!mediaSession.isActive()) {
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title) if (DEBUG) {
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist) Log.d(TAG, "setMetadata: mediaSession not active - exiting");
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration).build());
} }
}
public void setMetadata(final String title,
final String artist,
final Bitmap albumArt,
final long duration) {
if (albumArt == null || !mediaSession.isActive()) {
return; return;
} }
if (checkIfMetadataShouldBeSet(title, artist, optAlbumArt, duration)) {
if (DEBUG) {
Log.d(TAG, "setMetadata: N_Metadata update:"
+ " t: " + title
+ " a: " + artist
+ " thumb: " + (
optAlbumArt.isPresent()
? optAlbumArt.get().hashCode()
: "<none>")
+ " d: " + duration);
}
final MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
.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();
}
}
}
private boolean checkIfMetadataShouldBeSet(
@NonNull final String title,
@NonNull final String artist,
@NonNull final Optional<Bitmap> optAlbumArt,
final long duration
) {
// Check if the values have changed since the last time
if (title.hashCode() != lastTitleHashCode
|| artist.hashCode() != lastArtistHashCode
|| duration != lastDuration
|| (optAlbumArt.isPresent() && optAlbumArt.get().hashCode() != lastAlbumArtHashCode)
) {
if (DEBUG) {
Log.d(TAG,
"checkIfMetadataShouldBeSet: true - reason: changed values since last");
}
return true;
}
// 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 (DEBUG) {
if (getMetadataAlbumArt() == null) { if (getMetadataAlbumArt() == null) {
Log.d(TAG, "N_getMetadataAlbumArt: thumb == null"); Log.d(TAG, "N_getMetadataAlbumArt: thumb == null");
} }
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());
} }
return true;
} }
if (getMetadataAlbumArt() == null || getMetadataTitle() == null // Default - no update required
|| getMetadataArtist() == null || getMetadataDuration() <= 1 return false;
|| albumArt.hashCode() != lastAlbumArtHashCode) {
if (DEBUG) {
Log.d(TAG, "setMetadata: N_Metadata update: t: " + title + " a: " + artist
+ " thumb: " + albumArt.hashCode() + " d: " + duration);
} }
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();
}
}
private Bitmap getMetadataAlbumArt() { private Bitmap getMetadataAlbumArt() {
return mediaSession.getController().getMetadata() return mediaSession.getController().getMetadata()