-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android 6+] Add ability to always use ExoPlayer's MediaVideoCodecRen…
…derer setOutputSurface workaround As some devices not present in ExoPlayer's list may not implement MediaCodec.setOutputSurface(Surface) properly, this workaround could be useful on these devices. It forces ExoPlayer to fall back on releasing and re-instantiating video codec instances, which is always used on Android 5 and lower due to addition of this method in Android 6. To do so, a CustomMediaCodecVideoRenderer, based on ExoPlayer's MediaVideoCodecRenderer which always return true for the codecNeedsSetOutputSurfaceWorkaround method has been added, which is used in CustomRenderersFactory, a class based on DefaultRenderersFactory which always returns our CustomMediaCodecVideoRenderer as the video renderers. CustomRenderersFactory replaces DefaultRenderersFactory in the player, in the case this setting is enabled.
- Loading branch information
Showing
6 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/org/schabi/newpipe/player/helper/CustomMediaCodecVideoRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.schabi.newpipe.player.helper; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import com.google.android.exoplayer2.mediacodec.MediaCodecAdapter; | ||
import com.google.android.exoplayer2.mediacodec.MediaCodecSelector; | ||
import com.google.android.exoplayer2.video.MediaCodecVideoRenderer; | ||
import com.google.android.exoplayer2.video.VideoRendererEventListener; | ||
|
||
/** | ||
* A {@link MediaCodecVideoRenderer} which always enable the output surface workaround that | ||
* ExoPlayer enables on several devices which are known to implement | ||
* {@link android.media.MediaCodec#setOutputSurface(android.view.Surface) | ||
* MediaCodec.setOutputSurface(Surface)} incorrectly. | ||
* | ||
* <p> | ||
* See {@link MediaCodecVideoRenderer#codecNeedsSetOutputSurfaceWorkaround(String)} for more | ||
* details. | ||
* </p> | ||
* | ||
* <p> | ||
* This custom {@link MediaCodecVideoRenderer} may be useful in the case a device is affected by | ||
* this issue but is not present in ExoPlayer's list. | ||
* </p> | ||
* | ||
* <p> | ||
* This class has only effect on devices with Android 6 and higher, as the {@code setOutputSurface} | ||
* method is only implemented in these Android versions and the method used as a workaround is | ||
* always applied on older Android versions (releasing and re-instantiating video codec instances). | ||
* </p> | ||
*/ | ||
public final class CustomMediaCodecVideoRenderer extends MediaCodecVideoRenderer { | ||
|
||
@SuppressWarnings({"checkstyle:ParameterNumber", "squid:S107"}) | ||
public CustomMediaCodecVideoRenderer(final Context context, | ||
final MediaCodecAdapter.Factory codecAdapterFactory, | ||
final MediaCodecSelector mediaCodecSelector, | ||
final long allowedJoiningTimeMs, | ||
final boolean enableDecoderFallback, | ||
@Nullable final Handler eventHandler, | ||
@Nullable final VideoRendererEventListener eventListener, | ||
final int maxDroppedFramesToNotify) { | ||
super(context, codecAdapterFactory, mediaCodecSelector, allowedJoiningTimeMs, | ||
enableDecoderFallback, eventHandler, eventListener, maxDroppedFramesToNotify); | ||
} | ||
|
||
@Override | ||
protected boolean codecNeedsSetOutputSurfaceWorkaround(final String name) { | ||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/org/schabi/newpipe/player/helper/CustomRenderersFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.schabi.newpipe.player.helper; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
|
||
import com.google.android.exoplayer2.DefaultRenderersFactory; | ||
import com.google.android.exoplayer2.Renderer; | ||
import com.google.android.exoplayer2.mediacodec.MediaCodecSelector; | ||
import com.google.android.exoplayer2.video.VideoRendererEventListener; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* A {@link DefaultRenderersFactory} which only uses {@link CustomMediaCodecVideoRenderer} as an | ||
* implementation of video codec renders. | ||
* | ||
* <p> | ||
* As no ExoPlayer extension is currently used, the reflection code used by ExoPlayer to try to | ||
* load video extension libraries is not needed in our case and has been removed. This should be | ||
* changed in the case an extension is shipped with the app, such as the AV1 one. | ||
* </p> | ||
*/ | ||
public final class CustomRenderersFactory extends DefaultRenderersFactory { | ||
|
||
public CustomRenderersFactory(final Context context) { | ||
super(context); | ||
} | ||
|
||
@SuppressWarnings("checkstyle:ParameterNumber") | ||
@Override | ||
protected void buildVideoRenderers(final Context context, | ||
@ExtensionRendererMode final int extensionRendererMode, | ||
final MediaCodecSelector mediaCodecSelector, | ||
final boolean enableDecoderFallback, | ||
final Handler eventHandler, | ||
final VideoRendererEventListener eventListener, | ||
final long allowedVideoJoiningTimeMs, | ||
final ArrayList<Renderer> out) { | ||
out.add(new CustomMediaCodecVideoRenderer(context, getCodecAdapterFactory(), | ||
mediaCodecSelector, allowedVideoJoiningTimeMs, enableDecoderFallback, eventHandler, | ||
eventListener, MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters