package e2;

import androidx.annotation.Nullable;
import androidx.media3.common.Metadata;
import com.google.common.base.h;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public final class a extends a2.c {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Pattern f25444c = Pattern.compile("(.+?)='(.*?)';", 32);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final CharsetDecoder f25445a = StandardCharsets.UTF_8.newDecoder();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final CharsetDecoder f25446b = StandardCharsets.ISO_8859_1.newDecoder();

    @Nullable
    private String decodeToString(ByteBuffer byteBuffer) {
        CharsetDecoder charsetDecoder = this.f25446b;
        CharsetDecoder charsetDecoder2 = this.f25445a;
        try {
            return charsetDecoder2.decode(byteBuffer).toString();
        } catch (CharacterCodingException unused) {
            try {
                String string = charsetDecoder.decode(byteBuffer).toString();
                charsetDecoder.reset();
                byteBuffer.rewind();
                return string;
            } catch (CharacterCodingException unused2) {
                charsetDecoder.reset();
                byteBuffer.rewind();
                return null;
            } catch (Throwable th) {
                charsetDecoder.reset();
                byteBuffer.rewind();
                throw th;
            }
        } finally {
            charsetDecoder2.reset();
            byteBuffer.rewind();
        }
    }

    @Override // a2.c
    public final Metadata decode(a2.b bVar, ByteBuffer byteBuffer) {
        String strDecodeToString = decodeToString(byteBuffer);
        byte[] bArr = new byte[byteBuffer.limit()];
        byteBuffer.get(bArr);
        String str = null;
        if (strDecodeToString == null) {
            return new Metadata(new c(bArr, null, null));
        }
        Matcher matcher = f25444c.matcher(strDecodeToString);
        String str2 = null;
        for (int iEnd = 0; matcher.find(iEnd); iEnd = matcher.end()) {
            String strGroup = matcher.group(1);
            String strGroup2 = matcher.group(2);
            if (strGroup != null) {
                String strQ = h.q(strGroup);
                strQ.getClass();
                if (strQ.equals("streamurl")) {
                    str2 = strGroup2;
                } else if (strQ.equals("streamtitle")) {
                    str = strGroup2;
                }
            }
        }
        return new Metadata(new c(bArr, str, str2));
    }
}
