-
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.
[APT-9543] Add support for DASH streams
- Loading branch information
Showing
5 changed files
with
87 additions
and
6 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
74 changes: 74 additions & 0 deletions
74
...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,74 @@ | ||
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.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.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 cacheManager: CacheManager, | ||
private val headersStore: HeadersStore, | ||
private val downloadTracker: DownloadTracker) : MediaSourceGenerator { | ||
|
||
private val previousRequests = mutableMapOf<String, DefaultHttpDataSource.Factory>() | ||
|
||
override fun generateMediaSource(context: Context, request: AudioPlayable.MediaRequest): MediaSource { | ||
downloadTracker.getDownload(request.url.toUri())?.let { | ||
if (it.state != Download.STATE_FAILED) { | ||
return DownloadHelper.createMediaSource(it.request, buildDataSourceFactory(context, request)) | ||
} | ||
} | ||
|
||
val mediaItem = MediaItem.Builder() | ||
.setUri(request.url) | ||
|
||
.build() | ||
|
||
return DashMediaSource.Factory(buildDataSourceFactory(context, request)) | ||
.createMediaSource(mediaItem) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
private fun buildDataSourceFactory(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) | ||
} | ||
|
||
} |
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