-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Better player error handling #7142
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
Merged
Stypox
merged 11 commits into
TeamNewPipe:dev
from
litetex:better-player-error-handling
Nov 28, 2021
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e632fab
Added option to report player errors
litetex 769791a
Added a "Crash the player" debug option
litetex e5c00a7
Added some doc
litetex c3f1478
Support for debug option "Crash the player" on TVs
litetex fb58967
PlayerErrorHandler refactor + docs
litetex f18ee8e
Added a bit more documentation
litetex aa28a85
Added a workaround for not serializable exceptions
litetex 0d51eef
Refactoring
litetex 6992b2c
Moved report_player_errors to debug
litetex 2f99a21
Fixed build
litetex 4c8dca5
Fixed NPE + Problems with context
litetex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
app/src/main/java/org/schabi/newpipe/error/EnsureExceptionSerializable.java
This file contains hidden or 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,103 @@ | ||
| package org.schabi.newpipe.error; | ||
|
|
||
| import android.util.Log; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.ObjectOutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Ensures that a Exception is serializable. | ||
| * This is | ||
| */ | ||
| public final class EnsureExceptionSerializable { | ||
| private static final String TAG = "EnsureExSerializable"; | ||
|
|
||
| private EnsureExceptionSerializable() { | ||
| // No instance | ||
| } | ||
|
|
||
| /** | ||
| * Ensures that an exception is serializable. | ||
| * <br/> | ||
| * If that is not the case a {@link WorkaroundNotSerializableException} is created. | ||
| * | ||
| * @param exception | ||
| * @return if an exception is not serializable a new {@link WorkaroundNotSerializableException} | ||
| * otherwise the exception from the parameter | ||
| */ | ||
| public static Exception ensureSerializable(@NonNull final Exception exception) { | ||
| return checkIfSerializable(exception) | ||
| ? exception | ||
| : WorkaroundNotSerializableException.create(exception); | ||
| } | ||
|
|
||
| public static boolean checkIfSerializable(@NonNull final Exception exception) { | ||
| try { | ||
| // Check by creating a new ObjectOutputStream which does the serialization | ||
| try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| ObjectOutputStream oos = new ObjectOutputStream(bos) | ||
| ) { | ||
| oos.writeObject(exception); | ||
| oos.flush(); | ||
|
|
||
| bos.toByteArray(); | ||
| } | ||
|
|
||
| return true; | ||
| } catch (final IOException ex) { | ||
| Log.d(TAG, "Exception is not serializable", ex); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static class WorkaroundNotSerializableException extends Exception { | ||
| protected WorkaroundNotSerializableException( | ||
| final Throwable notSerializableException, | ||
| final Throwable cause) { | ||
| super(notSerializableException.toString(), cause); | ||
| setStackTrace(notSerializableException.getStackTrace()); | ||
| } | ||
|
|
||
| protected WorkaroundNotSerializableException(final Throwable notSerializableException) { | ||
| super(notSerializableException.toString()); | ||
| setStackTrace(notSerializableException.getStackTrace()); | ||
| } | ||
|
|
||
| public static WorkaroundNotSerializableException create( | ||
| @NonNull final Exception notSerializableException | ||
| ) { | ||
| // Build a list of the exception + all causes | ||
| final List<Throwable> throwableList = new ArrayList<>(); | ||
|
|
||
| int pos = 0; | ||
| Throwable throwableToProcess = notSerializableException; | ||
|
|
||
| while (throwableToProcess != null) { | ||
| throwableList.add(throwableToProcess); | ||
|
|
||
| pos++; | ||
| throwableToProcess = throwableToProcess.getCause(); | ||
| } | ||
|
|
||
| // Reverse list so that it starts with the last one | ||
| Collections.reverse(throwableList); | ||
|
|
||
| // Build exception stack | ||
| WorkaroundNotSerializableException cause = null; | ||
| for (final Throwable t : throwableList) { | ||
| cause = cause == null | ||
| ? new WorkaroundNotSerializableException(t) | ||
| : new WorkaroundNotSerializableException(t, cause); | ||
| } | ||
|
|
||
| return cause; | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
159 changes: 159 additions & 0 deletions
159
app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java
This file contains hidden or 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,159 @@ | ||
| package org.schabi.newpipe.fragments.detail; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.Log; | ||
| import android.view.ContextThemeWrapper; | ||
| import android.view.LayoutInflater; | ||
| import android.view.ViewGroup; | ||
| import android.widget.RadioButton; | ||
| import android.widget.RadioGroup; | ||
| import android.widget.Toast; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.appcompat.app.AlertDialog; | ||
|
|
||
| import com.google.android.exoplayer2.C; | ||
| import com.google.android.exoplayer2.ExoPlaybackException; | ||
|
|
||
| import org.schabi.newpipe.R; | ||
| import org.schabi.newpipe.databinding.ListRadioIconItemBinding; | ||
| import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; | ||
| import org.schabi.newpipe.player.Player; | ||
| import org.schabi.newpipe.util.ThemeHelper; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Outsourced logic for crashing the player in the {@link VideoDetailFragment}. | ||
| */ | ||
| public final class VideoDetailPlayerCrasher { | ||
|
|
||
| // This has to be <= 23 chars on devices running Android 7 or lower (API <= 25) | ||
| // or it fails with an IllegalArgumentException | ||
| // https://stackoverflow.com/a/54744028 | ||
| private static final String TAG = "VideoDetPlayerCrasher"; | ||
|
|
||
| private static final Map<String, Supplier<ExoPlaybackException>> AVAILABLE_EXCEPTION_TYPES = | ||
| getExceptionTypes(); | ||
|
|
||
| private VideoDetailPlayerCrasher() { | ||
| // No impls | ||
| } | ||
|
|
||
| private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() { | ||
| final String defaultMsg = "Dummy"; | ||
| final Map<String, Supplier<ExoPlaybackException>> exceptionTypes = new LinkedHashMap<>(); | ||
| exceptionTypes.put( | ||
| "Source", | ||
| () -> ExoPlaybackException.createForSource( | ||
| new IOException(defaultMsg) | ||
| ) | ||
| ); | ||
| exceptionTypes.put( | ||
| "Renderer", | ||
| () -> ExoPlaybackException.createForRenderer( | ||
| new Exception(defaultMsg), | ||
| "Dummy renderer", | ||
| 0, | ||
| null, | ||
| C.FORMAT_HANDLED | ||
| ) | ||
| ); | ||
| exceptionTypes.put( | ||
| "Unexpected", | ||
| () -> ExoPlaybackException.createForUnexpected( | ||
| new RuntimeException(defaultMsg) | ||
| ) | ||
| ); | ||
| exceptionTypes.put( | ||
| "Remote", | ||
| () -> ExoPlaybackException.createForRemote(defaultMsg) | ||
| ); | ||
|
|
||
| return Collections.unmodifiableMap(exceptionTypes); | ||
| } | ||
|
|
||
| private static Context getThemeWrapperContext(final Context context) { | ||
| return new ContextThemeWrapper( | ||
| context, | ||
| ThemeHelper.isLightThemeSelected(context) | ||
| ? R.style.LightTheme | ||
| : R.style.DarkTheme); | ||
| } | ||
|
|
||
| public static void onCrashThePlayer(final Player player, final LayoutInflater layoutInflater) { | ||
| final Context context = player.getContext(); | ||
| if (!isPlayerAvailable(player)) { | ||
litetex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Log.d(TAG, "Player is not available"); | ||
| Toast.makeText(context, "Player is not available", Toast.LENGTH_SHORT) | ||
| .show(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // -- Build the dialog/UI -- | ||
|
|
||
| final Context themeWrapperContext = getThemeWrapperContext(context); | ||
|
|
||
| final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext); | ||
| final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(layoutInflater) | ||
| .list; | ||
|
|
||
| final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext(context)) | ||
| .setTitle("Choose an exception") | ||
| .setView(radioGroup) | ||
| .setCancelable(true) | ||
| .setNegativeButton(R.string.cancel, null) | ||
| .create(); | ||
|
|
||
| for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry | ||
| : AVAILABLE_EXCEPTION_TYPES.entrySet()) { | ||
| final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot(); | ||
| radioButton.setText(entry.getKey()); | ||
| radioButton.setChecked(false); | ||
| radioButton.setLayoutParams( | ||
| new RadioGroup.LayoutParams( | ||
| ViewGroup.LayoutParams.MATCH_PARENT, | ||
| ViewGroup.LayoutParams.WRAP_CONTENT | ||
| ) | ||
| ); | ||
| radioButton.setOnClickListener(v -> { | ||
| tryCrashPlayerWith(player, entry.getValue().get()); | ||
| if (alertDialog != null) { | ||
| alertDialog.cancel(); | ||
| } | ||
| }); | ||
| radioGroup.addView(radioButton); | ||
| } | ||
|
|
||
| alertDialog.show(); | ||
| } | ||
|
|
||
| /** | ||
| * Note that this method does not crash the underlying exoplayer directly (it's not possible). | ||
| * It simply supplies a Exception to {@link Player#onPlayerError(ExoPlaybackException)}. | ||
| * @param player | ||
| * @param exception | ||
| */ | ||
| private static void tryCrashPlayerWith( | ||
| @NonNull final Player player, | ||
| @NonNull final ExoPlaybackException exception | ||
| ) { | ||
| Log.d(TAG, "Crashing the player using player.onPlayerError(ex)"); | ||
| try { | ||
| player.onPlayerError(exception); | ||
| } catch (final Exception exPlayer) { | ||
| Log.e(TAG, | ||
| "Run into an exception while crashing the player:", | ||
| exPlayer); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isPlayerAvailable(final Player player) { | ||
| return player != null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.