Skip to content

Commit

Permalink
Remove ImageScalingService from the public API of `pillarbox-core-b…
Browse files Browse the repository at this point in the history
…usiness`
  • Loading branch information
MGaetan89 committed Nov 29, 2023
1 parent 13fc1e1 commit 843ec0e
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 214 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ accompanist = "0.33.0-alpha"
activityCompose = "1.8.0"
androidGradlePlugin = "8.1.2"
coil = "2.5.0"
dataProvider = "0.6.2"
dataProvider = "0.7.7"
kotlinPlugin = "1.9.10"
detekt = "1.23.3" # https://github.com/detekt/detekt
kotlinxSerializationJson = "1.6.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ch.srgssr.pillarbox.analytics.commandersact.CommandersAct
import ch.srgssr.pillarbox.analytics.commandersact.CommandersActPageView
import ch.srgssr.pillarbox.analytics.commandersact.MediaEventType
import ch.srgssr.pillarbox.analytics.commandersact.TCMediaEvent
import ch.srgssr.pillarbox.core.business.images.DefaultImageScalingService
import ch.srgssr.pillarbox.core.business.tracker.DefaultMediaItemTrackerRepository
import ch.srgssr.pillarbox.core.business.tracker.commandersact.CommandersActStreaming
import ch.srgssr.pillarbox.player.PillarboxPlayer
Expand Down Expand Up @@ -47,7 +46,6 @@ class CommandersActTrackerTest {
context = context,
mediaItemSource = MediaCompositionMediaItemSource(
mediaCompositionDataSource = LocalMediaCompositionDataSource(context),
imageScalingService = DefaultImageScalingService()
),
mediaItemTrackerProvider = DefaultMediaItemTrackerRepository(
trackerRepository = MediaItemTrackerRepository(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.media3.datasource.DataSource
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.LoadControl
import ch.srgssr.pillarbox.core.business.akamai.AkamaiTokenDataSource
import ch.srgssr.pillarbox.core.business.images.DefaultImageScalingService
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultMediaCompositionDataSource
import ch.srgssr.pillarbox.core.business.tracker.DefaultMediaItemTrackerRepository
import ch.srgssr.pillarbox.player.PillarboxPlayer
Expand Down Expand Up @@ -41,7 +40,6 @@ object DefaultPillarbox {
mediaItemTrackerRepository: MediaItemTrackerProvider = DefaultMediaItemTrackerRepository(),
mediaItemSource: MediaItemSource = MediaCompositionMediaItemSource(
mediaCompositionDataSource = DefaultMediaCompositionDataSource(),
imageScalingService = DefaultImageScalingService()
),
dataSourceFactory: DataSource.Factory = AkamaiTokenDataSource.Factory(),
loadControl: LoadControl = DefaultLoadControl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
package ch.srgssr.pillarbox.core.business

import android.net.Uri
import androidx.annotation.Px
import androidx.core.net.toUri
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource.ImageScalingService.ImageWidth
import ch.srgssr.pillarbox.core.business.exception.BlockReasonException
import ch.srgssr.pillarbox.core.business.exception.DataParsingException
import ch.srgssr.pillarbox.core.business.exception.ResourceNotFoundException
import ch.srgssr.pillarbox.core.business.images.ImageScalingService
import ch.srgssr.pillarbox.core.business.images.ImageScalingService.ImageFormat
import ch.srgssr.pillarbox.core.business.images.ImageScalingService.ImageWidth
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Drm
import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition
import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaUrn
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Resource
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositionDataSource
import ch.srgssr.pillarbox.core.business.tracker.SRGEventLoggerTracker
import ch.srgssr.pillarbox.core.business.tracker.commandersact.CommandersActTracker
Expand All @@ -28,8 +28,11 @@ import ch.srgssr.pillarbox.player.data.MediaItemSource
import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerData
import ch.srgssr.pillarbox.player.extension.setTrackerData
import io.ktor.client.plugins.ClientRequestException
import io.ktor.http.URLBuilder
import io.ktor.http.appendEncodedPathSegments
import kotlinx.serialization.SerializationException
import java.io.IOException
import java.net.URL

/**
* Load [MediaItem] playable from a [ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition]
Expand All @@ -41,15 +44,14 @@ import java.io.IOException
* - [MediaMetadata.description] with [Chapter.description]
*
* @param mediaCompositionDataSource The MediaCompositionDataSource to use to load a MediaComposition.
* @param imageScalingService The ImageScaleService to use to get a scaled image.
* @param trackerDataProvider The TrackerDataProvider to customize TrackerData.
*/
class MediaCompositionMediaItemSource(
private val mediaCompositionDataSource: MediaCompositionDataSource,
private val imageScalingService: ImageScalingService,
private val trackerDataProvider: TrackerDataProvider? = null
) : MediaItemSource {
private val resourceSelector = ResourceSelector()
private val imageScalingService = ImageScalingService()

private fun fillMetaData(metadata: MediaMetadata, chapter: Chapter): MediaMetadata {
val builder = metadata.buildUpon()
Expand All @@ -60,7 +62,6 @@ class MediaCompositionMediaItemSource(
val artworkUri = imageScalingService.getScaledImageUrl(
imageUrl = chapter.imageUrl,
width = ImageWidth.W480,
format = ImageFormat.WEBP
).toUri()

builder.setArtworkUri(artworkUri)
Expand Down Expand Up @@ -131,7 +132,7 @@ class MediaCompositionMediaItemSource(
/**
* Select a [Resource] from [Chapter.listResource]
*/
class ResourceSelector {
internal class ResourceSelector {
/**
* Select the first resource from chapter that is playable by the Player.
*
Expand All @@ -151,6 +152,43 @@ class MediaCompositionMediaItemSource(
}
}

/**
* Service used to get a scaled image URL. This only works for SRG images.
*
* @param baseUrl Base URL of the service.
*/
internal class ImageScalingService(
private val baseUrl: URL = IlHost.DEFAULT
) {
/**
* The supported widths.
*
* @property width The width in pixels.
*/
enum class ImageWidth(@Px val width: Int) {
W240(width = 240),
W320(width = 320),
W480(width = 480),
W960(width = 960),
W1920(width = 1920)
}

fun getScaledImageUrl(
imageUrl: String,
width: ImageWidth,
): String {
return URLBuilder(baseUrl.toString())
.appendEncodedPathSegments("images/")
.apply {
parameters.append("imageUrl", imageUrl)
parameters.append("format", "webp")
parameters.append("width", width.width.toString())
}
.build()
.toString()
}
}

companion object {
/**
* Token Query Param to add to trigger token request
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business

import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource.ImageScalingService
import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource.ImageScalingService.ImageWidth
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
import org.junit.Assert.assertEquals
import org.junit.Test
import java.net.URLEncoder

class ImageScalingServiceTest {
@Test
fun getScaledImageUrlProd() {
val baseUrl = IlHost.PROD
val imageUrl = "https://www.rts.ch/2020/05/18/14/20/11333286.image/16x9"
val width = ImageWidth.W240

val imageScalingService = ImageScalingService(baseUrl)
val scaledImageUrl = imageScalingService.getScaledImageUrl(imageUrl, width)
val encodedImageUrl = URLEncoder.encode(imageUrl, Charsets.UTF_8)

assertEquals("${baseUrl}images/?imageUrl=$encodedImageUrl&format=webp&width=${width.width}", scaledImageUrl)
}

@Test
fun getScaledImageUrlTest() {
val baseUrl = IlHost.TEST
val imageUrl = "https://www.rts.ch/2021/08/05/18/12/12396566.image/2x3"
val width = ImageWidth.W320

val imageScalingService = ImageScalingService(baseUrl)
val scaledImageUrl = imageScalingService.getScaledImageUrl(imageUrl, width)
val encodedImageUrl = URLEncoder.encode(imageUrl, Charsets.UTF_8)

assertEquals("${baseUrl}images/?imageUrl=$encodedImageUrl&format=webp&width=${width.width}", scaledImageUrl)
}

@Test
fun getScaledImageUrlStage() {
val baseUrl = IlHost.STAGE
val imageUrl = "https://www.rts.ch/2022/10/06/17/32/13444418.image/4x5"
val width = ImageWidth.W480

val imageScalingService = ImageScalingService(baseUrl)
val scaledImageUrl = imageScalingService.getScaledImageUrl(imageUrl, width)
val encodedImageUrl = URLEncoder.encode(imageUrl, Charsets.UTF_8)

assertEquals("${baseUrl}images/?imageUrl=$encodedImageUrl&format=webp&width=${width.width}", scaledImageUrl)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import ch.srgssr.pillarbox.core.business.exception.BlockReasonException
import ch.srgssr.pillarbox.core.business.exception.ResourceNotFoundException
import ch.srgssr.pillarbox.core.business.images.ImageScalingService
import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter
import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition
Expand All @@ -23,7 +22,6 @@ class MediaCompositionMediaItemSourceTest {

private val mediaItemSource = MediaCompositionMediaItemSource(
mediaCompositionDataSource = DummyMediaCompositionProvider(),
imageScalingService = NoOpImageScalingService()
)

@Test(expected = IllegalArgumentException::class)
Expand Down Expand Up @@ -186,12 +184,6 @@ class MediaCompositionMediaItemSourceTest {
}
}

private class NoOpImageScalingService : ImageScalingService {
override fun getScaledImageUrl(imageUrl: String, width: ImageScalingService.ImageWidth, format: ImageScalingService.ImageFormat): String {
return imageUrl
}
}

companion object {
private fun createMediaItem(urn: String): MediaItem {
return MediaItem.Builder().setMediaId(urn).build()
Expand Down
Loading

0 comments on commit 843ec0e

Please sign in to comment.