Repository navigation
Fix cloud artwork getting stuck on the placeholder (#138) #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/lostf1sh/pixelplayeross/data/image/RemoteArtworkCache.kt
This file contains hidden or 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,88 @@ | ||
| package com.lostf1sh.pixelplayeross.data.image | ||
|
|
||
| import coil.decode.DataSource | ||
| import coil.decode.ImageSource | ||
| import coil.fetch.SourceResult | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okio.FileSystem | ||
| import okio.Path.Companion.toPath | ||
| import java.io.File | ||
| import java.io.IOException | ||
|
|
||
| /** | ||
| * Disk cache shared by the Navidrome and Jellyfin Coil fetchers. | ||
| * | ||
| * Only bodies that carry an image signature are cached: Subsonic servers answer failed | ||
| * `getCoverArt` calls with HTTP 200 and a JSON/XML error, and caching that body would pin a | ||
| * broken cover until the cache is cleared. Downloads land in a temp file that is renamed into | ||
| * place, so a concurrent request never decodes a file another request is still writing. | ||
| */ | ||
| internal object RemoteArtworkCache { | ||
|
|
||
| private const val SIGNATURE_BYTES = 12 | ||
|
|
||
| fun cachedResult(file: File): SourceResult? { | ||
| if (!file.isFile || file.length() == 0L) return null | ||
| // A body cached before this check existed is skipped, not deleted: the next successful | ||
| // download renames over it, and deleting here could remove a fresh file another | ||
| // request renamed into place after the signature was read. | ||
| if (!hasImageSignature(readSignature(file))) return null | ||
| return SourceResult( | ||
| source = ImageSource(file = file.absolutePath.toPath(), fileSystem = FileSystem.SYSTEM), | ||
| mimeType = "image/jpeg", | ||
| dataSource = DataSource.DISK | ||
| ) | ||
| } | ||
|
|
||
| @Throws(IOException::class) | ||
| fun download(client: OkHttpClient, request: Request, file: File): SourceResult { | ||
| client.newCall(request).execute().use { response -> | ||
| if (!response.isSuccessful) throw IOException("HTTP ${response.code}") | ||
| val bytes = response.body.bytes() | ||
| if (!hasImageSignature(bytes)) { | ||
| throw IOException("Response is not an image (Content-Type: ${response.header("Content-Type")})") | ||
| } | ||
|
|
||
| val tempFile = File.createTempFile(file.name, ".tmp", file.parentFile) | ||
| try { | ||
| tempFile.writeBytes(bytes) | ||
| if (!tempFile.renameTo(file)) throw IOException("Could not move artwork into $file") | ||
| } finally { | ||
| tempFile.delete() | ||
| } | ||
|
|
||
| return SourceResult( | ||
| source = ImageSource(file = file.absolutePath.toPath(), fileSystem = FileSystem.SYSTEM), | ||
| mimeType = response.header("Content-Type") ?: "image/jpeg", | ||
| dataSource = DataSource.NETWORK | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| internal fun hasImageSignature(header: ByteArray): Boolean { | ||
| fun matches(offset: Int, vararg expected: Int): Boolean = | ||
| header.size >= offset + expected.size && | ||
| expected.indices.all { header[offset + it].toInt() and 0xFF == expected[it] } | ||
|
|
||
| return matches(0, 0xFF, 0xD8, 0xFF) || // JPEG | ||
| matches(0, 0x89, 0x50, 0x4E, 0x47) || // PNG | ||
| matches(0, 0x47, 0x49, 0x46, 0x38) || // GIF8 | ||
| (matches(0, 0x52, 0x49, 0x46, 0x46) && matches(8, 0x57, 0x45, 0x42, 0x50)) || // RIFF....WEBP | ||
| matches(4, 0x66, 0x74, 0x79, 0x70) || // ISO-BMFF ftyp (HEIF/AVIF) | ||
| matches(0, 0x42, 0x4D) // BMP | ||
| } | ||
|
|
||
| private fun readSignature(file: File): ByteArray = runCatching { | ||
| file.inputStream().use { input -> | ||
| val buffer = ByteArray(SIGNATURE_BYTES) | ||
| var read = 0 | ||
| while (read < SIGNATURE_BYTES) { | ||
| val count = input.read(buffer, read, SIGNATURE_BYTES - read) | ||
| if (count < 0) break | ||
| read += count | ||
| } | ||
| buffer.copyOf(read) | ||
| } | ||
| }.getOrDefault(ByteArray(0)) | ||
| } |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/lostf1sh/pixelplayeross/data/image/SharedCloudArtworkMappers.kt
This file contains hidden or 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,29 @@ | ||
| package com.lostf1sh.pixelplayeross.data.image | ||
|
|
||
| import android.net.Uri | ||
| import coil.map.Mapper | ||
| import coil.request.Options | ||
| import com.lostf1sh.pixelplayeross.data.provider.SharedArtworkContentProvider | ||
|
|
||
| /* | ||
| * The media session rewrites cloud artwork to `content://<pkg>.artwork/cloud/...` for every | ||
| * controller, our own included, so the player UI, notification, and widgets receive that URI. | ||
| * Loading it in-process goes through SharedArtworkContentProvider, which decodes the cover with | ||
| * this same ImageLoader while the outer decode blocks reading the pipe. Coil allows only a few | ||
| * concurrent bitmap decodes, so a handful of such loads hold every decode slot and the inner | ||
| * decodes they wait on never start: all artwork stays stuck on its placeholder until restart. | ||
| * Mapping back to the raw cloud URI lets the Navidrome/Jellyfin fetchers serve it directly. | ||
| */ | ||
|
|
||
| class SharedCloudArtworkStringMapper(private val packageName: String) : Mapper<String, Uri> { | ||
| override fun map(data: String, options: Options): Uri? = | ||
| SharedArtworkContentProvider.parseCloudArtworkUri(data, packageName)?.let(Uri::parse) | ||
| } | ||
|
|
||
| class SharedCloudArtworkUriMapper(private val packageName: String) : Mapper<Uri, Uri> { | ||
| override fun map(data: Uri, options: Options): Uri? { | ||
| if (data.authority != SharedArtworkContentProvider.authority(packageName)) return null | ||
| return SharedArtworkContentProvider.parseCloudArtworkUri(data.toString(), packageName) | ||
| ?.let(Uri::parse) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new mappers send in-process cloud artwork straight to the fetchers, but the mapped URIs have no size parameter. Both fetchers therefore request their 500-pixel default instead of the provider’s 1024-pixel artwork. Covers on larger player surfaces may look less sharp.
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a regression. Before this PR the provider's inner request also loaded
navidrome_cover://<id>with nosizeparameter, so the fetchers already requested 500 px.CLOUD_ARTWORK_SIZE_PXonly set the decode target, not the server request, so the image and its resolution are unchanged. Raising the default request size would be a separate change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right — this is not a regression. Before this change, the provider's inner
ImageLoaderrequest already usednavidrome_cover:///jellyfin_cover://without asizeparameter, so the fetchers requested their existing 500 px default.CLOUD_ARTWORK_SIZE_PXonly controlled the decode target, not the server-requested artwork size. Mapping the URI directly therefore preserves the same source resolution; increasing the request default would be a separate improvement. I'll withdraw this finding.