Skip to content

Commit

Permalink
Network stack okhttp (#766)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Muller <[email protected]>
  • Loading branch information
StaehliJ and MGaetan89 authored Oct 25, 2024
1 parent 0cc373a commit 8adf7c1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor
androidx-media3-cast = { group = "androidx.media3", name = "media3-cast", version.ref = "androidx-media3" }
androidx-media3-common = { group = "androidx.media3", name = "media3-common", version.ref = "androidx-media3" }
androidx-media3-datasource = { group = "androidx.media3", name = "media3-datasource", version.ref = "androidx-media3" }
androidx-media3-datasource-okhttp = { group = "androidx.media3", name = "media3-datasource-okhttp", version.ref = "androidx-media3" }
androidx-media3-exoplayer = { group = "androidx.media3", name = "media3-exoplayer", version.ref = "androidx-media3" }
androidx-media3-ui = { group = "androidx.media3", name = "media3-ui", version.ref = "androidx-media3" }
androidx-media3-dash = { group = "androidx.media3", name = "media3-exoplayer-dash", version.ref = "androidx-media3" }
Expand Down
2 changes: 2 additions & 0 deletions pillarbox-core-business/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation(libs.androidx.core.ktx)
api(libs.androidx.media3.common)
api(libs.androidx.media3.datasource)
implementation(libs.androidx.media3.datasource.okhttp)
api(libs.androidx.media3.exoplayer)
implementation(libs.guava)
runtimeOnly(libs.kotlinx.coroutines.android)
Expand All @@ -37,6 +38,7 @@ dependencies {
implementation(libs.ktor.http)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktor.utils)
implementation(libs.okhttp)
api(libs.tagcommander.core)

testImplementation(project(":pillarbox-player-testutils"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.annotation.VisibleForTesting
import androidx.media3.common.MediaMetadata
import androidx.media3.datasource.DataSource.Factory
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.okhttp.OkHttpDataSource
import ch.srgssr.pillarbox.analytics.SRGAnalytics
import ch.srgssr.pillarbox.core.business.akamai.AkamaiTokenProvider
import ch.srgssr.pillarbox.core.business.integrationlayer.ResourceSelector
Expand All @@ -20,6 +21,7 @@ import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositi
import ch.srgssr.pillarbox.core.business.tracker.commandersact.CommandersActTracker
import ch.srgssr.pillarbox.core.business.tracker.comscore.ComScoreTracker
import ch.srgssr.pillarbox.player.PillarboxDsl
import ch.srgssr.pillarbox.player.network.PillarboxOkHttp
import ch.srgssr.pillarbox.player.tracker.MediaItemTracker
import ch.srgssr.pillarbox.player.tracker.MutableMediaItemTrackerData
import io.ktor.client.HttpClient
Expand All @@ -31,7 +33,7 @@ import kotlinx.coroutines.Dispatchers
*/
@PillarboxDsl
class SRGAssetLoaderConfig internal constructor(context: Context) {
private var dataSourceFactory: Factory = DefaultDataSource.Factory(context)
private var dataSourceFactory: Factory = DefaultDataSource.Factory(context, OkHttpDataSource.Factory(PillarboxOkHttp()))
private var mediaCompositionService: MediaCompositionService = HttpMediaCompositionService()
private var akamaiTokenProvider = AkamaiTokenProvider()
private var mediaItemTrackerDataConfig: (MutableMediaItemTrackerData.(Resource, Chapter, MediaComposition) -> Unit)? = null
Expand Down
1 change: 1 addition & 0 deletions pillarbox-demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.serialization.core)
implementation(libs.ktor.client.okhttp)
implementation(libs.okhttp)
implementation(libs.srg.data)
implementation(libs.srg.dataprovider.retrofit)
Expand Down
3 changes: 2 additions & 1 deletion pillarbox-player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
api(libs.androidx.media3.common)
implementation(libs.androidx.media3.dash)
implementation(libs.androidx.media3.datasource)
implementation(libs.androidx.media3.datasource.okhttp)
api(libs.androidx.media3.exoplayer)
implementation(libs.androidx.media3.hls)
api(libs.androidx.media3.session)
Expand All @@ -55,7 +56,7 @@ dependencies {
implementation(libs.ktor.serialization)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktor.utils)
implementation(libs.okhttp)
api(libs.okhttp)
implementation(libs.okhttp.logging.interceptor)

testImplementation(project(":pillarbox-player-testutils"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package ch.srgssr.pillarbox.player.network

import androidx.annotation.VisibleForTesting
import ch.srgssr.pillarbox.player.BuildConfig
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.cache.HttpCache
Expand All @@ -14,8 +13,6 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.ClassDiscriminatorMode
import kotlinx.serialization.json.Json
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level

/**
* Provide a Ktor [HttpClient] instance tailored for Pillarbox's needs.
Expand All @@ -39,13 +36,7 @@ object PillarboxHttpClient {
expectSuccess = true

engine {
addInterceptor(
HttpLoggingInterceptor().apply {
val logLevel = if (BuildConfig.DEBUG) Level.BODY else Level.NONE

setLevel(logLevel)
}
)
preconfigured = PillarboxOkHttp()
}

install(HttpCache)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.network

import ch.srgssr.pillarbox.player.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level

/**
* Provide a [OkHttpClient] instance tailored for Pillarbox's needs.
*/
object PillarboxOkHttp {

private val okHttpClient: OkHttpClient by lazy {
OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
val logLevel = if (BuildConfig.DEBUG) Level.BASIC else Level.NONE
setLevel(logLevel)
}
)
.build()
}

/**
* Returns the [OkHttpClient] tailored for Pillarbox's needs.
*
* @return A [OkHttpClient] instance.
*/
operator fun invoke(): OkHttpClient {
return okHttpClient
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package ch.srgssr.pillarbox.player.source

import android.content.Context
import androidx.media3.common.MediaItem
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.okhttp.OkHttpDataSource
import androidx.media3.exoplayer.drm.DrmSessionManagerProvider
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.exoplayer.source.MediaSource
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
import ch.srgssr.pillarbox.player.asset.AssetLoader
import ch.srgssr.pillarbox.player.asset.UrlAssetLoader
import ch.srgssr.pillarbox.player.network.PillarboxOkHttp
import kotlin.time.TimeSource

/**
Expand All @@ -28,7 +31,14 @@ class PillarboxMediaSourceFactory(
/**
* Default asset loader used when no other AssetLoader has been found.
*/
val defaultAssetLoader = UrlAssetLoader(DefaultMediaSourceFactory(context))
val defaultAssetLoader = UrlAssetLoader(
DefaultMediaSourceFactory(
DefaultDataSource.Factory(
context,
OkHttpDataSource.Factory(PillarboxOkHttp())
)
)
)

/**
* Minimal duration in milliseconds to consider a live with seek capabilities.
Expand Down

0 comments on commit 8adf7c1

Please sign in to comment.