From 486bc9c96d549d97e7a04bf3322acc2b3cf804de Mon Sep 17 00:00:00 2001 From: casey langen Date: Fri, 19 Jan 2018 15:59:30 -0800 Subject: [PATCH] Small changes to allow for blocking seek in GaplessExoPlayerWrapper. --- .../remote/service/playback/PlayerWrapper.kt | 2 +- .../playback/impl/player/ExoPlayerWrapper.kt | 3 ++- .../impl/player/GaplessExoPlayerWrapper.kt | 19 ++++++++++++------- .../impl/player/MediaPlayerWrapper.kt | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/PlayerWrapper.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/PlayerWrapper.kt index f80ca5167..e55f4093c 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/PlayerWrapper.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/PlayerWrapper.kt @@ -69,7 +69,7 @@ abstract class PlayerWrapper { } } - abstract fun play(uri: String, metadata: ITrack) + abstract fun play(uri: String, metadata: ITrack, offsetMs: Int = 0) abstract fun prefetch(uri: String, metadata: ITrack) abstract fun pause() abstract fun resume() diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/ExoPlayerWrapper.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/ExoPlayerWrapper.kt index b2a5fe7c1..b79a77069 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/ExoPlayerWrapper.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/ExoPlayerWrapper.kt @@ -54,13 +54,14 @@ class ExoPlayerWrapper : PlayerWrapper() { this.transcoding = this.prefs.getInt(Prefs.Key.TRANSCODER_BITRATE_INDEX, 0) != 0 } - override fun play(uri: String, metadata: ITrack) { + override fun play(uri: String, metadata: ITrack, offsetMs: Int) { Preconditions.throwIfNotOnMainThread() if (!dead()) { this.metadata = metadata this.originalUri = uri this.proxyUri = streamProxy.getProxyUrl(uri) + Log.d("ExoPlayerWrapper", "originalUri: ${this.originalUri} proxyUri: ${this.proxyUri}") addCacheListener() diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/GaplessExoPlayerWrapper.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/GaplessExoPlayerWrapper.kt index 0d0f098c9..56c2c80fd 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/GaplessExoPlayerWrapper.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/GaplessExoPlayerWrapper.kt @@ -15,19 +15,18 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.trackselection.TrackSelectionArray import com.google.android.exoplayer2.upstream.DataSource import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter -import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory +import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory import com.google.android.exoplayer2.util.Util import io.casey.musikcube.remote.Application import io.casey.musikcube.remote.service.playback.PlayerWrapper -import io.casey.musikcube.remote.service.playback.impl.streaming.StreamProxy import io.casey.musikcube.remote.service.websocket.model.ITrack import io.casey.musikcube.remote.ui.settings.constants.Prefs import io.casey.musikcube.remote.util.Preconditions import java.io.File class GaplessExoPlayerWrapper : PlayerWrapper() { - private var sourceFactory: DataSource.Factory = - DefaultDataSourceFactory(context, Util.getUserAgent(context, "musikdroid")) + private var sourceFactory: DataSource.Factory = DefaultHttpDataSourceFactory( + Util.getUserAgent(context, "musikdroid"), null, TIMEOUT, TIMEOUT, true) private val extractorsFactory = DefaultExtractorsFactory() private var source: MediaSource? = null @@ -38,12 +37,13 @@ class GaplessExoPlayerWrapper : PlayerWrapper() { private var originalUri: String? = null private var proxyUri: String? = null private val transcoding: Boolean + private var initialOffsetMs: Int = 0 init { this.transcoding = prefs.getInt(Prefs.Key.TRANSCODER_BITRATE_INDEX, 0) != 0 } - override fun play(uri: String, metadata: ITrack) { + override fun play(uri: String, metadata: ITrack, offsetMs: Int) { Preconditions.throwIfNotOnMainThread() if (!dead()) { @@ -52,6 +52,7 @@ class GaplessExoPlayerWrapper : PlayerWrapper() { this.metadata = metadata this.originalUri = uri this.proxyUri = streamProxy.getProxyUrl(uri) + this.initialOffsetMs = offsetMs addCacheListener() @@ -125,11 +126,13 @@ class GaplessExoPlayerWrapper : PlayerWrapper() { if (gaplessPlayer?.playbackState != ExoPlayer.STATE_IDLE) { if (gaplessPlayer?.isCurrentWindowSeekable == true) { var offset = millis.toLong() + val isInitialSeek = initialOffsetMs > 0 && (position == initialOffsetMs) /* if we're transcoding we don't want to seek arbitrarily because it may put a lot of pressure on the backend. just allow seeking up to what we currently - have buffered! */ - if (transcoding && percentAvailable != 100) { + have buffered! one exception: if we transfer playback context from the backend + to here, we want to wait until we are able to pickup where we left off. */ + if (!isInitialSeek && transcoding && percentAvailable != 100) { /* give ourselves 2% wiggle room! */ val percent = Math.max(0, percentAvailable - 2).toFloat() / 100.0f val totalMs = gaplessPlayer?.duration @@ -137,6 +140,7 @@ class GaplessExoPlayerWrapper : PlayerWrapper() { offset = Math.min(millis.toLong(), available) } + initialOffsetMs = 0 gaplessPlayer?.seekTo(offset) } } @@ -287,6 +291,7 @@ class GaplessExoPlayerWrapper : PlayerWrapper() { } companion object { + const val TIMEOUT = 1000 * 60 * 2 /* 2 minutes; makes seeking an incomplete transcode work most of the time */ private val prefs: SharedPreferences by lazy { Application.instance!!.getSharedPreferences(Prefs.NAME, Context.MODE_PRIVATE) } private val context: Context by lazy { Application.instance!! } private val trackSelector = DefaultTrackSelector(AdaptiveTrackSelection.Factory(DefaultBandwidthMeter())) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/MediaPlayerWrapper.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/MediaPlayerWrapper.kt index f0114f40e..32cb4e199 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/MediaPlayerWrapper.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/player/MediaPlayerWrapper.kt @@ -32,7 +32,7 @@ class MediaPlayerWrapper : PlayerWrapper() { this.prefs = context!!.getSharedPreferences(Prefs.NAME, Context.MODE_PRIVATE) } - override fun play(uri: String, metadata: ITrack) { + override fun play(uri: String, metadata: ITrack, offsetMs: Int) { Preconditions.throwIfNotOnMainThread() try {