package com.google.android.exoplayer2.drm; import android.net.Uri; import android.text.TextUtils; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.drm.ExoMediaDrm; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSourceInputStream; import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.StatsDataSource; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import com.google.common.collect.ImmutableMap; import com.google.common.net.HttpHeaders; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; /* loaded from: classes2.dex */ public final class HttpMediaDrmCallback implements MediaDrmCallback { private static final int MAX_MANUAL_REDIRECTS = 5; private final DataSource.Factory dataSourceFactory; private final String defaultLicenseUrl; private final boolean forceDefaultLicenseUrl; private final Map keyRequestProperties; public HttpMediaDrmCallback(String str, DataSource.Factory factory) { this(str, false, factory); } public HttpMediaDrmCallback(String str, boolean z, DataSource.Factory factory) { Assertions.checkArgument((z && TextUtils.isEmpty(str)) ? false : true); this.dataSourceFactory = factory; this.defaultLicenseUrl = str; this.forceDefaultLicenseUrl = z; this.keyRequestProperties = new HashMap(); } public void setKeyRequestProperty(String str, String str2) { Assertions.checkNotNull(str); Assertions.checkNotNull(str2); synchronized (this.keyRequestProperties) { this.keyRequestProperties.put(str, str2); } } public void clearKeyRequestProperty(String str) { Assertions.checkNotNull(str); synchronized (this.keyRequestProperties) { this.keyRequestProperties.remove(str); } } public void clearAllKeyRequestProperties() { synchronized (this.keyRequestProperties) { this.keyRequestProperties.clear(); } } @Override // com.google.android.exoplayer2.drm.MediaDrmCallback public byte[] executeProvisionRequest(UUID uuid, ExoMediaDrm.ProvisionRequest provisionRequest) throws MediaDrmCallbackException { return executePost(this.dataSourceFactory, provisionRequest.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(provisionRequest.getData()), null, Collections.emptyMap()); } @Override // com.google.android.exoplayer2.drm.MediaDrmCallback public byte[] executeKeyRequest(UUID uuid, ExoMediaDrm.KeyRequest keyRequest) throws MediaDrmCallbackException { String str; String licenseServerUrl = keyRequest.getLicenseServerUrl(); if (this.forceDefaultLicenseUrl || TextUtils.isEmpty(licenseServerUrl)) { licenseServerUrl = this.defaultLicenseUrl; } if (TextUtils.isEmpty(licenseServerUrl)) { throw new MediaDrmCallbackException(new DataSpec.Builder().setUri(Uri.EMPTY).build(), Uri.EMPTY, ImmutableMap.of(), 0L, new IllegalStateException("No license URL")); } HashMap hashMap = new HashMap(); if (C.PLAYREADY_UUID.equals(uuid)) { str = "text/xml"; } else { str = C.CLEARKEY_UUID.equals(uuid) ? "application/json" : "application/octet-stream"; } hashMap.put(HttpHeaders.CONTENT_TYPE, str); if (C.PLAYREADY_UUID.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); } private static byte[] executePost(DataSource.Factory factory, String str, byte[] bArr, Map map) throws MediaDrmCallbackException { StatsDataSource statsDataSource = new StatsDataSource(factory.createDataSource()); DataSpec build = new DataSpec.Builder().setUri(str).setHttpRequestHeaders(map).setHttpMethod(2).setHttpBody(bArr).setFlags(1).build(); int i = 0; DataSpec dataSpec = build; while (true) { try { DataSourceInputStream dataSourceInputStream = new DataSourceInputStream(statsDataSource, dataSpec); try { return Util.toByteArray(dataSourceInputStream); } catch (HttpDataSource.InvalidResponseCodeException e) { String redirectUrl = getRedirectUrl(e, i); if (redirectUrl == null) { throw e; } i++; dataSpec = dataSpec.buildUpon().setUri(redirectUrl).build(); } finally { Util.closeQuietly(dataSourceInputStream); } } catch (Exception e2) { throw new MediaDrmCallbackException(build, (Uri) Assertions.checkNotNull(statsDataSource.getLastOpenedUri()), statsDataSource.getResponseHeaders(), statsDataSource.getBytesRead(), e2); } } } private static String getRedirectUrl(HttpDataSource.InvalidResponseCodeException invalidResponseCodeException, int i) { Map> map; List list; if ((invalidResponseCodeException.responseCode != 307 && invalidResponseCodeException.responseCode != 308) || i >= 5 || (map = invalidResponseCodeException.headerFields) == null || (list = map.get(HttpHeaders.LOCATION)) == null || list.isEmpty()) { return null; } return list.get(0); } }