Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes related to MediasessionManager #7166

Merged

Conversation

litetex
Copy link
Member

@litetex litetex commented Sep 24, 2021

What is it?

  • Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

The following things are fixed

from #7161 (credits to @Redirion)

Issue 1 is when the option "Show thumbnail" is turned off. It caused setMetadata to never be called.
// setMetadata only updates the metadata when any of the metadata keys are null mediaSessionManager.setMetadata(getVideoTitle(), getUploaderName(), showThumbnail ? getThumbnail() : null, duration);

basically the first line in setMetadata checks, if the provided thumbnail/albumArt is not null.

Issue 2: if no thumbnail could be loaded, the dummy thumbnail resource would be used to be created through a Bitmap factory. Firstly it will be created each time (expensive) and secondly hashCode() will always yield different results. This means that setMetadata was basically executed in its whole every single time (each second since last updates).

I also reworked/de-duplicated the code a bit 馃槈

Issue 3: #7087
#7087 (comment)

Okay I tested the stuff a bit and found out that the metadata updates - that are done every second - are completely useless.
They should only be done when the metadata changes e.g. when opening a new video.

Issue 4:
Comparisons e.g. if the current title/artist/duration is also the requested one have been missing.
Added those by caching the last requested data using hashcodes.
This also avoids resource-intensive mediaSession.getController().getMetadata() calls.

With the changes applied there is a massive performance improvement

triggerProgressUpdate now runs within 4-13ms / 0,7-2ms (cpu):
grafik

Before it has been 30-130ms / 6-12ms (cpu):
grafik

So this decreases execution time of the progress update loop by about 10x (at least on my emulated Pixel 3a).

Fixes the following issue(s)

APK testing

The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR.

Due diligence

Robin and others added 3 commits September 24, 2021 23:28
* 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
@litetex litetex marked this pull request as draft September 24, 2021 21:55
@litetex litetex mentioned this pull request Sep 24, 2021
5 tasks
@Redirion
Copy link
Member

you could add the result of isLiveEdge to decide whether to check the duration

@litetex
Copy link
Member Author

litetex commented Sep 24, 2021

@Redirion

you could add the result of isLiveEdge to decide whether to check the duration

I can't follow. Where do you mean should I add this?

@litetex litetex marked this pull request as ready for review September 24, 2021 22:35
@Redirion
Copy link
Member

sorry isLiveEdge was wrong. But you can use isLive():

you could use it as an additional parameter:

mediaSessionManager.setMetadata(
                getVideoTitle(),
                getUploaderName(),
                showThumbnail ? Optional.ofNullable(getThumbnail()) : Optional.empty(),
                tag.getMetadata().getDuration(),
                isLive()
        );

this way you can use duration for comparisons if it isn't a livestream

@litetex
Copy link
Member Author

litetex commented Sep 25, 2021

@Redirion

this way you can use duration for comparisons if it isn't a livestream

Duration comparison is already always done:

So I don't understand the problem in the first place...

Anyway the check for isLive is done by asking the player which is risky as it may not be fully initalized when onMetadataChanged is called. It's also quite resource expensive when compared to simply reading if it's a livestream from the metadata.

I changed the code so that it now considers livestream and always uses duration -1 for them 馃槃

@Redirion
Copy link
Member

Yep sorry, all good

@AudricV AudricV added bug Issue is related to a bug player Issues related to any player (main, popup and background) labels Sep 26, 2021
Copy link
Member

@Redirion Redirion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Two personal preferences as a comment:

  • I would always prefer equals over hashCode for String comparisons
  • I would rather have a bit duplicated code by using more than one method than having a method with many return statements

@litetex
Copy link
Member Author

litetex commented Sep 27, 2021

  • I would always prefer equals over hashCode for String comparisons

I also considered that. However the deal with hashCode is that storing an int requires usually way less RAM than a String by - what I think are accaptable odds - of having an collision: Which is technically 1 in 2^32.
Someone tried it with 58k words and got one collision: https://stackoverflow.com/a/9407032

@Redirion Redirion merged commit f48ff61 into TeamNewPipe:dev Sep 27, 2021
@litetex litetex deleted the various-fixes-for-mediasession-player branch September 28, 2021 18:26
This was referenced Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is related to a bug player Issues related to any player (main, popup and background)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential performance problem with MediaSessionManager#get/setMetadata
3 participants