|
| 1 | +package com.brentvatne.exoplayer; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.Uri; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor; |
| 8 | +import com.google.android.exoplayer2.upstream.cache.Cache; |
| 9 | +import com.google.android.exoplayer2.upstream.cache.SimpleCache; |
| 10 | +import com.google.android.exoplayer2.upstream.cache.CacheUtil; |
| 11 | +import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; |
| 12 | +import com.google.android.exoplayer2.upstream.DataSpec; |
| 13 | +import com.google.android.exoplayer2.upstream.DataSource; |
| 14 | +import com.google.android.exoplayer2.upstream.DataSourceInputStream; |
| 15 | + |
| 16 | +import com.facebook.react.bridge.NativeModule; |
| 17 | +import com.facebook.react.bridge.Promise; |
| 18 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 19 | +import com.facebook.react.bridge.ReactContext; |
| 20 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 21 | +import com.facebook.react.bridge.ReactMethod; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileOutputStream; |
| 26 | +import java.io.OutputStream; |
| 27 | + |
| 28 | +public class ExoPlayerCache extends ReactContextBaseJavaModule { |
| 29 | + |
| 30 | + private static SimpleCache instance = null; |
| 31 | + |
| 32 | + |
| 33 | + public ExoPlayerCache(ReactApplicationContext reactContext) { |
| 34 | + super(reactContext); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getName() { |
| 39 | + return "ExoPlayerCache"; |
| 40 | + } |
| 41 | + |
| 42 | + @ReactMethod |
| 43 | + public void exportVideo(final String url, final Promise promise) { |
| 44 | + Log.d(getName(), "exportVideo"); |
| 45 | + |
| 46 | + Thread exportThread = new Thread(new Runnable() { |
| 47 | + @Override |
| 48 | + public void run() { |
| 49 | + Log.d(getName(), "Exporting..."); |
| 50 | + Log.d(getName(), url); |
| 51 | + final Uri uri = Uri.parse(url); |
| 52 | + final DataSpec dataSpec = new DataSpec(uri, 0, 100 * 1024 * 1024, null); // TODO won't work for video's over 100 MB |
| 53 | + final SimpleCache downloadCache = ExoPlayerCache.getInstance(getReactApplicationContext()); |
| 54 | + CacheUtil.CachingCounters counters = new CacheUtil.CachingCounters(); |
| 55 | + |
| 56 | + try { |
| 57 | + CacheUtil.getCached( |
| 58 | + dataSpec, |
| 59 | + downloadCache, |
| 60 | + counters |
| 61 | + ); |
| 62 | + |
| 63 | + // TODO check counters for when download is not complete // Download can complete during writing |
| 64 | + Log.d(getName(), "Cached " + counters.totalCachedBytes() + " bytes (start)"); |
| 65 | + |
| 66 | + DataSourceInputStream inputStream = new DataSourceInputStream(createDataSource(downloadCache), dataSpec); |
| 67 | + |
| 68 | + File targetFile = new File(ExoPlayerCache.getCacheDir(getReactApplicationContext()) + "/" + uri.getLastPathSegment()); |
| 69 | + OutputStream outStream = new FileOutputStream(targetFile); |
| 70 | + |
| 71 | + byte[] buffer = new byte[8 * 1024]; |
| 72 | + int bytesRead; |
| 73 | + try { |
| 74 | + while ((bytesRead = inputStream.read(buffer)) != -1) { |
| 75 | + outStream.write(buffer, 0, bytesRead); |
| 76 | + } |
| 77 | + } catch (IOException e) { |
| 78 | + // TODO this exception should not be thrown |
| 79 | + Log.d(getName(), "Read error"); |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + |
| 83 | + CacheUtil.getCached( |
| 84 | + dataSpec, |
| 85 | + downloadCache, |
| 86 | + counters |
| 87 | + ); |
| 88 | + |
| 89 | + // TODO are we sure the complete video is downloaded? |
| 90 | + Log.d(getName(), "Cached " + counters.totalCachedBytes() + " bytes (end)"); |
| 91 | + |
| 92 | + Log.d(getName(), "Export succeeded"); |
| 93 | + Log.d(getName(), targetFile.getPath()); |
| 94 | + |
| 95 | + promise.resolve(targetFile.getPath()); |
| 96 | + } catch (Exception e) { |
| 97 | + Log.d(getName(), "Export error"); |
| 98 | + e.printStackTrace(); |
| 99 | + promise.reject(e); |
| 100 | + } |
| 101 | + } |
| 102 | + }, "export_thread"); |
| 103 | + exportThread.start(); |
| 104 | + } |
| 105 | + |
| 106 | + public static SimpleCache getInstance(Context context) { |
| 107 | + if(instance == null) { |
| 108 | + instance = new SimpleCache(new File(ExoPlayerCache.getCacheDir(context)), new NoOpCacheEvictor()); |
| 109 | + } |
| 110 | + return instance; |
| 111 | + } |
| 112 | + |
| 113 | + private static String getCacheDir(Context context) { |
| 114 | + return context.getCacheDir().toString() + "/video"; |
| 115 | + } |
| 116 | + |
| 117 | + private DataSource createDataSource(Cache cache) { |
| 118 | + return new CacheDataSourceFactory(cache, DataSourceUtil.getDefaultDataSourceFactory( |
| 119 | + getReactApplicationContext(), |
| 120 | + null, |
| 121 | + null |
| 122 | + )).createDataSource(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments