Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): cache #3514

Merged
merged 25 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57a1654
feat: android cache
lovegaoshi Feb 1, 2024
09637b1
docs: bufferSize
lovegaoshi Feb 1, 2024
1734794
Revert "docs: bufferSize"
lovegaoshi Feb 5, 2024
47ddca6
fix: cacheSize name
lovegaoshi Feb 5, 2024
e61fe19
feat: singleton android cache
lovegaoshi Feb 19, 2024
f7cc2ad
fix: local cache resolve
lovegaoshi Mar 1, 2024
450f618
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Mar 1, 2024
88b4bff
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Mar 1, 2024
55c8ab1
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Mar 8, 2024
e36de4e
fix: lint
lovegaoshi Mar 8, 2024
b4a4346
docs: android cache
lovegaoshi Mar 8, 2024
3eae85d
chore: merge conflict
lovegaoshi Mar 8, 2024
426d08c
fix: lint
lovegaoshi Mar 11, 2024
7830a2e
Merge branch 'master' into dev-android-cache
lovegaoshi Mar 11, 2024
b2de1b7
Merge branch 'master' into dev-android-cache
lovegaoshi Mar 13, 2024
59e39f7
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Apr 1, 2024
f41ebd6
chore: useCache button
lovegaoshi Apr 1, 2024
eb0262e
Merge branch 'master' into dev-android-cache
lovegaoshi Apr 8, 2024
67c78f9
chore: fix state in the sample
freeboub Apr 16, 2024
51aac8c
fix: cache factory
lovegaoshi Apr 18, 2024
2ee85f1
Merge pull request #1 from freeboub/dev-android-cache-fix
lovegaoshi Apr 18, 2024
aae5647
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Apr 18, 2024
230106d
Merge branch 'dev-android-cache' of https://github.com/lovegaoshi/rea…
lovegaoshi Apr 18, 2024
f1f27ce
Merge branch 'master' of https://github.com/lovegaoshi/react-native-v…
lovegaoshi Apr 27, 2024
629349e
chore: update cacheSizeMB docs
lovegaoshi Apr 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
import androidx.media3.common.TrackSelectionOverride;
import androidx.media3.common.Tracks;
import androidx.media3.common.util.Util;
import androidx.media3.database.StandaloneDatabaseProvider;
import androidx.media3.datasource.cache.CacheDataSource;
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor;
import androidx.media3.datasource.cache.SimpleCache;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.HttpDataSource;
Expand Down Expand Up @@ -111,6 +115,7 @@
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
import com.google.common.collect.ImmutableList;

import java.io.File;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
Expand Down Expand Up @@ -185,6 +190,7 @@ public class ReactExoplayerView extends FrameLayout implements
private boolean isUsingContentResolution = false;
private boolean selectTrackWhenReady = false;

private DataSource.Factory cacheDataSourceFactory;
private int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS;
private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
private int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
Expand Down Expand Up @@ -634,8 +640,11 @@ private void initializePlayerCore(ReactExoplayerView self) {
.setAdEventListener(this)
.setAdErrorListener(this)
.build();

DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory);
if (cacheDataSourceFactory != null) {
mediaSourceFactory.setDataSourceFactory(cacheDataSourceFactory);
}

if (adsLoader != null) {
mediaSourceFactory.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
}
Expand Down Expand Up @@ -830,9 +839,16 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi
);
break;
case CONTENT_TYPE_OTHER:
mediaSourceFactory = new ProgressiveMediaSource.Factory(
freeboub marked this conversation as resolved.
Show resolved Hide resolved
mediaDataSourceFactory
);
if (cacheDataSourceFactory == null) {
mediaSourceFactory = new ProgressiveMediaSource.Factory(
mediaDataSourceFactory
);
} else {
mediaSourceFactory = new ProgressiveMediaSource.Factory(
cacheDataSourceFactory
);

}
break;
default: {
throw new IllegalStateException("Unsupported type: " + type);
Expand Down Expand Up @@ -2025,14 +2041,23 @@ public void setHideShutterView(boolean hideShutterView) {
exoPlayerView.setHideShutterView(hideShutterView);
}

public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent, double newMinBackBufferMemoryReservePercent, double newMinBufferMemoryReservePercent) {
public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent, double newMinBackBufferMemoryReservePercent, double newMinBufferMemoryReservePercent, int cacheSize) {
minBufferMs = newMinBufferMs;
maxBufferMs = newMaxBufferMs;
bufferForPlaybackMs = newBufferForPlaybackMs;
bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs;
maxHeapAllocationPercent = newMaxHeapAllocationPercent;
minBackBufferMemoryReservePercent = newMinBackBufferMemoryReservePercent;
minBufferMemoryReservePercent = newMinBufferMemoryReservePercent;
if (cacheSize == 0) {
cacheDataSourceFactory = null;
} else {
SimpleCache simpleCache = new SimpleCache(new File(this.getContext().getCacheDir(), "RNVCache"), new LeastRecentlyUsedCacheEvictor((long) cacheSize*1024*1024), new StandaloneDatabaseProvider(this.getContext()));
freeboub marked this conversation as resolved.
Show resolved Hide resolved
cacheDataSourceFactory =
new CacheDataSource.Factory()
.setCache(simpleCache)
.setUpstreamDataSourceFactory(buildHttpDataSourceFactory(false));
}
releasePlayer();
initializePlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
private static final String PROP_VOLUME = "volume";
private static final String PROP_BACK_BUFFER_DURATION_MS = "backBufferDurationMs";
private static final String PROP_BUFFER_CONFIG = "bufferConfig";
private static final String PROP_BUFFER_CONFIG_CACHE_SIZE = "cacheSizeMB";
private static final String PROP_BUFFER_CONFIG_MIN_BUFFER_MS = "minBufferMs";
private static final String PROP_BUFFER_CONFIG_MAX_BUFFER_MS = "maxBufferMs";
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs";
Expand Down Expand Up @@ -400,6 +401,7 @@ public void setShutterColor(final ReactExoplayerView videoView, final Integer co

@ReactProp(name = PROP_BUFFER_CONFIG)
public void setBufferConfig(final ReactExoplayerView videoView, @Nullable ReadableMap bufferConfig) {
int cacheSize = 0;
int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS;
int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
Expand All @@ -409,14 +411,15 @@ public void setBufferConfig(final ReactExoplayerView videoView, @Nullable Readab
double minBufferMemoryReservePercent = ReactExoplayerView.DEFAULT_MIN_BUFFER_MEMORY_RESERVE;

if (bufferConfig != null) {
cacheSize = ReactBridgeUtils.safeGetInt(bufferConfig, PROP_BUFFER_CONFIG_CACHE_SIZE, 0);
minBufferMs = ReactBridgeUtils.safeGetInt(bufferConfig, PROP_BUFFER_CONFIG_MIN_BUFFER_MS, minBufferMs);
maxBufferMs = ReactBridgeUtils.safeGetInt(bufferConfig, PROP_BUFFER_CONFIG_MAX_BUFFER_MS, maxBufferMs);
bufferForPlaybackMs = ReactBridgeUtils.safeGetInt(bufferConfig, PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS, bufferForPlaybackMs);
bufferForPlaybackAfterRebufferMs = ReactBridgeUtils.safeGetInt(bufferConfig, PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS, bufferForPlaybackAfterRebufferMs);
maxHeapAllocationPercent = ReactBridgeUtils.safeGetDouble(bufferConfig, PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT, maxHeapAllocationPercent);
minBackBufferMemoryReservePercent = ReactBridgeUtils.safeGetDouble(bufferConfig, PROP_BUFFER_CONFIG_MIN_BACK_BUFFER_MEMORY_RESERVE_PERCENT, minBackBufferMemoryReservePercent);
minBufferMemoryReservePercent = ReactBridgeUtils.safeGetDouble(bufferConfig, PROP_BUFFER_CONFIG_MIN_BUFFER_MEMORY_RESERVE_PERCENT, minBufferMemoryReservePercent);
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, maxHeapAllocationPercent, minBackBufferMemoryReservePercent, minBufferMemoryReservePercent);
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, maxHeapAllocationPercent, minBackBufferMemoryReservePercent, minBufferMemoryReservePercent, cacheSize);
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/pages/component/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ bufferForPlaybackAfterRebufferMs | number | The default duration of media that m
maxHeapAllocationPercent | number | The percentage of available heap that the video can use to buffer, between 0 and 1
minBackBufferMemoryReservePercent | number | The percentage of available app memory at which during startup the back buffer will be disabled, between 0 and 1
minBufferMemoryReservePercent | number | The percentage of available app memory to keep in reserve that prevents buffer from using it, between 0 and 1
cacheSizeMB | number | Cache size in MB, it will allow applications to store video data for a while in the cache folder, it is useful to decrease bandwidth usage when repeating small videos. Android only.

This prop should only be set when you are setting the source, changing it after the media is loaded will cause it to be reloaded.

Expand Down
17 changes: 12 additions & 5 deletions examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class VideoPlayer extends Component {
seekerWidth = 0;

srcAllPlatformList = [
{
description: 'another bunny (can be saved)',
uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4',
},
require('./broadchurch.mp4'),
{
description: '(hls|live) red bull tv',
Expand All @@ -114,10 +118,6 @@ class VideoPlayer extends Component {
description: 'Another live sample',
uri: 'https://live.forstreet.cl/live/livestream.m3u8',
},
{
description: 'another bunny (can be saved)',
uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4',
},
];

srcIosList = [];
Expand Down Expand Up @@ -303,7 +303,7 @@ class VideoPlayer extends Component {
};

onEnd = () => {
this.channelUp();
!this.state.loop && this.channelUp();
};

toggleFullscreen() {
Expand Down Expand Up @@ -763,6 +763,13 @@ class VideoPlayer extends Component {
selectedTextTrack={this.state.selectedTextTrack}
selectedAudioTrack={this.state.selectedAudioTrack}
playInBackground={false}
bufferConfig={{
minBufferMs: 15000,
maxBufferMs: 50000,
bufferForPlaybackMs: 2500,
bufferForPlaybackAfterRebufferMs: 5000,
cacheSizeMB: 200,
}}
/>
</TouchableOpacity>
);
Expand Down
1 change: 1 addition & 0 deletions src/VideoNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type BufferConfig = Readonly<{
maxHeapAllocationPercent?: number;
minBackBufferMemoryReservePercent?: number;
minBufferMemoryReservePercent?: number;
cacheSizeMB?: number;
}>;

type SelectedVideoTrack = Readonly<{
Expand Down
1 change: 1 addition & 0 deletions src/types/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type BufferConfig = {
maxHeapAllocationPercent?: number;
minBackBufferMemoryReservePercent?: number;
minBufferMemoryReservePercent?: number;
cacheSizeMB?: number;
};

export enum SelectedTrackType {
Expand Down
Loading