Skip to content

Commit

Permalink
chore(android): migrate DataSourceUtil to Kotlin (#3984)
Browse files Browse the repository at this point in the history
* Rename .java to .kt

* chore(android): migrate DataSourceUtil to Kotlin

* fix: type issue in DataSourceUtil

* fix: lint error

* chore: refactor DataSourceUtil with object & @JvmStatic

* fix: lint error
  • Loading branch information
seyedmostafahasani authored Jul 11, 2024
1 parent 452e42f commit b25e43e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 98 deletions.
96 changes: 0 additions & 96 deletions android/src/main/java/com/brentvatne/exoplayer/DataSourceUtil.java

This file was deleted.

88 changes: 88 additions & 0 deletions android/src/main/java/com/brentvatne/exoplayer/DataSourceUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.brentvatne.exoplayer

import android.net.Uri
import androidx.media3.common.util.Util
import androidx.media3.datasource.AssetDataSource
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.HttpDataSource
import androidx.media3.datasource.okhttp.OkHttpDataSource
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter
import com.facebook.react.bridge.ReactContext
import com.facebook.react.modules.network.CookieJarContainer
import com.facebook.react.modules.network.ForwardingCookieHandler
import com.facebook.react.modules.network.OkHttpClientProvider
import okhttp3.Call
import okhttp3.JavaNetCookieJar

object DataSourceUtil {
private var defaultDataSourceFactory: DataSource.Factory? = null
private var defaultHttpDataSourceFactory: HttpDataSource.Factory? = null
private var userAgent: String? = null

private fun getUserAgent(context: ReactContext): String {
if (userAgent == null) {
userAgent = Util.getUserAgent(context, context.packageName)
}
return userAgent as String
}

@JvmStatic
fun getDefaultDataSourceFactory(context: ReactContext, bandwidthMeter: DefaultBandwidthMeter?, requestHeaders: Map<String, String>?): DataSource.Factory {
if (defaultDataSourceFactory == null || !requestHeaders.isNullOrEmpty()) {
defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter, requestHeaders)
}
return defaultDataSourceFactory as DataSource.Factory
}

@JvmStatic
fun getDefaultHttpDataSourceFactory(
context: ReactContext,
bandwidthMeter: DefaultBandwidthMeter?,
requestHeaders: Map<String, String>?
): HttpDataSource.Factory {
if (defaultHttpDataSourceFactory == null || !requestHeaders.isNullOrEmpty()) {
defaultHttpDataSourceFactory = buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders)
}
return defaultHttpDataSourceFactory as HttpDataSource.Factory
}

private fun buildDataSourceFactory(
context: ReactContext,
bandwidthMeter: DefaultBandwidthMeter?,
requestHeaders: Map<String, String>?
): DataSource.Factory = DefaultDataSource.Factory(context, buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders))

private fun buildHttpDataSourceFactory(
context: ReactContext,
bandwidthMeter: DefaultBandwidthMeter?,
requestHeaders: Map<String, String>?
): HttpDataSource.Factory {
val client = OkHttpClientProvider.getOkHttpClient()
val container = client.cookieJar as CookieJarContainer
val handler = ForwardingCookieHandler(context)
container.setCookieJar(JavaNetCookieJar(handler))
val okHttpDataSourceFactory = OkHttpDataSource.Factory(client as Call.Factory)
.setTransferListener(bandwidthMeter)

if (requestHeaders != null) {
okHttpDataSourceFactory.setDefaultRequestProperties(requestHeaders)
if (!requestHeaders.containsKey("User-Agent")) {
okHttpDataSourceFactory.setUserAgent(getUserAgent(context))
}
} else {
okHttpDataSourceFactory.setUserAgent(getUserAgent(context))
}

return okHttpDataSourceFactory
}

@JvmStatic
fun buildAssetDataSourceFactory(context: ReactContext?, srcUri: Uri?): DataSource.Factory {
val dataSpec = DataSpec(srcUri!!)
val rawResourceDataSource = AssetDataSource(context!!)
rawResourceDataSource.open(dataSpec)
return DataSource.Factory { rawResourceDataSource }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static androidx.media3.common.C.CONTENT_TYPE_SS;
import static androidx.media3.common.C.TIME_END_OF_SOURCE;

import static com.brentvatne.exoplayer.DataSourceUtil.buildAssetDataSourceFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -1060,7 +1059,7 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi
case CONTENT_TYPE_OTHER:
if ("asset".equals(uri.getScheme())) {
try {
DataSource.Factory assetDataSourceFactory = buildAssetDataSourceFactory(themedReactContext, uri);
DataSource.Factory assetDataSourceFactory = DataSourceUtil.buildAssetDataSourceFactory(themedReactContext, uri);
mediaSourceFactory = new ProgressiveMediaSource.Factory(assetDataSourceFactory);
} catch (Exception e) {
throw new IllegalStateException("cannot open input file" + uri);
Expand Down

0 comments on commit b25e43e

Please sign in to comment.