Skip to content

Commit

Permalink
Added hardware acceleration options for VLC
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyPavlenko committed Oct 7, 2023
1 parent 17db2f0 commit ae23994
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
19 changes: 19 additions & 0 deletions fermata/src/main/java/me/aap/fermata/media/pref/MediaPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ public interface MediaPrefs extends PreferenceStore {
int MEDIA_ENG_VLC = 2;
int MEDIA_ENG_YT = 3;
int MEDIA_ENG_CAST = 4;

int SCALE_BEST = 0;
int SCALE_FILL = 1;
int SCALE_ORIGINAL = 2;
int SCALE_4_3 = 3;
int SCALE_16_9 = 4;

int MEDIA_SCANNER_DEFAULT = 0;
int MEDIA_SCANNER_SYSTEM = 1;
int MEDIA_SCANNER_VLC = 2;

int HW_ACCEL_AUTO = 0;
int HW_ACCEL_FULL = 1;
int HW_ACCEL_DECODING = 2;
int HW_ACCEL_DISABLED = 3;

Pref<IntSupplier> AUDIO_ENGINE = Pref.i("AUDIO_ENGINE", MEDIA_ENG_MP);
Pref<IntSupplier> VIDEO_ENGINE = Pref.i("VIDEO_ENGINE", MEDIA_ENG_MP);
Pref<IntSupplier> VIDEO_SCALE = Pref.i("VIDEO_SCALE", SCALE_BEST);
Expand Down Expand Up @@ -54,6 +62,9 @@ public interface MediaPrefs extends PreferenceStore {
Pref<Supplier<String>> AUDIO_LANG = Pref.s("AUDIO_LANG", "");
Pref<Supplier<String>> AUDIO_KEY = Pref.s("AUDIO_KEY", "");
Pref<IntSupplier> WATCHED_THRESHOLD = Pref.i("WATCHED_THRESHOLD", 95);
Pref<IntSupplier> HW_ACCEL = Pref.i("HW_ACCEL", HW_ACCEL_DECODING);



default int getAudioEnginePref() {
return getIntPref(AUDIO_ENGINE);
Expand Down Expand Up @@ -85,6 +96,14 @@ default void setVideoScalePref(int scale) {
}
}

default int getHwAccelPref() {
return getIntPref(HW_ACCEL);
}

default void setHwAccelPref(int accel) {
applyIntPref(HW_ACCEL, accel);
}

default int getMediaScannerPref() {
return getIntPref(MEDIA_SCANNER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ private PreferenceViewAdapter createAdapter(MainActivityDelegate a) {
o.values = new int[]{R.string.video_scaling_best, R.string.video_scaling_fill,
R.string.video_scaling_orig, R.string.video_scaling_4, R.string.video_scaling_16};
});
sub1.addListPref(o -> {
o.store = mediaPrefs;
o.pref = MediaLibPrefs.HW_ACCEL;
o.title = R.string.hw_accel;
o.subtitle = R.string.string_format;
o.formatSubtitle = true;
o.values = new int[]{R.string.hw_accel_auto, R.string.hw_accel_full,
R.string.hw_accel_decoding, R.string.hw_accel_disabled};
o.visibility = vlcCond;
});
sub1.addListPref(o -> {
o.store = a.getPrefs();
o.pref = MainActivityPrefs.CLOCK_POS;
Expand Down Expand Up @@ -557,7 +567,7 @@ private PreferenceViewAdapter createAdapter(MainActivityDelegate a) {

sub2 = sub1.subSet(o -> {
o.title = R.string.audio;
o.visibility = PrefCondition.create(mediaPrefs, MediaLibPrefs.VLC_ENABLED);
o.visibility = vlcCond;
});
addAudioPrefs(sub2, mediaPrefs, isCar);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.aap.fermata.ui.view;

import static java.util.Objects.requireNonNull;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_AUTO;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_DECODING;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_DISABLED;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_FULL;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_16_9;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_4_3;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_BEST;
Expand Down Expand Up @@ -202,6 +206,10 @@ protected void buildVideoMenu(OverlayMenu.Builder b) {

b.addItem(R.id.video_scaling, R.string.video_scaling).setSubmenu(this::buildVideoScalingMenu);

if (getMainActivity().getMediaSessionCallback().getEngineManager().isVlcPlayerSupported()) {
b.addItem(R.id.video_scaling, R.string.hw_accel).setSubmenu(this::buildHwAccelMenu);
}

if (!item.isExternal()) {
b.addItem(R.id.watched_threshold, R.string.watched_threshold)
.setSubmenu(this::buildWatchedThresholdMenu);
Expand Down Expand Up @@ -287,6 +295,12 @@ private FutureSupplier<Void> buildBrowsableMenu(MainActivityDelegate a, OverlayM
sb.addItem(R.id.audio_prefs, R.string.audio).setSubmenu(this::buildAudioPrefsMenu);
sb.addItem(R.id.video_scaling, R.string.video_scaling)
.setSubmenu(this::buildVideoScalingMenu);

if (a.getMediaSessionCallback().getEngineManager().isVlcPlayerSupported()) {
sb.addItem(R.id.video_scaling, R.string.hw_accel)
.setSubmenu(this::buildHwAccelMenu);
}

sb.addItem(R.id.watched_threshold, R.string.watched_threshold)
.setSubmenu(this::buildWatchedThresholdMenu);
});
Expand Down Expand Up @@ -438,6 +452,19 @@ private void buildVideoScalingMenu(OverlayMenu.Builder b) {
b.setSelectionHandler(this);
}

private void buildHwAccelMenu(OverlayMenu.Builder b) {
int accel = item.getPrefs().getHwAccelPref();
b.addItem(R.id.hw_accel_auto, null, R.string.hw_accel_auto)
.setChecked(accel == HW_ACCEL_AUTO, true);
b.addItem(R.id.hw_accel_full, null, R.string.hw_accel_full)
.setChecked(accel == HW_ACCEL_FULL, true);
b.addItem(R.id.hw_accel_decoding, null, R.string.hw_accel_decoding)
.setChecked(accel == HW_ACCEL_DECODING, true);
b.addItem(R.id.hw_accel_disabled, null, R.string.hw_accel_disabled)
.setChecked(accel == HW_ACCEL_DISABLED, true);
b.setSelectionHandler(this);
}

private void buildAudioPrefsMenu(OverlayMenu.Builder b) {
PreferenceSet prefSet = new PreferenceSet();
addAudioPrefs(prefSet, item.getPrefs(), getMainActivity().isCarActivity());
Expand Down Expand Up @@ -562,6 +589,14 @@ public boolean menuItemSelected(OverlayMenuItem i) {
item.getPrefs().setVideoScalePref(SCALE_4_3);
} else if (id == R.id.video_scaling_16) {
item.getPrefs().setVideoScalePref(SCALE_16_9);
} else if (id == R.id.hw_accel_auto) {
item.getPrefs().setHwAccelPref(HW_ACCEL_AUTO);
} else if (id == R.id.hw_accel_full) {
item.getPrefs().setHwAccelPref(HW_ACCEL_FULL);
} else if (id == R.id.hw_accel_decoding) {
item.getPrefs().setHwAccelPref(HW_ACCEL_DECODING);
} else if (id == R.id.hw_accel_disabled) {
item.getPrefs().setHwAccelPref(HW_ACCEL_DISABLED);
} else if (id == R.id.delete) {
UiUtils.showQuestion(getContext(), R.string.delete_file_title, R.string.delete_file_question,
R.drawable.delete).onSuccess(v -> {
Expand Down
5 changes: 5 additions & 0 deletions fermata/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
<string name="video">Видео</string>
<string name="video_settings">Настройки видео</string>
<string name="video_scaling">Масштабирование видео</string>
<string name="hw_accel">Аппаратное ускорение</string>
<string name="hw_accel_auto">Автоматически</string>
<string name="hw_accel_full">Полное</string>
<string name="hw_accel_decoding">Декодирование</string>
<string name="hw_accel_disabled">Выключено</string>
<string name="video_scaling_best">Наиболее подходящее</string>
<string name="video_scaling_fill">Заполнить экран</string>
<string name="video_scaling_orig">Оригинальный размер</string>
Expand Down
4 changes: 4 additions & 0 deletions fermata/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<item name="video_scaling_orig" type="id" />
<item name="video_scaling_4" type="id" />
<item name="video_scaling_16" type="id" />
<item name="hw_accel_auto" type="id" />
<item name="hw_accel_full" type="id" />
<item name="hw_accel_decoding" type="id" />
<item name="hw_accel_disabled" type="id" />
<item name="preferred_media_engine" type="id" />
<item name="preferred_video_engine" type="id" />
<item name="preferred_audio_engine" type="id" />
Expand Down
5 changes: 5 additions & 0 deletions fermata/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
<string name="video">Video</string>
<string name="video_settings">Video settings</string>
<string name="video_scaling">Video scaling</string>
<string name="hw_accel">Hardware acceleration</string>
<string name="hw_accel_auto">Auto</string>
<string name="hw_accel_full">Full</string>
<string name="hw_accel_decoding">Decoding</string>
<string name="hw_accel_disabled">Disabled</string>
<string name="video_scaling_best">Best fit</string>
<string name="video_scaling_fill">Fill screen</string>
<string name="video_scaling_orig">Original size</string>
Expand Down
18 changes: 14 additions & 4 deletions modules/vlc/src/main/java/me/aap/fermata/engine/vlc/VlcEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static java.util.Collections.emptyList;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_DECODING;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_DISABLED;
import static me.aap.fermata.media.pref.MediaPrefs.HW_ACCEL_FULL;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_16_9;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_4_3;
import static me.aap.fermata.media.pref.MediaPrefs.SCALE_BEST;
Expand Down Expand Up @@ -105,12 +108,19 @@ public void prepare(PlayableItem source) {
}
}

media.addOption(":input-fast-seek");
switch (source.getPrefs().getHwAccelPref()) {
case HW_ACCEL_DECODING -> {
media.setHWDecoderEnabled(true, true);
media.addOption(":no-mediacodec-dr");
media.addOption(":no-omxil-dr");
}
case HW_ACCEL_FULL -> media.setHWDecoderEnabled(true, true);
case HW_ACCEL_DISABLED -> media.setHWDecoderEnabled(false, false);
}

PendingSource pending = new PendingSource(source, media, fd);
this.source = pending;
media.setHWDecoderEnabled(true, true);
media.addOption(":no-omxil-dr");
media.addOption(":no-mediacodec-dr");
media.addOption(":input-fast-seek");

if (media.isParsed()) {
prepared(pending);
Expand Down

0 comments on commit ae23994

Please sign in to comment.