[Android 6+] Add ability to always use ExoPlayer's MediaVideoCodecRenderer 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.
This commit is contained in:
AudricV 2023-02-26 19:39:17 +01:00 committed by Stypox
parent a02b92fd59
commit 787758a436
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
6 changed files with 121 additions and 6 deletions

View File

@ -69,7 +69,6 @@ import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player.PositionInfo;
import com.google.android.exoplayer2.RenderersFactory;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.Tracks;
import com.google.android.exoplayer2.source.MediaSource;
@ -95,6 +94,7 @@ import org.schabi.newpipe.local.history.HistoryRecordManager;
import org.schabi.newpipe.player.event.PlayerEventListener;
import org.schabi.newpipe.player.event.PlayerServiceEventListener;
import org.schabi.newpipe.player.helper.AudioReactor;
import org.schabi.newpipe.player.helper.CustomRenderersFactory;
import org.schabi.newpipe.player.helper.LoadController;
import org.schabi.newpipe.player.helper.PlayerDataSource;
import org.schabi.newpipe.player.helper.PlayerHelper;
@ -196,7 +196,7 @@ public final class Player implements PlaybackListener, Listener {
@NonNull private final DefaultTrackSelector trackSelector;
@NonNull private final LoadController loadController;
@NonNull private final RenderersFactory renderFactory;
@NonNull private final DefaultRenderersFactory renderFactory;
@NonNull private final VideoPlaybackResolver videoResolver;
@NonNull private final AudioPlaybackResolver audioResolver;
@ -261,9 +261,16 @@ public final class Player implements PlaybackListener, Listener {
final PlayerDataSource dataSource = new PlayerDataSource(context,
new DefaultBandwidthMeter.Builder(context).build());
loadController = new LoadController();
renderFactory = new DefaultRenderersFactory(context)
.setEnableDecoderFallback(prefs.getBoolean(
context.getString(R.string.use_exoplayer_decoder_fallback_key), false));
renderFactory = prefs.getBoolean(
context.getString(
R.string.always_use_exoplayer_set_output_surface_workaround_key), false)
? new CustomRenderersFactory(context) : new DefaultRenderersFactory(context);
renderFactory.setEnableDecoderFallback(
prefs.getBoolean(
context.getString(
R.string.use_exoplayer_decoder_fallback_key), false));
videoResolver = new VideoPlaybackResolver(context, dataSource, getQualityResolver());
audioResolver = new AudioPlaybackResolver(context, dataSource);

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -1370,4 +1370,5 @@
<string name="exoplayer_settings_key">exoplayer_settings_key</string>
<string name="disable_media_tunneling_key">disable_media_tunneling_key</string>
<string name="use_exoplayer_decoder_fallback_key">use_exoplayer_decoder_fallback_key</string>
<string name="always_use_exoplayer_set_output_surface_workaround_key">always_use_exoplayer_set_output_surface_workaround_key</string>
</resources>

View File

@ -777,4 +777,6 @@
<string name="settings_category_exoplayer_summary">Manage some ExoPlayer settings. These changes require a player restart to take effect</string>
<string name="use_exoplayer_decoder_fallback_title">Use ExoPlayer\'s decoder fallback feature</string>
<string name="use_exoplayer_decoder_fallback_summary">Enable this option if you have decoder initialization issues, which falls back to lower-priority decoders if primary decoders initialization fail. This may result in poor playback performance than when using primary decoders</string>
<string name="always_use_exoplayer_set_output_surface_workaround_title">Always use ExoPlayer\'s video output surface setting workaround</string>
<string name="always_use_exoplayer_set_output_surface_workaround_summary">This workaround releases and re-instantiates video codecs when a surface change occurs, instead of setting the surface to the codec directly. Already used by ExoPlayer on some devices with this issue, this setting has only an effect on Android 6 and higher\n\nEnabling this option may prevent playback errors when switching the current video player or switching to fullscreen</string>
</resources>

View File

@ -29,4 +29,12 @@
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/always_use_exoplayer_set_output_surface_workaround_key"
android:summary="@string/always_use_exoplayer_set_output_surface_workaround_summary"
android:title="@string/always_use_exoplayer_set_output_surface_workaround_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
</PreferenceScreen>