Commit 65124632 by claincly Committed by Oliver Woodman

Make network-based DataSource implementations use ErrorCode.

PiperOrigin-RevId: 384666131
parent f9f93c5a
...@@ -465,13 +465,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou ...@@ -465,13 +465,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
} }
} catch (IOException e) { } catch (IOException e) {
closeConnectionQuietly(); closeConnectionQuietly();
throw new HttpDataSourceException(
@PlaybackException.ErrorCode int errorCode = PlaybackException.ERROR_CODE_IO_UNSPECIFIED; e,
if (e instanceof DataSourceException) { dataSpec,
errorCode = ((DataSourceException) e).reason; PlaybackException.ERROR_CODE_IO_UNSPECIFIED,
} HttpDataSourceException.TYPE_OPEN);
throw new HttpDataSourceException(e, dataSpec, errorCode, HttpDataSourceException.TYPE_OPEN);
} }
return bytesToRead; return bytesToRead;
......
...@@ -20,22 +20,32 @@ import static java.lang.Math.min; ...@@ -20,22 +20,32 @@ import static java.lang.Math.min;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.PlaybackException;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
import java.net.PortUnreachableException;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException;
/** A UDP {@link DataSource}. */ /** A UDP {@link DataSource}. */
public final class UdpDataSource extends BaseDataSource { public final class UdpDataSource extends BaseDataSource {
/** Thrown when an error is encountered when trying to read from a {@link UdpDataSource}. */ /** 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 { ...@@ -104,13 +114,14 @@ public final class UdpDataSource extends BaseDataSource {
socket = new DatagramSocket(socketAddress); socket = new DatagramSocket(socketAddress);
} }
} catch (IOException e) { } catch (IOException e) {
throw new UdpDataSourceException(e); throw new UdpDataSourceException(
e, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED);
} }
try { try {
socket.setSoTimeout(socketTimeoutMillis); socket.setSoTimeout(socketTimeoutMillis);
} catch (SocketException e) { } catch (SocketException e) {
throw new UdpDataSourceException(e); throw new UdpDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
} }
opened = true; opened = true;
...@@ -129,7 +140,7 @@ public final class UdpDataSource extends BaseDataSource { ...@@ -129,7 +140,7 @@ public final class UdpDataSource extends BaseDataSource {
try { try {
socket.receive(packet); socket.receive(packet);
} catch (IOException e) { } catch (IOException e) {
throw new UdpDataSourceException(e); throw createReadException(e);
} }
packetRemaining = packet.getLength(); packetRemaining = packet.getLength();
bytesTransferred(packetRemaining); bytesTransferred(packetRemaining);
...@@ -182,4 +193,15 @@ public final class UdpDataSource extends BaseDataSource { ...@@ -182,4 +193,15 @@ public final class UdpDataSource extends BaseDataSource {
} }
return socket.getLocalPort(); 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);
}
}
} }
...@@ -21,13 +21,13 @@ import static com.google.android.exoplayer2.util.Assertions.checkState; ...@@ -21,13 +21,13 @@ import static com.google.android.exoplayer2.util.Assertions.checkState;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; 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.DataSpec;
import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.upstream.UdpDataSource; import com.google.android.exoplayer2.upstream.UdpDataSource;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import java.io.IOException; import java.io.IOException;
import java.net.SocketTimeoutException;
/** An {@link RtpDataChannel} for UDP transport. */ /** An {@link RtpDataChannel} for UDP transport. */
/* package */ final class UdpDataSourceRtpDataChannel implements RtpDataChannel { /* package */ final class UdpDataSourceRtpDataChannel implements RtpDataChannel {
...@@ -98,7 +98,7 @@ import java.net.SocketTimeoutException; ...@@ -98,7 +98,7 @@ import java.net.SocketTimeoutException;
try { try {
return dataSource.read(target, offset, length); return dataSource.read(target, offset, length);
} catch (UdpDataSource.UdpDataSourceException e) { } 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; return C.RESULT_END_OF_INPUT;
} else { } else {
throw e; throw e;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment