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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove custom subtitle renderer #3825

Merged
merged 2 commits into from
Aug 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jellyfin.androidtv.preference.constant.WatchedIndicatorBehavior
import org.jellyfin.preference.booleanPreference
import org.jellyfin.preference.enumPreference
import org.jellyfin.preference.floatPreference
import org.jellyfin.preference.intPreference
import org.jellyfin.preference.longPreference
import org.jellyfin.preference.store.SharedPreferenceStore
Expand Down Expand Up @@ -156,29 +157,24 @@
var seriesThumbnailsEnabled = booleanPreference("pref_enable_series_thumbnails", true)

/**
* Enable subtitles background
*/
var subtitlesBackgroundEnabled = booleanPreference("subtitles_background_enabled", true)

/**
* Subtitles font size
* Subtitles foreground color
*/
var subtitlesSize = intPreference("subtitles_size", 28)
var subtitlesBackgroundColor = longPreference("subtitles_background_color", 0x00FFFFFF)

/**
* Subtitles stroke size
* Subtitles foreground color
*/
var subtitleStrokeSize = intPreference("subtitles_stroke_size", 0)
var subtitlesTextColor = longPreference("subtitles_text_color", 0xFFFFFFFF)

/**
* Subtitles position
* Subtitles stroke color
*/
var subtitlePosition = intPreference("subtitles_position", 40)
var subtitleTextStrokeColor = longPreference("subtitles_text_stroke_color", 0xFF000000)

/**
* Subtitles foreground color
* Subtitles font size
*/
var subtitlesTextColor = longPreference("subtitles_text_color", 0xFFFFFFFF)
var subtitlesTextSize = floatPreference("subtitles_text_size", 1f)

/**
* Show screensaver in app
Expand Down Expand Up @@ -216,6 +212,17 @@
// Enable playback rewrite for music
putBoolean("playback_new_audio", true)
}

// v0.17.z to v0.18.0
migration(toVersion = 8) {
// Set subtitle background color to black if it was enabled in a previous version
val subtitlesBackgroundEnabled = it.getBoolean("subtitles_background_enabled", true)
putLong("subtitles_background_color", if (subtitlesBackgroundEnabled) 0XFF000000L else 0X00FFFFFFL)

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.

// Set subtitle text stroke color to black if it was enabled in a previous version
val subtitleStrokeSize = it.getInt("subtitles_stroke_size", 0)
putLong("subtitles_text_stroke_color", if (subtitleStrokeSize > 0) 0XFF000000L else 0X00FFFFFFL)

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -25,7 +22,6 @@
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
Expand All @@ -45,7 +41,6 @@
import org.jellyfin.androidtv.data.service.BackgroundService;
import org.jellyfin.androidtv.databinding.OverlayTvGuideBinding;
import org.jellyfin.androidtv.databinding.VlcPlayerInterfaceBinding;
import org.jellyfin.androidtv.preference.UserPreferences;
import org.jellyfin.androidtv.ui.GuideChannelHeader;
import org.jellyfin.androidtv.ui.GuidePagingButton;
import org.jellyfin.androidtv.ui.HorizontalScrollViewListener;
Expand All @@ -67,7 +62,6 @@
import org.jellyfin.androidtv.ui.presentation.ChannelCardPresenter;
import org.jellyfin.androidtv.ui.presentation.MutableObjectAdapter;
import org.jellyfin.androidtv.ui.presentation.PositionableListRowPresenter;
import org.jellyfin.androidtv.ui.shared.PaddedLineBackgroundSpan;
import org.jellyfin.androidtv.util.CoroutineUtils;
import org.jellyfin.androidtv.util.DateTimeExtensionsKt;
import org.jellyfin.androidtv.util.ImageHelper;
Expand All @@ -77,12 +71,9 @@
import org.jellyfin.androidtv.util.Utils;
import org.jellyfin.androidtv.util.apiclient.EmptyLifecycleAwareResponse;
import org.jellyfin.androidtv.util.sdk.BaseItemExtensionsKt;
import org.jellyfin.apiclient.model.mediainfo.SubtitleTrackEvent;
import org.jellyfin.apiclient.model.mediainfo.SubtitleTrackInfo;
import org.jellyfin.sdk.model.api.BaseItemDto;
import org.jellyfin.sdk.model.api.BaseItemKind;
import org.jellyfin.sdk.model.api.ChapterInfo;
import org.koin.java.KoinJavaComponent;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -133,18 +124,6 @@ public class CustomPlaybackOverlayFragment extends Fragment implements LiveTvGui

private LeanbackOverlayFragment leanbackOverlayFragment;

// Subtitle fields
private static final int SUBTITLE_PADDING = 20;
private static final long SUBTITLE_RENDER_INTERVAL_MS = 50;
private SubtitleTrackInfo subtitleTrackInfo;
private int currentSubtitleIndex = 0;
private long lastSubtitlePositionMs = 0;
private final UserPreferences userPreferences = KoinJavaComponent.<UserPreferences>get(UserPreferences.class);
private final int subtitlesSize = userPreferences.get(UserPreferences.Companion.getSubtitlesSize());
private final boolean subtitlesBackgroundEnabled = userPreferences.get(UserPreferences.Companion.getSubtitlesBackgroundEnabled());
private final int subtitlesPosition = userPreferences.get(UserPreferences.Companion.getSubtitlePosition());
private final int subtitlesStrokeWidth = userPreferences.get(UserPreferences.Companion.getSubtitleStrokeSize());

private final Lazy<org.jellyfin.sdk.api.client.ApiClient> api = inject(org.jellyfin.sdk.api.client.ApiClient.class);
private final Lazy<MediaManager> mediaManager = inject(MediaManager.class);
private final Lazy<VideoQueueManager> videoQueueManager = inject(VideoQueueManager.class);
Expand Down Expand Up @@ -259,26 +238,6 @@ public void onActivityCreated(Bundle savedInstanceState) {

prepareOverlayFragment();

//manual subtitles
// This configuration is required for the PaddedLineBackgroundSpan to work
binding.subtitlesText.setShadowLayer(SUBTITLE_PADDING, 0, 0, Color.TRANSPARENT);
binding.subtitlesText.setPadding(SUBTITLE_PADDING, 0, SUBTITLE_PADDING, 0);

// Subtitles font size configuration
binding.subtitlesText.setTextSize(subtitlesSize);

// Subtitles font position (margin bottom)
if (subtitlesPosition > 0) {
ViewGroup.MarginLayoutParams currentLayoutParams = (ViewGroup.MarginLayoutParams) binding.subtitlesText.getLayoutParams();
currentLayoutParams.bottomMargin = (8 + Utils.convertDpToPixel(requireContext(), subtitlesPosition));
binding.subtitlesText.setLayoutParams(currentLayoutParams);
}

// Subtitles stroke width
if (subtitlesStrokeWidth > 0 && !subtitlesBackgroundEnabled) {
binding.subtitlesText.setStrokeWidth(subtitlesStrokeWidth);
}

//pre-load animations
fadeOut = AnimationUtils.loadAnimation(requireContext(), androidx.leanback.R.anim.abc_fade_out);
fadeOut.setAnimationListener(hideAnimationListener);
Expand Down Expand Up @@ -1313,137 +1272,6 @@ public void showNextUp(@NonNull UUID id) {
navigationRepository.getValue().navigate(Destinations.INSTANCE.nextUp(id), true);
}

public void addManualSubtitles(@Nullable SubtitleTrackInfo info) {
subtitleTrackInfo = info;
currentSubtitleIndex = -1;
lastSubtitlePositionMs = 0;
clearSubtitles();
}

public void showSubLoadingMsg(final boolean show) {
if (show) {
renderSubtitles(requireContext().getString(R.string.msg_subtitles_loading));
} else {
clearSubtitles();
}
}

public void updateSubtitles(long positionMs) {
int iterCount = 1;
final long positionTicks = positionMs * 10000;
final long lastPositionTicks = lastSubtitlePositionMs * 10000;

if (subtitleTrackInfo == null
|| subtitleTrackInfo.getTrackEvents() == null
|| subtitleTrackInfo.getTrackEvents().size() < 1
|| currentSubtitleIndex >= subtitleTrackInfo.getTrackEvents().size()) {
return;
}

if (positionTicks < subtitleTrackInfo.getTrackEvents().get(0).getStartPositionTicks())
return;

// Skip rendering if the interval ms have not passed since last render
if (lastSubtitlePositionMs > 0
&& Math.abs(lastSubtitlePositionMs - positionMs) < SUBTITLE_RENDER_INTERVAL_MS) {
return;
}

// If the user has skipped back, reset the subtitle index
if (lastSubtitlePositionMs > positionMs) {
currentSubtitleIndex = -1;
clearSubtitles();
}

if (currentSubtitleIndex == -1)
Timber.d("subtitle track events size %s", subtitleTrackInfo.getTrackEvents().size());

// Find the next subtitle event that should be rendered
for (int tmpSubtitleIndex = currentSubtitleIndex == -1 ? 0 : currentSubtitleIndex; tmpSubtitleIndex < subtitleTrackInfo.getTrackEvents().size(); tmpSubtitleIndex++) {
SubtitleTrackEvent trackEvent = subtitleTrackInfo.getTrackEvents().get(tmpSubtitleIndex);

if (positionTicks >= trackEvent.getStartPositionTicks()
&& positionTicks < trackEvent.getEndPositionTicks()) {
// This subtitle event should be displayed now
// use lastPositionTicks to ensure it is only rendered once

if (lastPositionTicks < trackEvent.getStartPositionTicks() || lastPositionTicks >= trackEvent.getEndPositionTicks()) {
Timber.d("rendering subtitle event: %s (pos %s start %s end %s)", tmpSubtitleIndex, positionMs, trackEvent.getStartPositionTicks() / 10000, trackEvent.getEndPositionTicks() / 10000);
renderSubtitles(trackEvent.getText());
}

currentSubtitleIndex = tmpSubtitleIndex;
lastSubtitlePositionMs = positionMs;
// rendering should happen on the 2nd iteration
if (iterCount > 2)
Timber.d("subtitles handled in %s iterations", iterCount);
return;
} else if (tmpSubtitleIndex < subtitleTrackInfo.getTrackEvents().size() - 1) {
SubtitleTrackEvent nextTrackEvent = subtitleTrackInfo.getTrackEvents().get(tmpSubtitleIndex + 1);

if (positionTicks >= trackEvent.getEndPositionTicks() && positionTicks < nextTrackEvent.getStartPositionTicks()) {
// clear the subtitles if between events
// use lastPositionTicks to ensure it is only cleared once

if (currentSubtitleIndex > -1 && !(lastPositionTicks >= trackEvent.getEndPositionTicks() && lastPositionTicks < nextTrackEvent.getStartPositionTicks())) {
Timber.d("clearing subtitle event: %s (pos %s - event end %s)", tmpSubtitleIndex, positionMs, trackEvent.getEndPositionTicks() / 10000);
clearSubtitles();
}

// set currentSubtitleIndex in case it was -1
currentSubtitleIndex = tmpSubtitleIndex;
lastSubtitlePositionMs = positionMs;
if (iterCount > 1)
Timber.d("subtitles handled in %s iterations", iterCount);
return;
}
}
iterCount++;
}
// handles clearing the last event
if (iterCount > 1)
Timber.d("subtitles handled in %s iterations", iterCount);
clearSubtitles();
}

private void clearSubtitles() {
requireActivity().runOnUiThread(() -> {
binding.subtitlesText.setVisibility(View.INVISIBLE);
binding.subtitlesText.setText(null);
});
}

private void renderSubtitles(@Nullable final String text) {
if (text == null || text.length() == 0) {
clearSubtitles();
return;
}
requireActivity().runOnUiThread(() -> {
final String htmlText = text
// Encode whitespace as html entities
.replaceAll("\\r\\n", "<br>")
.replaceAll("\\n", "<br>")
.replaceAll("\\\\h", "&ensp;")
// Remove SSA tags
.replaceAll("\\{\\\\.*?\\}", "");

final SpannableString span = new SpannableString(TextUtilsKt.toHtmlSpanned(htmlText));
if (subtitlesBackgroundEnabled) {
// Disable the text outlining when the background is enabled
binding.subtitlesText.setStrokeWidth(0.0f);

// get the alignment gravity of the TextView
// extract the absolute horizontal gravity so the span can draw its background aligned
int gravity = binding.subtitlesText.getGravity();
int horizontalGravity = Gravity.getAbsoluteGravity(gravity, binding.subtitlesText.getLayoutDirection()) & Gravity.HORIZONTAL_GRAVITY_MASK;
span.setSpan(new PaddedLineBackgroundSpan(ContextCompat.getColor(requireContext(), R.color.black_opaque), SUBTITLE_PADDING, horizontalGravity), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

binding.subtitlesText.setText(span);
binding.subtitlesText.setVisibility(View.VISIBLE);
});
}

@Override
public void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
import org.jellyfin.androidtv.util.apiclient.ReportingHelper;
import org.jellyfin.androidtv.util.apiclient.StreamHelper;
import org.jellyfin.androidtv.util.profile.ExoPlayerProfile;
import org.jellyfin.androidtv.util.sdk.ModelUtils;
import org.jellyfin.androidtv.util.sdk.compat.JavaCompat;
import org.jellyfin.apiclient.interaction.ApiClient;
import org.jellyfin.apiclient.interaction.Response;
import org.jellyfin.apiclient.model.dlna.DeviceProfile;
import org.jellyfin.apiclient.model.dlna.SubtitleDeliveryMethod;
import org.jellyfin.apiclient.model.mediainfo.SubtitleTrackInfo;
import org.jellyfin.apiclient.model.session.PlayMethod;
import org.jellyfin.sdk.model.api.BaseItemDto;
import org.jellyfin.sdk.model.api.BaseItemKind;
Expand Down Expand Up @@ -753,9 +751,6 @@ public void switchSubtitleStream(int index) {
refreshCurrentPosition();
Timber.d("Setting subtitle index to: %d", index);

// clear subtitles first
if (mFragment != null) mFragment.addManualSubtitles(null);
mVideoManager.disableSubs();
// clear the default in case there's an error loading the subtitles
mDefaultSubIndex = -1;

Expand Down Expand Up @@ -796,63 +791,16 @@ public void switchSubtitleStream(int index) {
// when burnt-in subtitles are selected, mCurrentOptions SubtitleStreamIndex is set in startItem() as soon as playback starts
// otherwise mCurrentOptions SubtitleStreamIndex is kept null until now so we knew subtitles needed to be enabled but weren't already

switch (streamInfo.getDeliveryMethod()) {
case Embed:
if (!mVideoManager.setExoPlayerTrack(index, MediaStreamType.SUBTITLE, getCurrentlyPlayingItem().getMediaStreams())) {
if (streamInfo.getDeliveryMethod() == SubtitleDeliveryMethod.Embed) {
if (!mVideoManager.setExoPlayerTrack(index, MediaStreamType.SUBTITLE, getCurrentlyPlayingItem().getMediaStreams())) {
// error selecting internal subs
if (mFragment != null)
Utils.showToast(mFragment.getContext(), mFragment.getString(R.string.msg_unable_load_subs));
} else {
mCurrentOptions.setSubtitleStreamIndex(index);
mDefaultSubIndex = index;
}
break;
case External:
if (mFragment != null) mFragment.showSubLoadingMsg(true);

stream = ModelUtils.withDelivery(
stream,
org.jellyfin.sdk.model.api.SubtitleDeliveryMethod.EXTERNAL,
String.format(
"%1$s/Videos/%2$s/%3$s/Subtitles/%4$s/0/Stream.JSON",
apiClient.getValue().getApiUrl(),
mCurrentStreamInfo.getItemId(),
mCurrentStreamInfo.getMediaSourceId(),
String.valueOf(stream.getIndex())
)
);
apiClient.getValue().getSubtitles(stream.getDeliveryUrl(), new Response<SubtitleTrackInfo>() {

@Override
public void onResponse(final SubtitleTrackInfo info) {

if (info != null) {
Timber.d("Adding json subtitle track to player");
if (mFragment != null) mFragment.addManualSubtitles(info);
mCurrentOptions.setSubtitleStreamIndex(index);
mDefaultSubIndex = index;
} else {
Timber.e("Empty subtitle result");
if (mFragment != null) {
Utils.showToast(mFragment.getContext(), mFragment.getString(R.string.msg_unable_load_subs));
mFragment.showSubLoadingMsg(false);
}
}
}

@Override
public void onError(Exception ex) {
Timber.e(ex, "Error downloading subtitles");
if (mFragment != null) {
Utils.showToast(mFragment.getContext(), mFragment.getString(R.string.msg_unable_load_subs));
mFragment.showSubLoadingMsg(false);
}
}

});
break;
case Hls:
break;
}
}
}

Expand Down Expand Up @@ -1279,9 +1227,6 @@ public void onProgress() {
stopSpinner();
}
}

if (mFragment != null && finishedInitialSeek)
mFragment.updateSubtitles(mCurrentPosition);
}
if (mFragment != null)
mFragment.setCurrentTime(mCurrentPosition);
Expand Down
Loading
Loading