Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.google.android.exoplayer2.Player.PositionInfo;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.Tracks;
import com.google.android.exoplayer2.audio.SilenceSkippingAudioProcessor;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.text.CueGroup;
Expand Down Expand Up @@ -299,10 +300,21 @@ public Player(@NonNull final PlayerService service,
new DefaultBandwidthMeter.Builder(context).build());
loadController = new LoadController();

renderFactory = prefs.getBoolean(
context.getString(
R.string.always_use_exoplayer_set_output_surface_workaround_key), false)
? new CustomRenderersFactory(context) : new DefaultRenderersFactory(context);
final boolean alwaysUseExoplayerSetOutputSurfaceWorkaround = prefs.getBoolean(
context.getString(R.string.always_use_exoplayer_set_output_surface_workaround_key),
false);
final int maxSilenceDurationMillis = prefs.getInt(
context.getString(R.string.max_silence_duration_key),
Integer.parseInt(context.getString(R.string.max_silence_duration_value)));
final long maxSilenceDurationMicros = MILLISECONDS.toMicros(maxSilenceDurationMillis);
final SilenceSkippingAudioProcessor silenceSkippingAudioProcessor =
new SilenceSkippingAudioProcessor(
maxSilenceDurationMicros,
maxSilenceDurationMicros,
SilenceSkippingAudioProcessor.DEFAULT_SILENCE_THRESHOLD_LEVEL);
Comment on lines +310 to +314
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For reference for NewPlayer, this approach has been changed in Media3.

I would remove the duplicate call to MILLISECONDS.toMicros(maxSilenceDurationMillis) by saving the result in a variable.

Can you explain why you set the same value to minimumSilenceDurationUs and paddingSilenceUs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved it to a variable.
The duration is passed both as minimumSilenceDurationUs and paddingSilenceUs so that when the silence longer than maxSilenceDuration is detected, it is truncated to maxSilenceDuration. Silence detection length is controlled by minimumSilenceDurationUs and the duration which is left is controlled by paddingSilenceUs. So when we set them to the same value, we achieve this truncation behavior.
I think this behavior is reasonable and it also unclutters the settings by having only one parameter to control silence skipping.
If these variables are independent, the behavior could be different, e.g. if minimumSilenceDurationUs is 2s and paddingSilenceUs is 0.01s, it means that silences up to 2 seconds are kept as-is, but silences longer than 2 seconds get truncated to 0.01s. To me this behavior seems unnatural so I made these values the same.

renderFactory = new CustomRenderersFactory(
context, alwaysUseExoplayerSetOutputSurfaceWorkaround,
silenceSkippingAudioProcessor);

renderFactory.setEnableDecoderFallback(
prefs.getBoolean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@

import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.audio.AudioCapabilities;
import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioSink;
import com.google.android.exoplayer2.audio.DefaultAudioSink;
import com.google.android.exoplayer2.audio.SilenceSkippingAudioProcessor;
import com.google.android.exoplayer2.audio.SonicAudioProcessor;
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.
* A {@link DefaultRenderersFactory} which uses {@link CustomMediaCodecVideoRenderer} as an
* implementation of video codec renders and uses a provided {@link SilenceSkippingAudioProcessor}
* to control silence skipping behavior more precisely.
*
* <p>
* As no ExoPlayer extension is currently used, the reflection code used by ExoPlayer to try to
Expand All @@ -22,8 +29,17 @@
*/
Comment thread
AudricV marked this conversation as resolved.
public final class CustomRenderersFactory extends DefaultRenderersFactory {

public CustomRenderersFactory(final Context context) {
private final boolean alwaysUseExoplayerSetOutputSurfaceWorkaround;
private final SilenceSkippingAudioProcessor silenceSkippingAudioProcessor;

public CustomRenderersFactory(
final Context context,
final boolean alwaysUseExoplayerSetOutputSurfaceWorkaround,
final SilenceSkippingAudioProcessor silenceSkippingAudioProcessor) {
super(context);
this.alwaysUseExoplayerSetOutputSurfaceWorkaround =
alwaysUseExoplayerSetOutputSurfaceWorkaround;
this.silenceSkippingAudioProcessor = silenceSkippingAudioProcessor;
}

@SuppressWarnings("checkstyle:ParameterNumber")
Expand All @@ -36,8 +52,35 @@ protected void buildVideoRenderers(final Context context,
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));
if (alwaysUseExoplayerSetOutputSurfaceWorkaround) {
out.add(new CustomMediaCodecVideoRenderer(context, getCodecAdapterFactory(),
mediaCodecSelector, allowedVideoJoiningTimeMs, enableDecoderFallback,
eventHandler, eventListener, MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY));
} else {
super.buildVideoRenderers(context, extensionRendererMode, mediaCodecSelector,
enableDecoderFallback, eventHandler, eventListener, allowedVideoJoiningTimeMs,
out);
}
}

@Override
protected AudioSink buildAudioSink(
final Context context,
final boolean enableFloatOutput,
final boolean enableAudioTrackPlaybackParams,
final boolean enableOffload) {
return new DefaultAudioSink.Builder()
.setAudioCapabilities(AudioCapabilities.getCapabilities(context))
.setEnableFloatOutput(enableFloatOutput)
.setEnableAudioTrackPlaybackParams(enableAudioTrackPlaybackParams)
.setOffloadMode(
enableOffload
? DefaultAudioSink.OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED
: DefaultAudioSink.OFFLOAD_MODE_DISABLED)
.setAudioProcessorChain(new DefaultAudioSink.DefaultAudioProcessorChain(
new AudioProcessor[]{}, silenceSkippingAudioProcessor,
new SonicAudioProcessor()
))
.build();
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
<string name="default_popup_resolution_key">default_popup_resolution</string>
<string name="default_popup_resolution_value">480p</string>
<string name="best_resolution_key">best_resolution</string>
<string name="max_silence_duration_value">500</string>
<string name="max_silence_duration_min">100</string>
<string name="max_silence_duration_max">5000</string>
<string name="max_silence_duration_key">max_silence_duration</string>

<string-array name="high_resolution_list_values">
<item>2160p</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<string name="default_popup_resolution_title">Default popup resolution</string>
<string name="show_higher_resolutions_title">Show higher resolutions</string>
<string name="show_higher_resolutions_summary">Only some devices can play 2K/4K videos</string>
<string name="max_silence_duration_title">Maximal silence duration in milliseconds that remains when \"fast forwarding during silence\" is enabled</string>
<string name="play_with_kodi_title">Play with Kodi</string>
<string name="kore_not_found">Install missing Kore app\?</string>
<string name="show_play_with_kodi_title">Show \"Play with Kodi\" option</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/exoplayer_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@
app:singleLineTitle="false"
app:iconSpaceReserved="false" />

<SeekBarPreference
android:defaultValue="@string/max_silence_duration_value"
app:min="@string/max_silence_duration_min"
android:max="@string/max_silence_duration_max"
android:key="@string/max_silence_duration_key"
android:title="@string/max_silence_duration_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true"
app:showSeekBarValue="true" />

</PreferenceScreen>