From e5d028c425578b2cb37fb0571b2b57f5134109f5 Mon Sep 17 00:00:00 2001 From: olly Date: Wed, 6 Apr 2016 11:21:19 -0700 Subject: [PATCH] Merge https://github.com/google/ExoPlayer/pull/1397. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=119182077 --- .../exoplayer/upstream/UdpDataSource.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/UdpDataSource.java b/library/src/main/java/com/google/android/exoplayer/upstream/UdpDataSource.java index c5317e5f6d..ee333b56a4 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/UdpDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/UdpDataSource.java @@ -25,6 +25,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; +import java.net.SocketException; /** * A UDP {@link DataSource}. @@ -51,8 +52,14 @@ public final class UdpDataSource implements DataSource { */ public static final int DEFAULT_MAX_PACKET_SIZE = 2000; + /** + * The default socket timeout, in milliseconds. + */ + public static final int DEAFULT_SOCKET_TIMEOUT_MILLIS = 8 * 1000; + private final TransferListener listener; private final DatagramPacket packet; + private final int socketTimeoutMillis; private Uri uri; private DatagramSocket socket; @@ -76,7 +83,18 @@ public final class UdpDataSource implements DataSource { * @param maxPacketSize The maximum datagram packet size, in bytes. */ public UdpDataSource(TransferListener listener, int maxPacketSize) { + this(listener, maxPacketSize, DEAFULT_SOCKET_TIMEOUT_MILLIS); + } + + /** + * @param listener An optional listener. + * @param maxPacketSize The maximum datagram packet size, in bytes. + * @param socketTimeoutMillis The socket timeout in milliseconds. A timeout of zero is interpreted + * as an infinite timeout. + */ + public UdpDataSource(TransferListener listener, int maxPacketSize, int socketTimeoutMillis) { this.listener = listener; + this.socketTimeoutMillis = socketTimeoutMillis; packetBuffer = new byte[maxPacketSize]; packet = new DatagramPacket(packetBuffer, 0, maxPacketSize); } @@ -101,6 +119,12 @@ public final class UdpDataSource implements DataSource { throw new UdpDataSourceException(e); } + try { + socket.setSoTimeout(socketTimeoutMillis); + } catch (SocketException e) { + throw new UdpDataSourceException(e); + } + opened = true; if (listener != null) { listener.onTransferStart();