mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Use HttpEngineDataSource when supported in demo app.
HttpEngineDataSource is the recommended HttpDataSource when it is available. It uses the [HttpEngine](https://developer.android.com/reference/android/net/http/HttpEngine). #cherrypick PiperOrigin-RevId: 646051562
This commit is contained in:
parent
476ec607f2
commit
e591c37b1e
@ -42,6 +42,9 @@
|
|||||||
* Leanback extension:
|
* Leanback extension:
|
||||||
* Cast Extension:
|
* Cast Extension:
|
||||||
* Test Utilities:
|
* Test Utilities:
|
||||||
|
* Demo app:
|
||||||
|
* Use `HttpEngineDataSource` as the `HttpDataSource` when supported by the
|
||||||
|
device.
|
||||||
* Remove deprecated symbols:
|
* Remove deprecated symbols:
|
||||||
|
|
||||||
## 1.4
|
## 1.4
|
||||||
|
@ -16,12 +16,15 @@
|
|||||||
package androidx.media3.demo.main;
|
package androidx.media3.demo.main;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.net.http.HttpEngine;
|
||||||
|
import android.os.Build;
|
||||||
import androidx.annotation.OptIn;
|
import androidx.annotation.OptIn;
|
||||||
import androidx.media3.database.DatabaseProvider;
|
import androidx.media3.database.DatabaseProvider;
|
||||||
import androidx.media3.database.StandaloneDatabaseProvider;
|
import androidx.media3.database.StandaloneDatabaseProvider;
|
||||||
import androidx.media3.datasource.DataSource;
|
import androidx.media3.datasource.DataSource;
|
||||||
import androidx.media3.datasource.DefaultDataSource;
|
import androidx.media3.datasource.DefaultDataSource;
|
||||||
import androidx.media3.datasource.DefaultHttpDataSource;
|
import androidx.media3.datasource.DefaultHttpDataSource;
|
||||||
|
import androidx.media3.datasource.HttpEngineDataSource;
|
||||||
import androidx.media3.datasource.cache.Cache;
|
import androidx.media3.datasource.cache.Cache;
|
||||||
import androidx.media3.datasource.cache.CacheDataSource;
|
import androidx.media3.datasource.cache.CacheDataSource;
|
||||||
import androidx.media3.datasource.cache.NoOpCacheEvictor;
|
import androidx.media3.datasource.cache.NoOpCacheEvictor;
|
||||||
@ -47,13 +50,14 @@ public final class DemoUtil {
|
|||||||
public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel";
|
public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the demo application uses Cronet for networking. Note that Cronet does not provide
|
* Whether the demo application uses Cronet for networking when {@link HttpEngine} is not
|
||||||
* automatic support for cookies (https://github.com/google/ExoPlayer/issues/5975).
|
* supported. Note that Cronet does not provide automatic support for cookies
|
||||||
|
* (https://github.com/google/ExoPlayer/issues/5975).
|
||||||
*
|
*
|
||||||
* <p>If set to false, the platform's default network stack is used with a {@link CookieManager}
|
* <p>If set to false, the {@link DefaultHttpDataSource} is used with a {@link CookieManager}
|
||||||
* configured in {@link #getHttpDataSourceFactory}.
|
* configured in {@link #getHttpDataSourceFactory} when {@link HttpEngine} is not supported.
|
||||||
*/
|
*/
|
||||||
private static final boolean USE_CRONET_FOR_NETWORKING = true;
|
private static final boolean ALLOW_CRONET_FOR_NETWORKING = true;
|
||||||
|
|
||||||
private static final String TAG = "DemoUtil";
|
private static final String TAG = "DemoUtil";
|
||||||
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
|
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
|
||||||
@ -97,26 +101,38 @@ public final class DemoUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized DataSource.Factory getHttpDataSourceFactory(Context context) {
|
public static synchronized DataSource.Factory getHttpDataSourceFactory(Context context) {
|
||||||
if (httpDataSourceFactory == null) {
|
if (httpDataSourceFactory != null) {
|
||||||
if (USE_CRONET_FOR_NETWORKING) {
|
return httpDataSourceFactory;
|
||||||
context = context.getApplicationContext();
|
}
|
||||||
@Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context);
|
context = context.getApplicationContext();
|
||||||
if (cronetEngine != null) {
|
if (Build.VERSION.SDK_INT >= 34) {
|
||||||
httpDataSourceFactory =
|
setCookieHandler();
|
||||||
new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor());
|
HttpEngine httpEngine = new HttpEngine.Builder(context).build();
|
||||||
}
|
httpDataSourceFactory =
|
||||||
}
|
new HttpEngineDataSource.Factory(httpEngine, Executors.newSingleThreadExecutor());
|
||||||
if (httpDataSourceFactory == null) {
|
return httpDataSourceFactory;
|
||||||
// We don't want to use Cronet, or we failed to instantiate a CronetEngine.
|
}
|
||||||
CookieManager cookieManager = new CookieManager();
|
if (ALLOW_CRONET_FOR_NETWORKING) {
|
||||||
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
@Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context);
|
||||||
CookieHandler.setDefault(cookieManager);
|
if (cronetEngine != null) {
|
||||||
httpDataSourceFactory = new DefaultHttpDataSource.Factory();
|
httpDataSourceFactory =
|
||||||
|
new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor());
|
||||||
|
return httpDataSourceFactory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// The device doesn't support HttpEngine or we don't want to allow Cronet, or we failed to
|
||||||
|
// instantiate a CronetEngine.
|
||||||
|
setCookieHandler();
|
||||||
|
httpDataSourceFactory = new DefaultHttpDataSource.Factory();
|
||||||
return httpDataSourceFactory;
|
return httpDataSourceFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void setCookieHandler() {
|
||||||
|
CookieManager cookieManager = new CookieManager();
|
||||||
|
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
||||||
|
CookieHandler.setDefault(cookieManager);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns a {@link DataSource.Factory}. */
|
/** Returns a {@link DataSource.Factory}. */
|
||||||
public static synchronized DataSource.Factory getDataSourceFactory(Context context) {
|
public static synchronized DataSource.Factory getDataSourceFactory(Context context) {
|
||||||
if (dataSourceFactory == null) {
|
if (dataSourceFactory == null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user