Skip to content

Commit

Permalink
update exo support 2.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Oct 26, 2020
1 parent 1ac5c4e commit 1bbcc30
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import androidx.annotation.VisibleForTesting;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.upstream.BaseDataSource;
import com.google.android.exoplayer2.upstream.DataSourceException;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DataSpec.HttpMethod;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Predicate;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Predicate;

import java.io.EOFException;
import java.io.IOException;
Expand Down Expand Up @@ -62,6 +63,9 @@
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import static java.lang.Math.max;
import static java.lang.Math.min;

/**
* An {@link HttpDataSource} that uses Android's {@link HttpURLConnection}.
*
Expand All @@ -76,6 +80,7 @@
*/
public class GSYDefaultHttpDataSource extends BaseDataSource implements HttpDataSource {


/**
* The default connection timeout, in milliseconds.
*/
Expand Down Expand Up @@ -120,13 +125,27 @@ public class GSYDefaultHttpDataSource extends BaseDataSource implements HttpData
private long bytesRead;

/**
* Creates an instance.
*/
public GSYDefaultHttpDataSource() {
this(
ExoPlayerLibraryInfo.DEFAULT_USER_AGENT,
DEFAULT_CONNECT_TIMEOUT_MILLIS,
DEFAULT_READ_TIMEOUT_MILLIS);
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
*/
public GSYDefaultHttpDataSource(String userAgent) {
this(userAgent, DEFAULT_CONNECT_TIMEOUT_MILLIS, DEFAULT_READ_TIMEOUT_MILLIS);
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
* interpreted as an infinite timeout.
Expand All @@ -143,6 +162,8 @@ public GSYDefaultHttpDataSource(String userAgent, int connectTimeoutMillis, int
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
* interpreted as an infinite timeout. Pass {@link #DEFAULT_CONNECT_TIMEOUT_MILLIS} to use the
Expand Down Expand Up @@ -170,13 +191,16 @@ public GSYDefaultHttpDataSource(
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
* #open(DataSpec)}.
* @deprecated Use {@link #GSYDefaultHttpDataSource(String)} and {@link
* #setContentTypePredicate(Predicate)}.
*/
@SuppressWarnings("deprecation")
@Deprecated
public GSYDefaultHttpDataSource(String userAgent, @Nullable Predicate<String> contentTypePredicate) {
this(
Expand All @@ -187,6 +211,8 @@ public GSYDefaultHttpDataSource(String userAgent, @Nullable Predicate<String> co
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
Expand Down Expand Up @@ -215,6 +241,8 @@ public GSYDefaultHttpDataSource(
}

/**
* Creates an instance.
*
* @param userAgent The User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
Expand Down Expand Up @@ -306,8 +334,8 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
try {
connection = makeConnection(dataSpec);
} catch (IOException e) {
throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e,
dataSpec, HttpDataSourceException.TYPE_OPEN);
throw new HttpDataSourceException(
"Unable to connect", e, dataSpec, HttpDataSourceException.TYPE_OPEN);
}

String responseMessage;
Expand All @@ -316,16 +344,27 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
responseMessage = connection.getResponseMessage();
} catch (IOException e) {
closeConnectionQuietly();
throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e,
dataSpec, HttpDataSourceException.TYPE_OPEN);
throw new HttpDataSourceException(
"Unable to connect", e, dataSpec, HttpDataSourceException.TYPE_OPEN);
}

// Check for a valid response code.
if (responseCode < 200 || responseCode > 299) {
Map<String, List<String>> headers = connection.getHeaderFields();
@Nullable InputStream errorStream = connection.getErrorStream();
byte[] errorResponseBody;
try {
errorResponseBody =
errorStream != null ? Util.toByteArray(errorStream) : Util.EMPTY_BYTE_ARRAY;
} catch (IOException e) {
throw new HttpDataSourceException(
"Error reading non-2xx response body", e, dataSpec, HttpDataSourceException.TYPE_OPEN);
}
closeConnectionQuietly();
InvalidResponseCodeException exception =
new InvalidResponseCodeException(responseCode, responseMessage, headers, dataSpec);
new InvalidResponseCodeException(
responseCode, responseMessage, headers, dataSpec, errorResponseBody);

if (responseCode == 416) {
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
}
Expand All @@ -334,7 +373,7 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {

// Check for a valid content type.
String contentType = connection.getContentType();
if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) {
if (contentTypePredicate != null && !contentTypePredicate.apply(contentType)) {
closeConnectionQuietly();
throw new InvalidContentTypeException(contentType, dataSpec);
}
Expand Down Expand Up @@ -413,8 +452,8 @@ public void close() throws HttpDataSourceException {
*
* @return The current open connection, or null.
*/
protected final @Nullable
HttpURLConnection getConnection() {
@Nullable
protected final HttpURLConnection getConnection() {
return connection;
}

Expand Down Expand Up @@ -456,7 +495,7 @@ protected final long bytesRemaining() {
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
URL url = new URL(dataSpec.uri.toString());
@HttpMethod int httpMethod = dataSpec.httpMethod;
byte[] httpBody = dataSpec.httpBody;
@Nullable byte[] httpBody = dataSpec.httpBody;
long position = dataSpec.position;
long length = dataSpec.length;
boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
Expand All @@ -475,6 +514,7 @@ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
dataSpec.httpRequestHeaders);
}


// We need to handle redirects ourselves to allow cross-protocol redirects.
int redirectCount = 0;
while (redirectCount++ <= MAX_REDIRECTS) {
Expand Down Expand Up @@ -523,7 +563,7 @@ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
*
* @param url The url to connect to.
* @param httpMethod The http method.
* @param httpBody The body data.
* @param httpBody The body data, or {@code null} if not required.
* @param position The byte offset of the requested data.
* @param length The length of the requested data, or {@link C#LENGTH_UNSET}.
* @param allowGzip Whether to allow the use of gzip.
Expand All @@ -533,7 +573,7 @@ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
private HttpURLConnection makeConnection(
URL url,
@HttpMethod int httpMethod,
byte[] httpBody,
@Nullable byte[] httpBody,
long position,
long length,
boolean allowGzip,
Expand Down Expand Up @@ -642,11 +682,11 @@ public boolean verify(String hostname, SSLSession session) {
* Handles a redirect.
*
* @param originalUrl The original URL.
* @param location The Location header in the response.
* @param location The Location header in the response. May be {@code null}.
* @return The next URL.
* @throws IOException If redirection isn't possible.
*/
private static URL handleRedirect(URL originalUrl, String location) throws IOException {
private static URL handleRedirect(URL originalUrl, @Nullable String location) throws IOException {
if (location == null) {
throw new ProtocolException("Null location redirect");
}
Expand Down Expand Up @@ -701,7 +741,7 @@ private static long getContentLength(HttpURLConnection connection) {
// increase it.
Log.w(TAG, "Inconsistent headers [" + contentLengthHeader + "] [" + contentRangeHeader
+ "]");
contentLength = Math.max(contentLength, contentLengthFromRange);
contentLength = max(contentLength, contentLengthFromRange);
}
} catch (NumberFormatException e) {
Log.e(TAG, "Unexpected Content-Range [" + contentRangeHeader + "]");
Expand Down Expand Up @@ -731,7 +771,7 @@ private void skipInternal() throws IOException {
}

while (bytesSkipped != bytesToSkip) {
int readLength = (int) Math.min(bytesToSkip - bytesSkipped, skipBuffer.length);
int readLength = (int) min(bytesToSkip - bytesSkipped, skipBuffer.length);
int read = inputStream.read(skipBuffer, 0, readLength);
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedIOException();
Expand Down Expand Up @@ -770,7 +810,7 @@ private int readInternal(byte[] buffer, int offset, int readLength) throws IOExc
if (bytesRemaining == 0) {
return C.RESULT_END_OF_INPUT;
}
readLength = (int) Math.min(readLength, bytesRemaining);
readLength = (int) min(readLength, bytesRemaining);
}

int read = inputStream.read(buffer, offset, readLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory;
import com.google.android.exoplayer2.upstream.cache.CacheSpan;
import com.google.android.exoplayer2.upstream.cache.CacheUtil;
import com.google.android.exoplayer2.upstream.cache.CacheWriter;
import com.google.android.exoplayer2.upstream.cache.ContentMetadata;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
Expand All @@ -41,6 +42,8 @@
import java.util.Map;
import java.util.NavigableSet;

import static com.google.android.exoplayer2.upstream.cache.CacheKeyFactory.DEFAULT;

/**
* Created by guoshuyu on 2018/5/18.
*/
Expand All @@ -56,6 +59,7 @@ public class ExoSourceManager {
private static Cache mCache;
/**
* 忽律Https证书校验
*
* @deprecated 如果需要忽略证书,请直接使用 ExoMediaSourceInterceptListener 的 getHttpDataSourceFactory
*/
@Deprecated
Expand Down Expand Up @@ -238,12 +242,12 @@ public static void clearCache(Context context, File cacheDir, String url) {
Cache cache = getCacheSingleInstance(context, cacheDir);
if (!TextUtils.isEmpty(url)) {
if (cache != null) {
CacheUtil.remove(cache, CacheUtil.generateKey(Uri.parse(url)));
removeCache(cache, url);
}
} else {
if (cache != null) {
for (String key : cache.getKeys()) {
CacheUtil.remove(cache, key);
removeCache(cache, key);
}
}
}
Expand All @@ -252,6 +256,24 @@ public static void clearCache(Context context, File cacheDir, String url) {
}
}


public static void removeCache(Cache cache, String url) {
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(buildCacheKey(url));
for (CacheSpan cachedSpan : cachedSpans) {
try {
cache.removeSpan(cachedSpan);
} catch (Exception e) {
// Do nothing.
}
}
}

public static String buildCacheKey(String url) {
DataSpec dataSpec = new DataSpec(Uri.parse(url));
String key = CacheKeyFactory.DEFAULT.buildCacheKey(dataSpec);
return key;
}

public static boolean cachePreView(Context context, File cacheDir, String url) {
return resolveCacheState(getCacheSingleInstance(context, cacheDir), url);
}
Expand All @@ -262,6 +284,7 @@ public boolean hadCached() {

/**
* 忽律Https证书校验
*
* @deprecated 如果需要忽略证书,请直接使用 ExoMediaSourceInterceptListener 的 getHttpDataSourceFactory
*/
@Deprecated
Expand Down Expand Up @@ -367,7 +390,7 @@ private DataSource.Factory getHttpDataSourceFactory(Context context, boolean pre
private static boolean resolveCacheState(Cache cache, String url) {
boolean isCache = true;
if (!TextUtils.isEmpty(url)) {
String key = CacheUtil.generateKey(Uri.parse(url));
String key = buildCacheKey(url);
if (!TextUtils.isEmpty(key)) {
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(key);
if (cachedSpans.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,51 +650,6 @@ public void onTracksChanged(EventTime eventTime, TrackGroupArray trackGroups, Tr

}

@Override
public void onLoadStarted(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData) {

}

@Override
public void onLoadCompleted(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData) {

}

@Override
public void onLoadCanceled(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData) {

}

@Override
public void onLoadError(EventTime eventTime, MediaSourceEventListener.LoadEventInfo loadEventInfo, MediaSourceEventListener.MediaLoadData mediaLoadData, IOException error, boolean wasCanceled) {

}

@Override
public void onDownstreamFormatChanged(EventTime eventTime, MediaSourceEventListener.MediaLoadData mediaLoadData) {

}

@Override
public void onUpstreamDiscarded(EventTime eventTime, MediaSourceEventListener.MediaLoadData mediaLoadData) {

}

@Override
public void onMediaPeriodCreated(EventTime eventTime) {

}

@Override
public void onMediaPeriodReleased(EventTime eventTime) {

}

@Override
public void onReadingStarted(EventTime eventTime) {

}

@Override
public void onBandwidthEstimate(EventTime eventTime, int totalLoadTimeMs, long totalBytesLoaded, long bitrateEstimate) {

Expand Down
Loading

0 comments on commit 1bbcc30

Please sign in to comment.