From 6512463280b161a77c59754db7632fcebdea0988 Mon Sep 17 00:00:00 2001 From: claincly Date: Wed, 14 Jul 2021 12:05:27 +0100 Subject: [PATCH] Make network-based DataSource implementations use ErrorCode. PiperOrigin-RevId: 384666131 --- .../upstream/DefaultHttpDataSource.java | 12 +++---- .../exoplayer2/upstream/UdpDataSource.java | 34 +++++++++++++++---- .../rtsp/UdpDataSourceRtpDataChannel.java | 4 +-- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java index 421937e3ce..7a3c1efc25 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java @@ -465,13 +465,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou } } catch (IOException e) { closeConnectionQuietly(); - - @PlaybackException.ErrorCode int errorCode = PlaybackException.ERROR_CODE_IO_UNSPECIFIED; - if (e instanceof DataSourceException) { - errorCode = ((DataSourceException) e).reason; - } - - throw new HttpDataSourceException(e, dataSpec, errorCode, HttpDataSourceException.TYPE_OPEN); + throw new HttpDataSourceException( + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_OPEN); } return bytesToRead; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/UdpDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/UdpDataSource.java index 2da837e788..5b82f50715 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/UdpDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/UdpDataSource.java @@ -20,22 +20,32 @@ import static java.lang.Math.min; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; +import java.net.PortUnreachableException; import java.net.SocketException; +import java.net.SocketTimeoutException; /** A UDP {@link DataSource}. */ public final class UdpDataSource extends BaseDataSource { /** Thrown when an error is encountered when trying to read from a {@link UdpDataSource}. */ - public static final class UdpDataSourceException extends IOException { + public static final class UdpDataSourceException extends DataSourceException { - public UdpDataSourceException(IOException cause) { - super(cause); + /** + * Creates a {@code UdpDataSourceException}. + * + * @param cause The error cause. + * @param errorCode Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. + */ + public UdpDataSourceException(IOException cause, @PlaybackException.ErrorCode int errorCode) { + super(cause, errorCode); } } @@ -104,13 +114,14 @@ public final class UdpDataSource extends BaseDataSource { socket = new DatagramSocket(socketAddress); } } catch (IOException e) { - throw new UdpDataSourceException(e); + throw new UdpDataSourceException( + e, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED); } try { socket.setSoTimeout(socketTimeoutMillis); } catch (SocketException e) { - throw new UdpDataSourceException(e); + throw new UdpDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } opened = true; @@ -129,7 +140,7 @@ public final class UdpDataSource extends BaseDataSource { try { socket.receive(packet); } catch (IOException e) { - throw new UdpDataSourceException(e); + throw createReadException(e); } packetRemaining = packet.getLength(); bytesTransferred(packetRemaining); @@ -182,4 +193,15 @@ public final class UdpDataSource extends BaseDataSource { } return socket.getLocalPort(); } + + private static UdpDataSourceException createReadException(IOException e) { + if (e instanceof PortUnreachableException) { + return new UdpDataSourceException(e, PlaybackException.ERROR_CODE_IO_NETWORK_UNAVAILABLE); + } else if (e instanceof SocketTimeoutException) { + return new UdpDataSourceException( + e, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT); + } else { + return new UdpDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); + } + } } diff --git a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannel.java b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannel.java index 95bf5027ca..315eab78b8 100644 --- a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannel.java +++ b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannel.java @@ -21,13 +21,13 @@ import static com.google.android.exoplayer2.util.Assertions.checkState; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.upstream.UdpDataSource; import com.google.android.exoplayer2.util.Util; import com.google.common.primitives.Ints; import java.io.IOException; -import java.net.SocketTimeoutException; /** An {@link RtpDataChannel} for UDP transport. */ /* package */ final class UdpDataSourceRtpDataChannel implements RtpDataChannel { @@ -98,7 +98,7 @@ import java.net.SocketTimeoutException; try { return dataSource.read(target, offset, length); } catch (UdpDataSource.UdpDataSourceException e) { - if (e.getCause() instanceof SocketTimeoutException) { + if (e.reason == PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT) { return C.RESULT_END_OF_INPUT; } else { throw e;