Rabbit-R1/android (non root)/java/sources/androidx/media3/datasource/UdpDataSource.java
2024-05-21 17:08:36 -04:00

141 lines
4.7 KiB
Java

package androidx.media3.datasource;
import android.net.Uri;
import androidx.media3.common.util.Assertions;
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.SocketTimeoutException;
/* loaded from: classes2.dex */
public final class UdpDataSource extends BaseDataSource {
public static final int DEFAULT_MAX_PACKET_SIZE = 2000;
public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 8000;
public static final int UDP_PORT_UNSET = -1;
private InetAddress address;
private MulticastSocket multicastSocket;
private boolean opened;
private final DatagramPacket packet;
private final byte[] packetBuffer;
private int packetRemaining;
private DatagramSocket socket;
private final int socketTimeoutMillis;
private Uri uri;
@Override // androidx.media3.datasource.DataSource
public Uri getUri() {
return this.uri;
}
/* loaded from: classes2.dex */
public static final class UdpDataSourceException extends DataSourceException {
public UdpDataSourceException(Throwable th, int i) {
super(th, i);
}
}
public UdpDataSource() {
this(2000);
}
public UdpDataSource(int i) {
this(i, 8000);
}
public UdpDataSource(int i, int i2) {
super(true);
this.socketTimeoutMillis = i2;
byte[] bArr = new byte[i];
this.packetBuffer = bArr;
this.packet = new DatagramPacket(bArr, 0, i);
}
@Override // androidx.media3.datasource.DataSource
public long open(DataSpec dataSpec) throws UdpDataSourceException {
Uri uri = dataSpec.uri;
this.uri = uri;
String str = (String) Assertions.checkNotNull(uri.getHost());
int port = this.uri.getPort();
transferInitializing(dataSpec);
try {
this.address = InetAddress.getByName(str);
InetSocketAddress inetSocketAddress = new InetSocketAddress(this.address, port);
if (this.address.isMulticastAddress()) {
MulticastSocket multicastSocket = new MulticastSocket(inetSocketAddress);
this.multicastSocket = multicastSocket;
multicastSocket.joinGroup(this.address);
this.socket = this.multicastSocket;
} else {
this.socket = new DatagramSocket(inetSocketAddress);
}
this.socket.setSoTimeout(this.socketTimeoutMillis);
this.opened = true;
transferStarted(dataSpec);
return -1L;
} catch (IOException e) {
throw new UdpDataSourceException(e, 2001);
} catch (SecurityException e2) {
throw new UdpDataSourceException(e2, 2006);
}
}
@Override // androidx.media3.common.DataReader
public int read(byte[] bArr, int i, int i2) throws UdpDataSourceException {
if (i2 == 0) {
return 0;
}
if (this.packetRemaining == 0) {
try {
((DatagramSocket) Assertions.checkNotNull(this.socket)).receive(this.packet);
int length = this.packet.getLength();
this.packetRemaining = length;
bytesTransferred(length);
} catch (SocketTimeoutException e) {
throw new UdpDataSourceException(e, 2002);
} catch (IOException e2) {
throw new UdpDataSourceException(e2, 2001);
}
}
int length2 = this.packet.getLength();
int i3 = this.packetRemaining;
int min = Math.min(i3, i2);
System.arraycopy(this.packetBuffer, length2 - i3, bArr, i, min);
this.packetRemaining -= min;
return min;
}
@Override // androidx.media3.datasource.DataSource
public void close() {
this.uri = null;
MulticastSocket multicastSocket = this.multicastSocket;
if (multicastSocket != null) {
try {
multicastSocket.leaveGroup((InetAddress) Assertions.checkNotNull(this.address));
} catch (IOException unused) {
}
this.multicastSocket = null;
}
DatagramSocket datagramSocket = this.socket;
if (datagramSocket != null) {
datagramSocket.close();
this.socket = null;
}
this.address = null;
this.packetRemaining = 0;
if (this.opened) {
this.opened = false;
transferEnded();
}
}
public int getLocalPort() {
DatagramSocket datagramSocket = this.socket;
if (datagramSocket == null) {
return -1;
}
return datagramSocket.getLocalPort();
}
}