forked from RalfNieuwenhuizen/react-native-video
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExoPlayerCache.java
More file actions
121 lines (100 loc) · 4.5 KB
/
ExoPlayerCache.java
File metadata and controls
121 lines (100 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.brentvatne.exoplayer;
import android.net.Uri;
import android.util.Log;
import android.util.Pair;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory;
import com.google.android.exoplayer2.upstream.cache.CacheUtil;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
public class ExoPlayerCache extends ReactContextBaseJavaModule {
private static SimpleCache instance = null;
private static final String CACHE_KEY_PREFIX = "exoPlayerCacheKeyPrefix";
private String TMP_EXPORT_PATH;
public ExoPlayerCache(ReactApplicationContext reactContext) {
super(reactContext);
TMP_EXPORT_PATH = getReactApplicationContext().getCacheDir().toString() + "/video-tmp";
File exportPath = new File(TMP_EXPORT_PATH);
// Clear the temporary export files on launch to make sure this doesn't grow infinitely.
if (exportPath.exists()) {
for (File child: exportPath.listFiles()) {
child.delete();
}
}
}
@Override
public String getName() {
return "ExoPlayerCache";
}
@ReactMethod
public void exportVideo(final String url, final Promise promise) {
Log.d(getName(), "exportVideo");
Thread exportThread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(getName(), "Exporting...");
Log.d(getName(), url);
final Uri uri = Uri.parse(url);
final SimpleCache downloadCache = VideoCache.getInstance().getSimpleCache();
final DataSource dataSource = createDataSource(downloadCache);
final DataSpec dataSpec = new DataSpec(uri, 0, C.LENGTH_UNSET, null);
File targetFile = new File(TMP_EXPORT_PATH, uri.getLastPathSegment());
// Create export dir if not exists.
targetFile.getParentFile().mkdirs();
// https://github.com/google/ExoPlayer/issues/5569
try {
CacheUtil.getCached(
dataSpec,
downloadCache,
new ExoplayerCacheKeyFactory()
);
DataSourceInputStream inputStream = new DataSourceInputStream(createDataSource(downloadCache), dataSpec);
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(targetFile), 64 * 1024);
try {
byte[] data = new byte[64 * 1024];
int bytesRead;
while ((bytesRead = inputStream.read(data)) != C.RESULT_END_OF_INPUT) {
outStream.write(data, 0, bytesRead);
}
} catch (IOException e) {
Log.d(getName(), "Write error");
e.printStackTrace();
} finally {
dataSource.close();
outStream.close();
}
Log.d(getName(), "Export succeeded");
Log.d(getName(), targetFile.getPath());
promise.resolve(targetFile.getPath());
} catch (Exception e) {
Log.d(getName(), "Export error");
e.printStackTrace();
promise.reject(e);
}
}
}, "export_thread");
exportThread.start();
}
private CacheDataSource createDataSource(Cache cache) {
return new CacheDataSourceFactory(cache, DataSourceUtil.getDefaultDataSourceFactory(
getReactApplicationContext(),
null,
null
)).createDataSource();
}
}