-
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] Refactor common pieces for header-aware media
- Loading branch information
Showing
5 changed files
with
80 additions
and
85 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
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