Merge pull request #922 from brandonmoon/link-exoplayer-with-cookies
Link exoplayer with cookies
This commit is contained in:
commit
d716ee48d9
@ -1,7 +1,11 @@
|
|||||||
package com.brentvatne.exoplayer;
|
package com.brentvatne.exoplayer;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.ContextWrapper;
|
||||||
|
|
||||||
|
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 com.facebook.react.modules.network.OkHttpClientProvider;
|
||||||
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory;
|
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory;
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
@ -10,6 +14,10 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
|||||||
import com.google.android.exoplayer2.upstream.HttpDataSource;
|
import com.google.android.exoplayer2.upstream.HttpDataSource;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
|
||||||
|
import okhttp3.Cookie;
|
||||||
|
import okhttp3.JavaNetCookieJar;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
public class DataSourceUtil {
|
public class DataSourceUtil {
|
||||||
|
|
||||||
private DataSourceUtil() {
|
private DataSourceUtil() {
|
||||||
@ -23,14 +31,14 @@ public class DataSourceUtil {
|
|||||||
DataSourceUtil.userAgent = userAgent;
|
DataSourceUtil.userAgent = userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUserAgent(Context context) {
|
public static String getUserAgent(ReactContext context) {
|
||||||
if (userAgent == null) {
|
if (userAgent == null) {
|
||||||
userAgent = Util.getUserAgent(context.getApplicationContext(), "ReactNativeVideo");
|
userAgent = Util.getUserAgent(context, "ReactNativeVideo");
|
||||||
}
|
}
|
||||||
return userAgent;
|
return userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataSource.Factory getRawDataSourceFactory(Context context) {
|
public static DataSource.Factory getRawDataSourceFactory(ReactContext context) {
|
||||||
if (rawDataSourceFactory == null) {
|
if (rawDataSourceFactory == null) {
|
||||||
rawDataSourceFactory = buildRawDataSourceFactory(context);
|
rawDataSourceFactory = buildRawDataSourceFactory(context);
|
||||||
}
|
}
|
||||||
@ -41,7 +49,7 @@ public class DataSourceUtil {
|
|||||||
DataSourceUtil.rawDataSourceFactory = factory;
|
DataSourceUtil.rawDataSourceFactory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataSource.Factory getDefaultDataSourceFactory(Context context, DefaultBandwidthMeter bandwidthMeter) {
|
public static DataSource.Factory getDefaultDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter) {
|
||||||
if (defaultDataSourceFactory == null) {
|
if (defaultDataSourceFactory == null) {
|
||||||
defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter);
|
defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter);
|
||||||
}
|
}
|
||||||
@ -52,18 +60,21 @@ public class DataSourceUtil {
|
|||||||
DataSourceUtil.defaultDataSourceFactory = factory;
|
DataSourceUtil.defaultDataSourceFactory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DataSource.Factory buildRawDataSourceFactory(Context context) {
|
private static DataSource.Factory buildRawDataSourceFactory(ReactContext context) {
|
||||||
return new RawResourceDataSourceFactory(context.getApplicationContext());
|
return new RawResourceDataSourceFactory(context.getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DataSource.Factory buildDataSourceFactory(Context context, DefaultBandwidthMeter bandwidthMeter) {
|
private static DataSource.Factory buildDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter) {
|
||||||
Context appContext = context.getApplicationContext();
|
return new DefaultDataSourceFactory(context, bandwidthMeter,
|
||||||
return new DefaultDataSourceFactory(appContext, bandwidthMeter,
|
buildHttpDataSourceFactory(context, bandwidthMeter));
|
||||||
buildHttpDataSourceFactory(appContext, bandwidthMeter));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpDataSource.Factory buildHttpDataSourceFactory(Context context, DefaultBandwidthMeter bandwidthMeter) {
|
private static HttpDataSource.Factory buildHttpDataSourceFactory(ReactContext context, DefaultBandwidthMeter bandwidthMeter) {
|
||||||
return new OkHttpDataSourceFactory(OkHttpClientProvider.getOkHttpClient(), getUserAgent(context), bandwidthMeter);
|
OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
|
||||||
|
CookieJarContainer container = (CookieJarContainer) client.cookieJar();
|
||||||
|
ForwardingCookieHandler handler = new ForwardingCookieHandler(context);
|
||||||
|
container.setCookieJar(new JavaNetCookieJar(handler));
|
||||||
|
return new OkHttpDataSourceFactory(client, getUserAgent(context), bandwidthMeter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -131,9 +131,9 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
|
|
||||||
public ReactExoplayerView(ThemedReactContext context) {
|
public ReactExoplayerView(ThemedReactContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
this.themedReactContext = context;
|
||||||
createViews();
|
createViews();
|
||||||
this.eventEmitter = new VideoEventEmitter(context);
|
this.eventEmitter = new VideoEventEmitter(context);
|
||||||
this.themedReactContext = context;
|
|
||||||
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||||
themedReactContext.addLifecycleEventListener(this);
|
themedReactContext.addLifecycleEventListener(this);
|
||||||
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
||||||
@ -363,7 +363,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
* @return A new DataSource factory.
|
* @return A new DataSource factory.
|
||||||
*/
|
*/
|
||||||
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
|
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
|
||||||
return DataSourceUtil.getDefaultDataSourceFactory(getContext(), useBandwidthMeter ? BANDWIDTH_METER : null);
|
return DataSourceUtil.getDefaultDataSourceFactory(this.themedReactContext, useBandwidthMeter ? BANDWIDTH_METER : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// AudioManager.OnAudioFocusChangeListener implementation
|
// AudioManager.OnAudioFocusChangeListener implementation
|
||||||
@ -535,8 +535,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
decoderInitializationException.decoderName);
|
decoderInitializationException.decoderName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (e.type == ExoPlaybackException.TYPE_SOURCE) {
|
||||||
else if (e.type == ExoPlaybackException.TYPE_SOURCE) {
|
|
||||||
ex = e.getSourceException();
|
ex = e.getSourceException();
|
||||||
errorString = getResources().getString(R.string.unrecognized_media_format);
|
errorString = getResources().getString(R.string.unrecognized_media_format);
|
||||||
}
|
}
|
||||||
@ -580,7 +579,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
|
|
||||||
this.srcUri = uri;
|
this.srcUri = uri;
|
||||||
this.extension = extension;
|
this.extension = extension;
|
||||||
this.mediaDataSourceFactory = DataSourceUtil.getDefaultDataSourceFactory(getContext(), BANDWIDTH_METER);
|
this.mediaDataSourceFactory = DataSourceUtil.getDefaultDataSourceFactory(this.themedReactContext, BANDWIDTH_METER);
|
||||||
|
|
||||||
if (!isOriginalSourceNull && !isSourceEqual) {
|
if (!isOriginalSourceNull && !isSourceEqual) {
|
||||||
reloadSource();
|
reloadSource();
|
||||||
@ -599,7 +598,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
|
|
||||||
this.srcUri = uri;
|
this.srcUri = uri;
|
||||||
this.extension = extension;
|
this.extension = extension;
|
||||||
this.mediaDataSourceFactory = DataSourceUtil.getRawDataSourceFactory(getContext());
|
this.mediaDataSourceFactory = DataSourceUtil.getRawDataSourceFactory(this.themedReactContext);
|
||||||
|
|
||||||
if (!isOriginalSourceNull && !isSourceEqual) {
|
if (!isOriginalSourceNull && !isSourceEqual) {
|
||||||
reloadSource();
|
reloadSource();
|
||||||
|
Loading…
Reference in New Issue
Block a user