Skip to content

Commit

Permalink
make meta repositories forward the HttpDataSource.Factory of the inne…
Browse files Browse the repository at this point in the history
…r repo
  • Loading branch information
theScrabi committed Nov 1, 2024
1 parent e914e94 commit 7d091e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package net.newpipe.newplayer.repository

import android.graphics.Bitmap
import androidx.media3.common.MediaMetadata
import androidx.media3.datasource.HttpDataSource
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
Expand Down Expand Up @@ -134,6 +135,10 @@ class CachingRepository(
actualRepository.getTimestampLink(item, timestampInSeconds)
}

override fun getHttpDataSourceFactory(item: String): HttpDataSource.Factory {
return actualRepository.getHttpDataSourceFactory(item)
}

/**
* Will flush the caches.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package net.newpipe.newplayer.repository

import android.graphics.Bitmap
import androidx.media3.common.MediaMetadata
import androidx.media3.datasource.HttpDataSource
import kotlinx.coroutines.delay
import net.newpipe.newplayer.data.Chapter
import net.newpipe.newplayer.data.Stream
Expand Down Expand Up @@ -72,4 +73,8 @@ class DelayTestRepository(val actualRepo: MediaRepository, var delayInMS: Long)
delay(delayInMS)
return actualRepo.getTimestampLink(item, timestampInSeconds)
}

override fun getHttpDataSourceFactory(item: String): HttpDataSource.Factory {
return actualRepo.getHttpDataSourceFactory(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package net.newpipe.newplayer.repository

import androidx.media3.datasource.HttpDataSource
import net.newpipe.newplayer.data.NewPlayerException

/**
Expand Down Expand Up @@ -48,16 +49,20 @@ class MultiRepository(val actualRepositories: List<MultiRepoEntry>) : MediaRepos
repos
}

private suspend fun <T> pickAndExecuteRepo(
private data class RepoSelection (
val repo: MediaRepository,
val item: String
)

private fun getActualRepoAndItem(
item: String,
exec: suspend (MediaRepository, String) -> T
): T {
): RepoSelection {
val decomposedItem = item.split(":")
val repoId = decomposedItem[0]
val repo = repos[repoId] ?: throw NewPlayerException(
"Could find a MediaRepository matching the item $item. Its repo key was apparently: $repoId"
)
return exec(repo, decomposedItem[1])
return RepoSelection(repo, decomposedItem[1])
}

override fun getRepoInfo(): MediaRepository.RepoMetaInfo {
Expand All @@ -76,36 +81,40 @@ class MultiRepository(val actualRepositories: List<MultiRepoEntry>) : MediaRepos
)
}

override suspend fun getMetaInfo(item: String) = pickAndExecuteRepo(item) { repo, item ->
repo.getMetaInfo(item)
override fun getHttpDataSourceFactory(item: String) = getActualRepoAndItem(item).let {
it.repo.getHttpDataSourceFactory(it.item)
}

override suspend fun getMetaInfo(item: String) = getActualRepoAndItem(item).let {
it.repo.getMetaInfo(it.item)
}

override suspend fun getStreams(item: String) = pickAndExecuteRepo(item) { repo, item ->
repo.getStreams(item)
override suspend fun getStreams(item: String) = getActualRepoAndItem(item).let {
it.repo.getStreams(it.item)
}

override suspend fun getSubtitles(item: String) = pickAndExecuteRepo(item) { repo, item ->
repo.getSubtitles(item)
override suspend fun getSubtitles(item: String) = getActualRepoAndItem(item).let {
it.repo.getSubtitles(it.item)
}


override suspend fun getPreviewThumbnail(item: String, timestampInMs: Long) =
pickAndExecuteRepo(item) { repo, item ->
repo.getPreviewThumbnail(item, timestampInMs)
getActualRepoAndItem(item).let {
it.repo.getPreviewThumbnail(it.item, timestampInMs)
}

override suspend fun getPreviewThumbnailsInfo(item: String) =
pickAndExecuteRepo(item) { repo, item ->
repo.getPreviewThumbnailsInfo(item)
getActualRepoAndItem(item).let {
it.repo.getPreviewThumbnailsInfo(it.item)
}

override suspend fun getChapters(item: String) =
pickAndExecuteRepo(item) { repo, item ->
repo.getChapters(item)
getActualRepoAndItem(item).let {
it.repo.getChapters(it.item)
}

override suspend fun getTimestampLink(item: String, timestampInSeconds: Long) =
pickAndExecuteRepo(item) { repo, item ->
repo.getTimestampLink(item, timestampInSeconds)
getActualRepoAndItem(item).let {
it.repo.getTimestampLink(it.item, timestampInSeconds)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package net.newpipe.newplayer.repository

import androidx.media3.datasource.HttpDataSource
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -110,6 +111,9 @@ class PrefetchingRepository(
override suspend fun getTimestampLink(item: String, timestampInSeconds: Long) =
cachingRepository.getTimestampLink(item, timestampInSeconds)

override fun getHttpDataSourceFactory(item: String) =
cachingRepository.getHttpDataSourceFactory(item)

/**
* Resets the information weather something was seen before or not.
*/
Expand Down

0 comments on commit 7d091e1

Please sign in to comment.