-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from scribd/karl/APT-9543-dash
Add MPEG-DASH support to playback
- Loading branch information
Showing
11 changed files
with
127 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...dillo/src/main/java/com/scribd/armadillo/playback/mediasource/DashMediaSourceGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.scribd.armadillo.playback.mediasource | ||
|
||
import android.content.Context | ||
import com.google.android.exoplayer2.MediaItem | ||
import com.google.android.exoplayer2.offline.Download | ||
import com.google.android.exoplayer2.offline.DownloadHelper | ||
import com.google.android.exoplayer2.source.MediaSource | ||
import com.google.android.exoplayer2.source.dash.DashMediaSource | ||
import com.scribd.armadillo.download.DownloadTracker | ||
import com.scribd.armadillo.extensions.toUri | ||
import com.scribd.armadillo.models.AudioPlayable | ||
import javax.inject.Inject | ||
|
||
internal class DashMediaSourceGenerator @Inject constructor( | ||
private val mediaSourceHelper: HeadersMediaSourceHelper, | ||
private val downloadTracker: DownloadTracker) : MediaSourceGenerator { | ||
|
||
override fun generateMediaSource(context: Context, request: AudioPlayable.MediaRequest): MediaSource { | ||
val dataSourceFactory = mediaSourceHelper.createDataSourceFactory(context, request) | ||
|
||
downloadTracker.getDownload(request.url.toUri())?.let { | ||
if (it.state != Download.STATE_FAILED) { | ||
return DownloadHelper.createMediaSource(it.request, dataSourceFactory) | ||
} | ||
} | ||
|
||
val mediaItem = MediaItem.Builder() | ||
.setUri(request.url) | ||
.build() | ||
|
||
return DashMediaSource.Factory(dataSourceFactory) | ||
.createMediaSource(mediaItem) | ||
} | ||
|
||
override fun updateMediaSourceHeaders(request: AudioPlayable.MediaRequest) = mediaSourceHelper.updateMediaSourceHeaders(request) | ||
} |
10 changes: 10 additions & 0 deletions
10
...dillo/src/main/java/com/scribd/armadillo/playback/mediasource/HeadersMediaSourceHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.scribd.armadillo.playback.mediasource | ||
|
||
import android.content.Context | ||
import com.google.android.exoplayer2.upstream.DataSource | ||
import com.scribd.armadillo.models.AudioPlayable | ||
|
||
internal interface HeadersMediaSourceHelper { | ||
fun createDataSourceFactory(context: Context, request: AudioPlayable.MediaRequest): DataSource.Factory | ||
fun updateMediaSourceHeaders(request: AudioPlayable.MediaRequest) | ||
} |
52 changes: 52 additions & 0 deletions
52
...o/src/main/java/com/scribd/armadillo/playback/mediasource/HeadersMediaSourceHelperImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.scribd.armadillo.playback.mediasource | ||
|
||
import android.content.Context | ||
import com.google.android.exoplayer2.upstream.DataSource | ||
import com.google.android.exoplayer2.upstream.DefaultDataSource | ||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource | ||
import com.scribd.armadillo.Constants | ||
import com.scribd.armadillo.HeadersStore | ||
import com.scribd.armadillo.download.CacheManager | ||
import com.scribd.armadillo.models.AudioPlayable | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class HeadersMediaSourceHelperImpl @Inject constructor( | ||
private val cacheManager: CacheManager, | ||
private val headersStore: HeadersStore | ||
): HeadersMediaSourceHelper { | ||
private val previousRequests = mutableMapOf<String, DefaultHttpDataSource.Factory>() | ||
|
||
override fun createDataSourceFactory(context: Context, request: AudioPlayable.MediaRequest): DataSource.Factory { | ||
val httpDataSourceFactory = DefaultHttpDataSource.Factory() | ||
.setUserAgent(Constants.getUserAgent(context)) | ||
.setConnectTimeoutMs(DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS) | ||
.setReadTimeoutMs(DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS) | ||
.setAllowCrossProtocolRedirects(true) | ||
|
||
previousRequests[request.url] = httpDataSourceFactory | ||
if (request.headers.isNotEmpty()) { | ||
headersStore.keyForUrl(request.url)?.let { | ||
headersStore.setHeaders(it, request.headers) | ||
} | ||
httpDataSourceFactory.setDefaultRequestProperties(request.headers) | ||
} | ||
|
||
val upstreamFactory = DefaultDataSource.Factory(context, httpDataSourceFactory) | ||
return cacheManager.playbackDataSourceFactory(context, upstreamFactory) | ||
|
||
} | ||
|
||
override fun updateMediaSourceHeaders(request: AudioPlayable.MediaRequest) { | ||
previousRequests[request.url]?.let { factory -> | ||
if (request.headers.isNotEmpty()) { | ||
headersStore.keyForUrl(request.url)?.let { | ||
headersStore.setHeaders(it, request.headers) | ||
} | ||
// Updating the factory instance updates future requests generated from this factory by ExoPlayer | ||
factory.setDefaultRequestProperties(request.headers) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters