package com.google.android.gms.internal.ads;

import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.media3.common.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.SocketTimeoutException;

/* JADX INFO: loaded from: classes2.dex */
public final class ta2 extends qr1 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final byte[] f16831d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final DatagramPacket f16832e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f16833f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f16834g;

    @Nullable
    private Uri zzc;

    @Nullable
    private DatagramSocket zzd;

    @Nullable
    private MulticastSocket zze;

    @Nullable
    private InetAddress zzf;

    public ta2() {
        super(true);
        byte[] bArr = new byte[2000];
        this.f16831d = bArr;
        this.f16832e = new DatagramPacket(bArr, 0, 2000);
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzia {
        Uri uri = dy1Var.f10564a;
        this.zzc = uri;
        String host = uri.getHost();
        host.getClass();
        int port = this.zzc.getPort();
        l(dy1Var);
        try {
            this.zzf = InetAddress.getByName(host);
            InetSocketAddress inetSocketAddress = new InetSocketAddress(this.zzf, port);
            if (this.zzf.isMulticastAddress()) {
                MulticastSocket multicastSocket = new MulticastSocket(inetSocketAddress);
                this.zze = multicastSocket;
                multicastSocket.joinGroup(this.zzf);
                this.zzd = this.zze;
            } else {
                this.zzd = new DatagramSocket(inetSocketAddress);
            }
            this.zzd.setSoTimeout(8000);
            this.f16833f = true;
            m(dy1Var);
            return -1L;
        } catch (IOException e10) {
            throw new zzia(e10, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED);
        } catch (SecurityException e11) {
            throw new zzia(e11, PlaybackException.ERROR_CODE_IO_NO_PERMISSION);
        }
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws zzia {
        if (i11 == 0) {
            return 0;
        }
        int i12 = this.f16834g;
        DatagramPacket datagramPacket = this.f16832e;
        if (i12 == 0) {
            try {
                DatagramSocket datagramSocket = this.zzd;
                datagramSocket.getClass();
                datagramSocket.receive(datagramPacket);
                int length = datagramPacket.getLength();
                this.f16834g = length;
                n(length);
            } catch (SocketTimeoutException e10) {
                throw new zzia(e10, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT);
            } catch (IOException e11) {
                throw new zzia(e11, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED);
            }
        }
        int length2 = datagramPacket.getLength();
        int i13 = this.f16834g;
        int iMin = Math.min(i13, i11);
        System.arraycopy(this.f16831d, length2 - i13, bArr, i10, iMin);
        this.f16834g -= iMin;
        return iMin;
    }

    @Override // com.google.android.gms.internal.ads.qr1, com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    @Nullable
    public final Uri zzc() {
        return this.zzc;
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() {
        this.zzc = null;
        MulticastSocket multicastSocket = this.zze;
        if (multicastSocket != null) {
            try {
                InetAddress inetAddress = this.zzf;
                inetAddress.getClass();
                multicastSocket.leaveGroup(inetAddress);
            } catch (IOException unused) {
            }
            this.zze = null;
        }
        DatagramSocket datagramSocket = this.zzd;
        if (datagramSocket != null) {
            datagramSocket.close();
            this.zzd = null;
        }
        this.zzf = null;
        this.f16834g = 0;
        if (this.f16833f) {
            this.f16833f = false;
            o();
        }
    }
}
