|
|
|
|
@ -5,16 +5,21 @@ import android.net.Uri; |
|
|
|
|
|
|
|
|
|
import com.fongmi.android.tv.App; |
|
|
|
|
import com.fongmi.android.tv.bean.Result; |
|
|
|
|
import com.fongmi.android.tv.utils.FileUtil; |
|
|
|
|
import com.github.catvod.crawler.SpiderDebug; |
|
|
|
|
import com.google.android.exoplayer2.MediaItem; |
|
|
|
|
import com.google.android.exoplayer2.ext.rtmp.RtmpDataSource; |
|
|
|
|
import com.google.android.exoplayer2.database.DatabaseProvider; |
|
|
|
|
import com.google.android.exoplayer2.database.StandaloneDatabaseProvider; |
|
|
|
|
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory; |
|
|
|
|
import com.google.android.exoplayer2.source.MediaSource; |
|
|
|
|
import com.google.android.exoplayer2.ui.CaptionStyleCompat; |
|
|
|
|
import com.google.android.exoplayer2.upstream.DataSource; |
|
|
|
|
import com.google.android.exoplayer2.upstream.DefaultDataSource; |
|
|
|
|
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; |
|
|
|
|
import com.google.android.exoplayer2.upstream.HttpDataSource; |
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.Cache; |
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; |
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor; |
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.SimpleCache; |
|
|
|
|
import com.google.android.exoplayer2.util.MimeTypes; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
@ -24,6 +29,9 @@ import java.util.Map; |
|
|
|
|
|
|
|
|
|
public class ExoUtil { |
|
|
|
|
|
|
|
|
|
private static DatabaseProvider database; |
|
|
|
|
private static Cache cache; |
|
|
|
|
|
|
|
|
|
public static CaptionStyleCompat getCaptionStyle() { |
|
|
|
|
return new CaptionStyleCompat(Color.WHITE, Color.TRANSPARENT, Color.TRANSPARENT, CaptionStyleCompat.EDGE_TYPE_OUTLINE, Color.BLACK, null); |
|
|
|
|
} |
|
|
|
|
@ -39,7 +47,7 @@ public class ExoUtil { |
|
|
|
|
private static MediaSource getSource(Map<String, String> headers, String url, List<MediaItem.SubtitleConfiguration> config) { |
|
|
|
|
SpiderDebug.log(url); |
|
|
|
|
Uri videoUri = Uri.parse(url); |
|
|
|
|
DataSource.Factory factory = getFactory(headers, url); |
|
|
|
|
DataSource.Factory factory = getDataSourceFactory(headers); |
|
|
|
|
MediaItem.Builder builder = new MediaItem.Builder().setUri(videoUri); |
|
|
|
|
if (url.contains("php") || url.contains("m3u8")) builder.setMimeType(MimeTypes.APPLICATION_M3U8); |
|
|
|
|
if (config.size() > 0) builder.setSubtitleConfigurations(config); |
|
|
|
|
@ -57,8 +65,26 @@ public class ExoUtil { |
|
|
|
|
return items; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static DataSource.Factory getFactory(Map<String, String> headers, String url) { |
|
|
|
|
HttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setDefaultRequestProperties(headers).setConnectTimeoutMs(10000).setReadTimeoutMs(10000).setAllowCrossProtocolRedirects(true); |
|
|
|
|
return url.startsWith("rtmp") ? new RtmpDataSource.Factory() : new DefaultDataSource.Factory(App.get(), httpDataSourceFactory); |
|
|
|
|
private static synchronized DataSource.Factory getHttpDataSourceFactory(Map<String, String> headers) { |
|
|
|
|
return new DefaultHttpDataSource.Factory().setDefaultRequestProperties(headers).setConnectTimeoutMs(5000).setReadTimeoutMs(5000).setAllowCrossProtocolRedirects(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static synchronized DataSource.Factory getDataSourceFactory(Map<String, String> headers) { |
|
|
|
|
DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(App.get(), getHttpDataSourceFactory(headers)); |
|
|
|
|
return buildReadOnlyCacheDataSource(upstreamFactory, getCache()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static CacheDataSource.Factory buildReadOnlyCacheDataSource(DataSource.Factory upstreamFactory, Cache cache) { |
|
|
|
|
return new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(upstreamFactory).setCacheWriteDataSinkFactory(null).setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static synchronized DatabaseProvider getDatabase() { |
|
|
|
|
if (database == null) database = new StandaloneDatabaseProvider(App.get()); |
|
|
|
|
return database; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static synchronized Cache getCache() { |
|
|
|
|
if (cache == null) cache = new SimpleCache(FileUtil.getCacheDir(), new NoOpCacheEvictor(), getDatabase()); |
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|