2017-01-11 12:51:45 +00:00
|
|
|
package com.brentvatne.exoplayer;
|
|
|
|
|
2018-06-09 21:30:38 +02:00
|
|
|
import com.facebook.react.bridge.ReactContext;
|
|
|
|
import com.facebook.react.modules.network.CookieJarContainer;
|
|
|
|
import com.facebook.react.modules.network.ForwardingCookieHandler;
|
2017-01-11 12:51:45 +00:00
|
|
|
import com.facebook.react.modules.network.OkHttpClientProvider;
|
|
|
|
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory;
|
|
|
|
import com.google.android.exoplayer2.upstream.DataSource;
|
|
|
|
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
|
|
|
|
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
|
|
|
import com.google.android.exoplayer2.upstream.HttpDataSource;
|
|
|
|
import com.google.android.exoplayer2.util.Util;
|
|
|
|
|
2018-06-09 21:30:38 +02:00
|
|
|
import okhttp3.JavaNetCookieJar;
|
|
|
|
import okhttp3.OkHttpClient;
|
2017-10-02 20:11:41 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
2017-01-11 12:51:45 +00:00
|
|
|
public class DataSourceUtil {
|
|
|
|
|
|
|
|
private DataSourceUtil() {
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DataSource.Factory rawDataSourceFactory = null;
|
|
|
|
private static DataSource.Factory defaultDataSourceFactory = null;
|
|
|
|
private static String userAgent = null;
|
|
|
|
|
|
|
|
public static void setUserAgent(String userAgent) {
|
|
|
|
DataSourceUtil.userAgent = userAgent;
|
|
|
|
}
|
|
|
|
|
2018-01-29 13:25:58 -07:00
|
|
|
public static String getUserAgent(ReactContext context) {
|
2017-01-11 12:51:45 +00:00
|
|
|
if (userAgent == null) {
|
2018-01-29 13:25:58 -07:00
|
|
|
userAgent = Util.getUserAgent(context, "ReactNativeVideo");
|
2017-01-11 12:51:45 +00:00
|
|
|
}
|
|
|
|
return userAgent;
|
|
|
|
}
|
|
|
|
|
2018-01-29 13:25:58 -07:00
|
|
|
public static DataSource.Factory getRawDataSourceFactory(ReactContext context) {
|
2017-01-11 12:51:45 +00:00
|
|
|
if (rawDataSourceFactory == null) {
|
|
|
|
rawDataSourceFactory = buildRawDataSourceFactory(context);
|
|
|
|
}
|
|
|
|
return rawDataSourceFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setRawDataSourceFactory(DataSource.Factory factory) {
|
|
|
|
DataSourceUtil.rawDataSourceFactory = factory;
|
|
|
|
}
|
|
|
|
|
2018-06-09 21:36:09 +02:00
|
|
|
|
2018-06-09 21:30:38 +02:00
|
|
|
public static DataSource.Factory getDefaultDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map<String, String> requestHeaders) {
|
2018-01-15 17:10:54 +01:00
|
|
|
if (defaultDataSourceFactory == null || (requestHeaders != null && !requestHeaders.isEmpty())) {
|
2017-10-02 20:11:41 +02:00
|
|
|
defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter, requestHeaders);
|
2017-01-11 12:51:45 +00:00
|
|
|
}
|
|
|
|
return defaultDataSourceFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDefaultDataSourceFactory(DataSource.Factory factory) {
|
|
|
|
DataSourceUtil.defaultDataSourceFactory = factory;
|
|
|
|
}
|
|
|
|
|
2018-01-29 13:25:58 -07:00
|
|
|
private static DataSource.Factory buildRawDataSourceFactory(ReactContext context) {
|
2017-01-11 12:51:45 +00:00
|
|
|
return new RawResourceDataSourceFactory(context.getApplicationContext());
|
|
|
|
}
|
|
|
|
|
2018-06-09 21:30:38 +02:00
|
|
|
private static DataSource.Factory buildDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map<String, String> requestHeaders) {
|
|
|
|
return new DefaultDataSourceFactory(context, bandwidthMeter,
|
|
|
|
buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders));
|
2017-01-11 12:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:30:38 +02:00
|
|
|
private static HttpDataSource.Factory buildHttpDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter, Map<String, String> requestHeaders) {
|
|
|
|
OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
|
|
|
|
CookieJarContainer container = (CookieJarContainer) client.cookieJar();
|
|
|
|
ForwardingCookieHandler handler = new ForwardingCookieHandler(context);
|
|
|
|
container.setCookieJar(new JavaNetCookieJar(handler));
|
|
|
|
OkHttpDataSourceFactory okHttpDataSourceFactory = new OkHttpDataSourceFactory(client, getUserAgent(context), bandwidthMeter);
|
2017-01-11 12:51:45 +00:00
|
|
|
|
2017-10-02 20:11:41 +02:00
|
|
|
if (requestHeaders != null)
|
|
|
|
okHttpDataSourceFactory.getDefaultRequestProperties().set(requestHeaders);
|
|
|
|
|
|
|
|
return okHttpDataSourceFactory;
|
|
|
|
}
|
2017-01-11 12:51:45 +00:00
|
|
|
}
|