package com.brentvatne.exoplayer; import android.net.Uri; import androidx.annotation.NonNull; import androidx.media3.common.util.Util; import androidx.media3.datasource.AssetDataSource; import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DataSpec; import androidx.media3.datasource.DefaultDataSource; import androidx.media3.datasource.HttpDataSource; import androidx.media3.datasource.okhttp.OkHttpDataSource; import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter; import com.facebook.react.bridge.ReactContext; import com.facebook.react.modules.network.CookieJarContainer; import com.facebook.react.modules.network.ForwardingCookieHandler; import com.facebook.react.modules.network.OkHttpClientProvider; import java.util.Map; import okhttp3.Call; import okhttp3.JavaNetCookieJar; import okhttp3.OkHttpClient; public class DataSourceUtil { private DataSourceUtil() { } private static DataSource.Factory defaultDataSourceFactory = null; private static HttpDataSource.Factory defaultHttpDataSourceFactory = null; private static String userAgent = null; public static void setUserAgent(String userAgent) { DataSourceUtil.userAgent = userAgent; } public static String getUserAgent(ReactContext context) { if (userAgent == null) { userAgent = Util.getUserAgent(context, context.getPackageName()); } return userAgent; } public static DataSource.Factory getDefaultDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map requestHeaders) { if (defaultDataSourceFactory == null || (requestHeaders != null && !requestHeaders.isEmpty())) { defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter, requestHeaders); } return defaultDataSourceFactory; } public static HttpDataSource.Factory getDefaultHttpDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map requestHeaders) { if (defaultHttpDataSourceFactory == null || (requestHeaders != null && !requestHeaders.isEmpty())) { defaultHttpDataSourceFactory = buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders); } return defaultHttpDataSourceFactory; } private static DataSource.Factory buildDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map requestHeaders) { return new DefaultDataSource.Factory(context, buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders)); } private static HttpDataSource.Factory buildHttpDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map requestHeaders) { OkHttpClient client = OkHttpClientProvider.getOkHttpClient(); CookieJarContainer container = (CookieJarContainer) client.cookieJar(); ForwardingCookieHandler handler = new ForwardingCookieHandler(context); container.setCookieJar(new JavaNetCookieJar(handler)); OkHttpDataSource.Factory okHttpDataSourceFactory = new OkHttpDataSource.Factory((Call.Factory) client) .setTransferListener(bandwidthMeter); if (requestHeaders != null) { okHttpDataSourceFactory.setDefaultRequestProperties(requestHeaders); if (!requestHeaders.containsKey("User-Agent")) { okHttpDataSourceFactory.setUserAgent(getUserAgent(context)); } } else { okHttpDataSourceFactory.setUserAgent(getUserAgent(context)); } return okHttpDataSourceFactory; } public static DataSource.Factory buildAssetDataSourceFactory(ReactContext context, Uri srcUri) throws AssetDataSource.AssetDataSourceException { DataSpec dataSpec = new DataSpec(srcUri); final AssetDataSource rawResourceDataSource = new AssetDataSource(context); rawResourceDataSource.open(dataSpec); return new DataSource.Factory() { @NonNull @Override public DataSource createDataSource() { return rawResourceDataSource; } }; } }