Search in sources :

Example 1 with CronetEngine

use of org.chromium.net.CronetEngine in project ExoPlayer by google.

the class DemoUtil method getHttpDataSourceFactory.

public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) {
    if (httpDataSourceFactory == null) {
        if (USE_CRONET_FOR_NETWORKING) {
            context = context.getApplicationContext();
            @Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context);
            if (cronetEngine != null) {
                httpDataSourceFactory = new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor());
            }
        }
        if (httpDataSourceFactory == null) {
            // We don't want to use Cronet, or we failed to instantiate a CronetEngine.
            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
            CookieHandler.setDefault(cookieManager);
            httpDataSourceFactory = new DefaultHttpDataSource.Factory();
        }
    }
    return httpDataSourceFactory;
}
Also used : CronetEngine(org.chromium.net.CronetEngine) CronetDataSource(com.google.android.exoplayer2.ext.cronet.CronetDataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CookieManager(java.net.CookieManager)

Example 2 with CronetEngine

use of org.chromium.net.CronetEngine in project ExoPlayer by google.

the class CronetDataSourceFactory method createDataSourceInternal.

@Override
protected HttpDataSource createDataSourceInternal(HttpDataSource.RequestProperties defaultRequestProperties) {
    @Nullable CronetEngine cronetEngine = cronetEngineWrapper.getCronetEngine();
    if (cronetEngine == null) {
        return fallbackFactory.createDataSource();
    }
    CronetDataSource dataSource = new CronetDataSource(cronetEngine, executor, REQUEST_PRIORITY_MEDIUM, connectTimeoutMs, readTimeoutMs, resetTimeoutOnRedirects, /* handleSetCookieRequests= */
    false, /* userAgent= */
    null, defaultRequestProperties, /* contentTypePredicate= */
    null, /* keepPostFor302Redirects */
    false);
    if (transferListener != null) {
        dataSource.addTransferListener(transferListener);
    }
    return dataSource;
}
Also used : CronetEngine(org.chromium.net.CronetEngine) Nullable(androidx.annotation.Nullable)

Example 3 with CronetEngine

use of org.chromium.net.CronetEngine in project ExoPlayer by google.

the class CronetDataSourceContractTest method createDataSource.

@Override
protected DataSource createDataSource() {
    @Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(ApplicationProvider.getApplicationContext(), /* userAgent= */
    "test-agent", /* preferGMSCoreCronet= */
    false);
    assertThat(cronetEngine).isNotNull();
    return new CronetDataSource.Factory(cronetEngine, executorService).createDataSource();
}
Also used : CronetEngine(org.chromium.net.CronetEngine) Nullable(androidx.annotation.Nullable)

Example 4 with CronetEngine

use of org.chromium.net.CronetEngine in project ExoPlayer by google.

the class CronetUtil method buildCronetEngine.

/**
 * Builds a {@link CronetEngine} suitable for use with {@link CronetDataSource}. When choosing a
 * {@link CronetProvider Cronet provider} to build the {@link CronetEngine}, disabled providers
 * are not considered. Neither are fallback providers, since it's more efficient to use {@link
 * DefaultHttpDataSource} than it is to use {@link CronetDataSource} with a fallback {@link
 * CronetEngine}.
 *
 * <p>Note that it's recommended for applications to create only one instance of {@link
 * CronetEngine}, so if your application already has an instance for performing other networking,
 * then that instance should be used and calling this method is unnecessary. See the <a
 * href="https://developer.android.com/guide/topics/connectivity/cronet/start">Android developer
 * guide</a> to learn more about using Cronet for network operations.
 *
 * @param context A context.
 * @param userAgent A default user agent, or {@code null} to use a default user agent of the
 *     {@link CronetEngine}.
 * @param preferGooglePlayServices Whether Cronet from Google Play Services should be preferred
 *     over Cronet Embedded, if both are available.
 * @return The {@link CronetEngine}, or {@code null} if no suitable engine could be built.
 */
@Nullable
public static CronetEngine buildCronetEngine(Context context, @Nullable String userAgent, boolean preferGooglePlayServices) {
    List<CronetProvider> cronetProviders = new ArrayList<>(CronetProvider.getAllProviders(context));
    // Remove disabled and fallback Cronet providers from list.
    for (int i = cronetProviders.size() - 1; i >= 0; i--) {
        if (!cronetProviders.get(i).isEnabled() || CronetProvider.PROVIDER_NAME_FALLBACK.equals(cronetProviders.get(i).getName())) {
            cronetProviders.remove(i);
        }
    }
    // Sort remaining providers by type and version.
    CronetProviderComparator providerComparator = new CronetProviderComparator(preferGooglePlayServices);
    Collections.sort(cronetProviders, providerComparator);
    for (int i = 0; i < cronetProviders.size(); i++) {
        String providerName = cronetProviders.get(i).getName();
        try {
            CronetEngine.Builder cronetEngineBuilder = cronetProviders.get(i).createBuilder();
            if (userAgent != null) {
                cronetEngineBuilder.setUserAgent(userAgent);
            }
            CronetEngine cronetEngine = cronetEngineBuilder.build();
            Log.d(TAG, "CronetEngine built using " + providerName);
            return cronetEngine;
        } catch (SecurityException e) {
            Log.w(TAG, "Failed to build CronetEngine. Please check that the process has " + "android.permission.ACCESS_NETWORK_STATE.");
        } catch (UnsatisfiedLinkError e) {
            Log.w(TAG, "Failed to link Cronet binaries. Please check that native Cronet binaries are" + "bundled into your app.");
        }
    }
    Log.w(TAG, "CronetEngine could not be built.");
    return null;
}
Also used : CronetEngine(org.chromium.net.CronetEngine) CronetProvider(org.chromium.net.CronetProvider) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Aggregations

CronetEngine (org.chromium.net.CronetEngine)4 Nullable (androidx.annotation.Nullable)3 CronetDataSource (com.google.android.exoplayer2.ext.cronet.CronetDataSource)1 DefaultHttpDataSource (com.google.android.exoplayer2.upstream.DefaultHttpDataSource)1 CookieManager (java.net.CookieManager)1 ArrayList (java.util.ArrayList)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 CronetProvider (org.chromium.net.CronetProvider)1