Remove bundle-serialization methods from ExoPlaybackException

The `toBundle()` method is implemented incorrectly as it uses
`parcelable` implementation to bundle `Format.metadata`.
The `parcelable` is not compatible and is likely to cause crashes if
a new field is added or removed.

Moreover, the `fromBundle` method is likely unused as in session
module the exception is [unbundled](df887a9422/libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java (L1021)) as `PlaybackException`.

This is a non breaking change as all the calls to `toBundle()` and
`fromBundle` will now go to the parent class `PlaybackException`.

If this specific bundling is required in future then `Format.Metadata`
and all the `Metadata.Entry` classes need to provide `toBundle()`
and `fromBundle()` method.

PiperOrigin-RevId: 705167002
This commit is contained in:
sheenachhabra 2024-12-11 11:02:04 -08:00 committed by Copybara-Service
parent 3936c27b6d
commit f587ac2a67
2 changed files with 0 additions and 145 deletions

View File

@ -254,19 +254,6 @@ public final class ExoPlaybackException extends PlaybackException {
isRecoverable);
}
private ExoPlaybackException(Bundle bundle) {
super(bundle);
type = bundle.getInt(FIELD_TYPE, /* defaultValue= */ TYPE_UNEXPECTED);
rendererName = bundle.getString(FIELD_RENDERER_NAME);
rendererIndex = bundle.getInt(FIELD_RENDERER_INDEX, /* defaultValue= */ C.INDEX_UNSET);
@Nullable Bundle rendererFormatBundle = bundle.getBundle(FIELD_RENDERER_FORMAT);
rendererFormat = rendererFormatBundle == null ? null : Format.fromBundle(rendererFormatBundle);
rendererFormatSupport =
bundle.getInt(FIELD_RENDERER_FORMAT_SUPPORT, /* defaultValue= */ C.FORMAT_HANDLED);
isRecoverable = bundle.getBoolean(FIELD_IS_RECOVERABLE, /* defaultValue= */ false);
mediaPeriodId = null;
}
private ExoPlaybackException(
String message,
@Nullable Throwable cause,
@ -399,44 +386,4 @@ public final class ExoPlaybackException extends PlaybackException {
}
return message;
}
/** Restores a {@code ExoPlaybackException} from a {@link Bundle}. */
@UnstableApi
public static ExoPlaybackException fromBundle(Bundle bundle) {
return new ExoPlaybackException(bundle);
}
private static final String FIELD_TYPE = Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 1);
private static final String FIELD_RENDERER_NAME =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 2);
private static final String FIELD_RENDERER_INDEX =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 3);
private static final String FIELD_RENDERER_FORMAT =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 4);
private static final String FIELD_RENDERER_FORMAT_SUPPORT =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 5);
private static final String FIELD_IS_RECOVERABLE =
Util.intToStringMaxRadix(FIELD_CUSTOM_ID_BASE + 6);
/**
* {@inheritDoc}
*
* <p>It omits the {@link #mediaPeriodId} field. The {@link #mediaPeriodId} of an instance
* restored by {@link #fromBundle} will always be {@code null}.
*/
@UnstableApi
@Override
public Bundle toBundle() {
Bundle bundle = super.toBundle();
bundle.putInt(FIELD_TYPE, type);
bundle.putString(FIELD_RENDERER_NAME, rendererName);
bundle.putInt(FIELD_RENDERER_INDEX, rendererIndex);
if (rendererFormat != null) {
bundle.putBundle(
FIELD_RENDERER_FORMAT, rendererFormat.toBundle(/* excludeMetadata= */ false));
}
bundle.putInt(FIELD_RENDERER_FORMAT_SUPPORT, rendererFormatSupport);
bundle.putBoolean(FIELD_IS_RECOVERABLE, isRecoverable);
return bundle;
}
}

View File

@ -1,92 +0,0 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.exoplayer;
import static com.google.common.truth.Truth.assertThat;
import android.os.RemoteException;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.util.Util;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Unit tests for {@link ExoPlaybackException}. */
@RunWith(AndroidJUnit4.class)
public class ExoPlaybackExceptionTest {
@Test
public void roundTripViaBundle_ofExoPlaybackExceptionTypeRemote_yieldsEqualInstance() {
ExoPlaybackException before = ExoPlaybackException.createForRemote(/* message= */ "test");
ExoPlaybackException after = ExoPlaybackException.fromBundle(before.toBundle());
assertThat(areExoPlaybackExceptionsEqual(before, after)).isTrue();
}
@Test
public void roundTripViaBundle_ofExoPlaybackExceptionTypeRenderer_yieldsEqualInstance() {
ExoPlaybackException before =
ExoPlaybackException.createForRenderer(
new IllegalStateException("ExoPlaybackExceptionTest"),
/* rendererName= */ "rendererName",
/* rendererIndex= */ 123,
/* rendererFormat= */ new Format.Builder().setCodecs("anyCodec").build(),
/* rendererFormatSupport= */ C.FORMAT_UNSUPPORTED_SUBTYPE,
/* isRecoverable= */ true,
/* errorCode= */ PlaybackException.ERROR_CODE_DECODER_INIT_FAILED);
ExoPlaybackException after = ExoPlaybackException.fromBundle(before.toBundle());
assertThat(areExoPlaybackExceptionsEqual(before, after)).isTrue();
}
@Test
public void
roundTripViaBundle_ofExoPlaybackExceptionTypeUnexpectedWithPrivateCause_yieldsRemoteExceptionWithSameMessage() {
ExoPlaybackException before =
ExoPlaybackException.createForUnexpected(
new RuntimeException(
/* message= */ "anonymous exception that class loader cannot know") {},
PlaybackException.ERROR_CODE_TIMEOUT);
ExoPlaybackException after = ExoPlaybackException.fromBundle(before.toBundle());
assertThat(after.getCause()).isInstanceOf(RemoteException.class);
assertThat(after.getCause()).hasMessageThat().isEqualTo(before.getCause().getMessage());
}
private static boolean areExoPlaybackExceptionsEqual(
ExoPlaybackException a, ExoPlaybackException b) {
if (a == null || b == null) {
return a == b;
}
return Util.areEqual(a.getMessage(), b.getMessage())
&& a.type == b.type
&& Util.areEqual(a.rendererName, b.rendererName)
&& a.rendererIndex == b.rendererIndex
&& Util.areEqual(a.rendererFormat, b.rendererFormat)
&& a.rendererFormatSupport == b.rendererFormatSupport
&& a.timestampMs == b.timestampMs
&& a.isRecoverable == b.isRecoverable
&& areThrowablesEqual(a.getCause(), b.getCause());
}
private static boolean areThrowablesEqual(Throwable a, Throwable b) {
if (a == null || b == null) {
return a == b;
}
return a.getClass() == b.getClass() && Util.areEqual(a.getMessage(), b.getMessage());
}
}