Skip to content

Commit

Permalink
[expo-av][android] fix for exo video bandwidth
Browse files Browse the repository at this point in the history
pass bandwidth listener through to datasource factory
and set using addTransferListener when not null
  • Loading branch information
watchinharrison committed May 19, 2020
1 parent 6d56a66 commit 4fbf05a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/expo-av/CHANGELOG.md
Expand Up @@ -9,3 +9,5 @@
### 🐛 Bug fixes

- Fixed `Plaback.loadAsync()` return type. ([#7559](https://github.com/expo/expo/pull/7559) by [@awinograd](https://github.com/awinograd))

- Fixed the adaptive streaming for exoplayer for android as raised in https://github.com/expo/expo/issues/2177
Expand Up @@ -92,7 +92,7 @@ public void load(final Bundle status, final LoadCompletionListener loadCompletio
mSimpleExoPlayer.addVideoListener(this);

// Produces DataSource instances through which media data is loaded.
final DataSource.Factory dataSourceFactory = mAVModule.getModuleRegistry().getModule(DataSourceFactoryProvider.class).createFactory(mReactContext, mAVModule.getModuleRegistry(), Util.getUserAgent(mAVModule.getContext(), "yourApplicationName"), mRequestHeaders);
final DataSource.Factory dataSourceFactory = mAVModule.getModuleRegistry().getModule(DataSourceFactoryProvider.class).createFactory(mReactContext, mAVModule.getModuleRegistry(), Util.getUserAgent(mAVModule.getContext(), "yourApplicationName"), mRequestHeaders, bandwidthMeter.getTransferListener());
try {
// This is the MediaSource representing the media to be played.
final MediaSource source = buildMediaSource(mUri, mOverridingExtension, mainHandler, dataSourceFactory);
Expand Down
Expand Up @@ -24,13 +24,16 @@ public class CustomHeadersOkHttpDataSourceFactory extends HttpDataSource.BaseFac
private final TransferListener mListener;
@Nullable
private final CacheControl mCacheControl;
@Nullable
private final TransferListener mBandwidthMeter;

public CustomHeadersOkHttpDataSourceFactory(@NonNull Call.Factory callFactory, @Nullable String userAgent, @Nullable Map<String, Object> requestHeaders) {
public CustomHeadersOkHttpDataSourceFactory(@NonNull Call.Factory callFactory, @Nullable String userAgent, @Nullable Map<String, Object> requestHeaders, @Nullable TransferListener bandwidthMeter) {
super();
mCallFactory = callFactory;
mUserAgent = userAgent;
mListener = null;
mCacheControl = null;
mBandwidthMeter = bandwidthMeter;
updateRequestProperties(getDefaultRequestProperties(), requestHeaders);
}

Expand All @@ -45,6 +48,10 @@ protected void updateRequestProperties(HttpDataSource.RequestProperties requestP
}

protected OkHttpDataSource createDataSourceInternal(HttpDataSource.RequestProperties defaultRequestProperties) {
return new OkHttpDataSource(mCallFactory, mUserAgent, null, mCacheControl, defaultRequestProperties);
OkHttpDataSource okHttpDataSource = new OkHttpDataSource(mCallFactory, mUserAgent, null, mCacheControl, defaultRequestProperties);
if (mBandwidthMeter != null) {
okHttpDataSource.addTransferListener(mBandwidthMeter);
}
return okHttpDataSource;
}
}
Expand Up @@ -3,11 +3,12 @@
import android.content.Context;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.TransferListener;

import java.util.Map;

import org.unimodules.core.ModuleRegistry;

public interface DataSourceFactoryProvider {
DataSource.Factory createFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders);
DataSource.Factory createFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders, TransferListener bandwidthMeter);
}
Expand Up @@ -4,6 +4,7 @@

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.TransferListener;

import java.net.CookieHandler;
import java.util.Map;
Expand All @@ -15,14 +16,14 @@
public class SharedCookiesDataSourceFactory implements DataSource.Factory {
private final DataSource.Factory mDataSourceFactory;

public SharedCookiesDataSourceFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders) {
public SharedCookiesDataSourceFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders, TransferListener bandwidthMeter) {
CookieHandler cookieHandler = moduleRegistry.getModule(CookieHandler.class);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (cookieHandler != null) {
builder.cookieJar(new JavaNetCookieJar(cookieHandler));
}
OkHttpClient client = builder.build();
mDataSourceFactory = new DefaultDataSourceFactory(reactApplicationContext, null, new CustomHeadersOkHttpDataSourceFactory(client, userAgent, requestHeaders));
mDataSourceFactory = new DefaultDataSourceFactory(reactApplicationContext, null, new CustomHeadersOkHttpDataSourceFactory(client, userAgent, requestHeaders, bandwidthMeter));
}

@Override
Expand Down
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.TransferListener;

import java.util.Collections;
import java.util.List;
Expand All @@ -18,7 +19,7 @@ public List<Class> getExportedInterfaces() {
}

@Override
public DataSource.Factory createFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders) {
return new SharedCookiesDataSourceFactory(reactApplicationContext, moduleRegistry, userAgent, requestHeaders);
public DataSource.Factory createFactory(Context reactApplicationContext, ModuleRegistry moduleRegistry, String userAgent, Map<String, Object> requestHeaders, TransferListener bandwidthMeter) {
return new SharedCookiesDataSourceFactory(reactApplicationContext, moduleRegistry, userAgent, requestHeaders, bandwidthMeter);
}
}

0 comments on commit 4fbf05a

Please sign in to comment.