LINE.apk(点击下载) / HttpMediaDrmCallback.java


package ai.clova.cic.clientlib.exoplayer2.drm;

import ai.clova.cic.clientlib.exoplayer2.C;
import ai.clova.cic.clientlib.exoplayer2.drm.ExoMediaDrm;
import ai.clova.cic.clientlib.exoplayer2.upstream.DataSourceInputStream;
import ai.clova.cic.clientlib.exoplayer2.upstream.DataSpec;
import ai.clova.cic.clientlib.exoplayer2.upstream.HttpDataSource;
import ai.clova.cic.clientlib.exoplayer2.util.Assertions;
import ai.clova.cic.clientlib.exoplayer2.util.Util;
import android.annotation.TargetApi;
import android.net.Uri;
import android.text.TextUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@TargetApi(18)
public final class HttpMediaDrmCallback implements MediaDrmCallback {
    private static final int MAX_MANUAL_REDIRECTS = 5;
    private final HttpDataSource.Factory dataSourceFactory;
    private final String defaultLicenseUrl;
    private final boolean forceDefaultLicenseUrl;
    private final Map<String, String> keyRequestProperties;

    public HttpMediaDrmCallback(String str, HttpDataSource.Factory factory) {
        this(str, false, factory);
    }

    public HttpMediaDrmCallback(String str, boolean z14, HttpDataSource.Factory factory) {
        this.dataSourceFactory = factory;
        this.defaultLicenseUrl = str;
        this.forceDefaultLicenseUrl = z14;
        this.keyRequestProperties = new HashMap();
    }

    /* JADX INFO: finally extract failed */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x006d A[Catch:{ InvalidResponseCodeException -> 0x0053, all -> 0x0051 }] */
    /* JADX WARNING: Removed duplicated region for block: B:27:0x0072  */
    /* JADX WARNING: Removed duplicated region for block: B:29:0x0075 A[LOOP:1: B:7:0x002d->B:29:0x0075, LOOP_END] */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x007e A[SYNTHETIC] */
    private static byte[] executePost(HttpDataSource.Factory factory, String str, byte[] bArr, Map<String, String> map) throws IOException {
        boolean z14;
        int i14;
        String redirectUrl;
        HttpDataSource createDataSource = factory.createDataSource();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                createDataSource.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        String str2 = str;
        int i15 = 0;
        while (true) {
            DataSourceInputStream dataSourceInputStream = new DataSourceInputStream(createDataSource, new DataSpec(Uri.parse(str2), 2, bArr, 0, 0, -1, null, 1));
            try {
                byte[] byteArray = Util.toByteArray(dataSourceInputStream);
                Util.closeQuietly(dataSourceInputStream);
                return byteArray;
            } catch (HttpDataSource.InvalidResponseCodeException e14) {
                if (e14.responseCode == 307 || e14.responseCode == 308) {
                    i14 = i15 + 1;
                    if (i15 < 5) {
                        z14 = true;
                        redirectUrl = !z14 ? getRedirectUrl(e14) : null;
                        if (redirectUrl == null) {
                            Util.closeQuietly(dataSourceInputStream);
                            i15 = i14;
                            str2 = redirectUrl;
                        } else {
                            throw e14;
                        }
                    } else {
                        i15 = i14;
                    }
                }
                i14 = i15;
                z14 = false;
                if (!z14) {
                }
                if (redirectUrl == null) {
                }
            } catch (Throwable th4) {
                Util.closeQuietly(dataSourceInputStream);
                throw th4;
            }
        }
    }

    private static String getRedirectUrl(HttpDataSource.InvalidResponseCodeException invalidResponseCodeException) {
        List<String> list;
        Map<String, List<String>> map = invalidResponseCodeException.headerFields;
        if (map == null || (list = map.get("Location")) == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public void clearAllKeyRequestProperties() {
        synchronized (this.keyRequestProperties) {
            this.keyRequestProperties.clear();
        }
    }

    public void clearKeyRequestProperty(String str) {
        Assertions.checkNotNull(str);
        synchronized (this.keyRequestProperties) {
            this.keyRequestProperties.remove(str);
        }
    }

    @Override // ai.clova.cic.clientlib.exoplayer2.drm.MediaDrmCallback
    public byte[] executeKeyRequest(UUID uuid, ExoMediaDrm.KeyRequest keyRequest) throws Exception {
        String licenseServerUrl = keyRequest.getLicenseServerUrl();
        if (this.forceDefaultLicenseUrl || TextUtils.isEmpty(licenseServerUrl)) {
            licenseServerUrl = this.defaultLicenseUrl;
        }
        HashMap hashMap = new HashMap();
        UUID uuid2 = C.PLAYREADY_UUID;
        hashMap.put("Content-Type", uuid2.equals(uuid) ? "text/xml" : C.CLEARKEY_UUID.equals(uuid) ? "application/json" : "application/octet-stream");
        if (uuid2.equals(uuid)) {
            hashMap.put("SOAPAction", "http://schemas.microsoft.com/DRM/2007/03/protocols/AcquireLicense");
        }
        synchronized (this.keyRequestProperties) {
            hashMap.putAll(this.keyRequestProperties);
        }
        return executePost(this.dataSourceFactory, licenseServerUrl, keyRequest.getData(), hashMap);
    }

    @Override // ai.clova.cic.clientlib.exoplayer2.drm.MediaDrmCallback
    public byte[] executeProvisionRequest(UUID uuid, ExoMediaDrm.ProvisionRequest provisionRequest) throws IOException {
        return executePost(this.dataSourceFactory, provisionRequest.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(provisionRequest.getData()), null, null);
    }

    public void setKeyRequestProperty(String str, String str2) {
        Assertions.checkNotNull(str);
        Assertions.checkNotNull(str2);
        synchronized (this.keyRequestProperties) {
            this.keyRequestProperties.put(str, str2);
        }
    }
}