Make network-based DataSource implementations use ErrorCode.

PiperOrigin-RevId: 384666131
This commit is contained in:
claincly 2021-07-14 12:05:27 +01:00 committed by Oliver Woodman
parent f9f93c5a49
commit 6512463280
3 changed files with 35 additions and 15 deletions

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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;