Improved filename generation in the stream proxy and a couple other

minor fixes.
This commit is contained in:
casey langen 2017-06-02 21:50:04 -07:00
parent 45a81cb138
commit a40fea7b21
2 changed files with 39 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public class ExoPlayerWrapper extends PlayerWrapper {
private boolean prefetch;
private Context context;
private long lastPosition = -1;
private String uri, proxyUri;
public ExoPlayerWrapper() {
this.context = Application.getInstance();
@ -52,7 +53,8 @@ public class ExoPlayerWrapper extends PlayerWrapper {
Preconditions.throwIfNotOnMainThread();
if (!dead()) {
final String proxyUri = StreamProxy.getProxyUrl(context, uri);
this.uri = uri;
this.proxyUri = StreamProxy.getProxyUrl(context, uri);
this.source = new ExtractorMediaSource(Uri.parse(proxyUri), datasources, extractors, null, null);
this.player.setPlayWhenReady(true);
this.player.prepare(this.source);
@ -66,8 +68,9 @@ public class ExoPlayerWrapper extends PlayerWrapper {
Preconditions.throwIfNotOnMainThread();
if (!dead()) {
this.uri = uri;
this.prefetch = true;
final String proxyUri = StreamProxy.getProxyUrl(context, uri);
this.proxyUri = StreamProxy.getProxyUrl(context, uri);
this.source = new ExtractorMediaSource(Uri.parse(proxyUri), datasources, extractors, null, null);
this.player.setPlayWhenReady(false);
this.player.prepare(this.source);

View File

@ -2,12 +2,17 @@ package io.casey.musikcube.remote.playback;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Base64;
import com.danikula.videocache.CacheListener;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.FileNameGenerator;
import com.danikula.videocache.file.Md5FileNameGenerator;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.casey.musikcube.remote.util.NetworkUtil;
@ -17,6 +22,7 @@ public class StreamProxy {
private static final long BYTES_PER_MEGABYTE = 1048576L;
private static final long BYTES_PER_GIGABYTE = 1073741824L;
private static final Map<Integer, Long> CACHE_SETTING_TO_BYTES;
private static final FileNameGenerator DEFAULT_FILENAME_GENERATOR = new Md5FileNameGenerator();
static {
CACHE_SETTING_TO_BYTES = new HashMap<>();
@ -61,7 +67,22 @@ public class StreamProxy {
final String encoded = Base64.encodeToString(userPass.getBytes(), Base64.NO_WRAP);
headers.put("Authorization", "Basic " + encoded);
return headers;
}).build();
})
.fileNameGenerator((url) -> {
try {
final Uri uri = Uri.parse(url);
/* format is: audio/external_id/<id> */
final List<String> segments = uri.getPathSegments();
if (segments.size() == 3 && "external_id".equals(segments.get(1))) {
return segments.get(2); /* id, should be globally unique. */
}
}
catch (Exception ex) {
/* eh... */
}
return DEFAULT_FILENAME_GENERATOR.generate(url);
})
.build();
}
public static synchronized void init(final Context context) {
@ -70,6 +91,18 @@ public class StreamProxy {
}
}
public static synchronized void registerCacheListener(final CacheListener cl, final String uri) {
if (INSTANCE != null && cl != null) {
INSTANCE.proxy.registerCacheListener(cl, uri); /* let it throw */
}
}
public static synchronized void unregisterCacheListener(final CacheListener cl) {
if (INSTANCE != null && cl != null) {
INSTANCE.proxy.unregisterCacheListener(cl);
}
}
public static synchronized String getProxyUrl(final Context context, final String url) {
init(context);
return INSTANCE.proxy.getProxyUrl(url);