Support for Cronet from GMSCore (Blaze only)

The blaze BUILD file for the Cronet extension now has three options:
Using native bundled Cronet libs, using GMSCore, or using whichever is newer.
The GMSCore version is preselected (as it is the smallest), but other variants
may be used by uncommenting the respective lines.

The API is the same for all cases and the CronetEngine.Builder automatically
selects the newest option or falls back to default http.

To avoid that apps using this extension need to add a dependency to Cronet
themselves, I added a CronetEngineFactory to the Exoplayer extension.

Gradle builds can't be supported (as far as I can see), as the GMSCore Cronet
version is first party only.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154812029
This commit is contained in:
tonihei 2017-05-02 02:34:59 -07:00 committed by Oliver Woodman
parent 6d2fa12e16
commit 94ffbb2b6a
3 changed files with 59 additions and 8 deletions

View file

@ -0,0 +1,5 @@
"""GCore versions supporting Cronet."""
GCORE_VERSIONS = [
"v10",
]

View file

@ -22,7 +22,6 @@ import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Predicate;
import java.util.concurrent.Executor;
import org.chromium.net.CronetEngine;
/**
* A {@link Factory} that produces {@link CronetDataSource}.
@ -34,13 +33,14 @@ public final class CronetDataSourceFactory extends BaseFactory {
*/
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS =
CronetDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS;
/**
* The default read timeout, in milliseconds.
*/
public static final int DEFAULT_READ_TIMEOUT_MILLIS =
CronetDataSource.DEFAULT_READ_TIMEOUT_MILLIS;
private final CronetEngine cronetEngine;
private final CronetEngineFactory cronetEngineFactory;
private final Executor executor;
private final Predicate<String> contentTypePredicate;
private final TransferListener<? super DataSource> transferListener;
@ -48,18 +48,18 @@ public final class CronetDataSourceFactory extends BaseFactory {
private final int readTimeoutMs;
private final boolean resetTimeoutOnRedirects;
public CronetDataSourceFactory(CronetEngine cronetEngine,
public CronetDataSourceFactory(CronetEngineFactory cronetEngineFactory,
Executor executor, Predicate<String> contentTypePredicate,
TransferListener<? super DataSource> transferListener) {
this(cronetEngine, executor, contentTypePredicate, transferListener,
this(cronetEngineFactory, executor, contentTypePredicate, transferListener,
DEFAULT_CONNECT_TIMEOUT_MILLIS, DEFAULT_READ_TIMEOUT_MILLIS, false);
}
public CronetDataSourceFactory(CronetEngine cronetEngine,
public CronetDataSourceFactory(CronetEngineFactory cronetEngineFactory,
Executor executor, Predicate<String> contentTypePredicate,
TransferListener<? super DataSource> transferListener, int connectTimeoutMs,
int readTimeoutMs, boolean resetTimeoutOnRedirects) {
this.cronetEngine = cronetEngine;
this.cronetEngineFactory = cronetEngineFactory;
this.executor = executor;
this.contentTypePredicate = contentTypePredicate;
this.transferListener = transferListener;
@ -71,8 +71,9 @@ public final class CronetDataSourceFactory extends BaseFactory {
@Override
protected CronetDataSource createDataSourceInternal(HttpDataSource.RequestProperties
defaultRequestProperties) {
return new CronetDataSource(cronetEngine, executor, contentTypePredicate, transferListener,
connectTimeoutMs, readTimeoutMs, resetTimeoutOnRedirects, defaultRequestProperties);
return new CronetDataSource(cronetEngineFactory.createCronetEngine(), executor,
contentTypePredicate, transferListener, connectTimeoutMs, readTimeoutMs,
resetTimeoutOnRedirects, defaultRequestProperties);
}
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.ext.cronet;
import android.content.Context;
import org.chromium.net.CronetEngine;
/**
* A factory class which creates or reuses a {@link CronetEngine}.
*/
public final class CronetEngineFactory {
private final Context context;
private CronetEngine cronetEngine = null;
/**
* Creates the factory for a {@link CronetEngine}.
* @param context The application context.
*/
public CronetEngineFactory(Context context) {
this.context = context;
}
/* package */ CronetEngine createCronetEngine() {
if (cronetEngine == null) {
cronetEngine = new CronetEngine.Builder(context).build();
}
return cronetEngine;
}
}