Play from Plex when the stem server is off or unreachable (dev 38)
Stems are used only when the stem server is online or they're fully downloaded. A failed stem open falls back to Plex's own file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac0e962105
commit
1d7576371a
2 files changed
+38
-10
No files matched your search
@@ -639,7 +639,7 @@ final class PlaybackManager: StemEngineListener, @unchecked Sendable {
|
||||
/// True when [track] would play without the home network: its stems (or, lacking stems, its file) are fully cached.
|
||||
func playableOffline(_ track: Track) -> Bool {
|
||||
if let entry = stems.find(track) {
|
||||
return StemQuality.allCases.contains { q in entry.stems.allSatisfy { cache.isComplete(Self.stemKey(entry, $0.file, $0.size, q)) } }
|
||||
return stemsCached(entry)
|
||||
}
|
||||
return track.partKey != nil && cache.isComplete(Self.plexKey(track))
|
||||
}
|
||||
@@ -721,29 +721,57 @@ final class PlaybackManager: StemEngineListener, @unchecked Sendable {
|
||||
}
|
||||
|
||||
private func openDetached(_ track: Track, _ startMs: Int64) async throws -> (TrackSession, StemEntry?) {
|
||||
// Plain Plex files need a playback session first, or a Remote Access upload limit fails them.
|
||||
let plain = stems.find(track) == nil && !cache.isComplete(Self.plexKey(track))
|
||||
// Stems when the stem server can serve them (or they're downloaded); Plex's own file otherwise.
|
||||
// Stemify works as a plain Plex player: a stem server that's off or out of reach only costs the stems.
|
||||
if let entry = await stemsToPlay(track) {
|
||||
do {
|
||||
return try await openOn(track, startMs, entry, nil)
|
||||
} catch {
|
||||
if Task.isCancelled { throw error }
|
||||
Log.shared.error("play", "stems for \(track.title) failed (\(error.localizedDescription)), playing from Plex")
|
||||
Task.detached { [stems] in await stems.refresh() }
|
||||
}
|
||||
}
|
||||
let plain = !cache.isComplete(Self.plexKey(track))
|
||||
let stream = plain ? await plex.streamUrl(track) : nil
|
||||
do {
|
||||
return try await openOn(track, startMs, stream)
|
||||
return try await openOn(track, startMs, nil, stream)
|
||||
} catch {
|
||||
// A dead saved address (left home, network changed): pick a working one and try once more.
|
||||
guard plain, !Task.isCancelled, await plex.reroute("stream failed") else { throw error }
|
||||
return try await openOn(track, startMs, await plex.streamUrl(track))
|
||||
return try await openOn(track, startMs, nil, await plex.streamUrl(track))
|
||||
}
|
||||
}
|
||||
|
||||
private func openOn(_ track: Track, _ startMs: Int64, _ stream: URL?) async throws -> (TrackSession, StemEntry?) {
|
||||
/// The stem entry to play, or nil to play Plex's file: skips stems the stem server can't
|
||||
/// serve right now unless they're fully downloaded.
|
||||
private func stemsToPlay(_ track: Track) async -> StemEntry? {
|
||||
guard let entry = stems.find(track) else { return nil }
|
||||
if stemsCached(entry) { return entry }
|
||||
// Right after launch the first check may still be running: give it a few seconds
|
||||
// rather than sit through a long timeout on a server that isn't there.
|
||||
var waited = 0
|
||||
while case .unknown = stems.status, waited < 30, !Task.isCancelled {
|
||||
try? await Task.sleep(nanoseconds: 100_000_000); waited += 1
|
||||
}
|
||||
if case .online = stems.status { return entry }
|
||||
return nil
|
||||
}
|
||||
|
||||
private func stemsCached(_ entry: StemEntry) -> Bool {
|
||||
StemQuality.allCases.contains { q in entry.stems.allSatisfy { cache.isComplete(Self.stemKey(entry, $0.file, $0.size, q)) } }
|
||||
}
|
||||
|
||||
private func openOn(_ track: Track, _ startMs: Int64, _ entry: StemEntry?, _ stream: URL?) async throws -> (TrackSession, StemEntry?) {
|
||||
try await withCheckedThrowingContinuation { cont in
|
||||
DispatchQueue.global(qos: .userInitiated).async { [self] in
|
||||
cont.resume(with: Result { try open(track, startMs, stream: stream) })
|
||||
cont.resume(with: Result { try open(track, startMs, entry: entry, stream: stream) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens every stem (or the original file) and waits for the decoders to know their format.
|
||||
private func open(_ track: Track, _ startMs: Int64, stream: URL? = nil) throws -> (TrackSession, StemEntry?) {
|
||||
let entry = stems.find(track)
|
||||
private func open(_ track: Track, _ startMs: Int64, entry: StemEntry?, stream: URL? = nil) throws -> (TrackSession, StemEntry?) {
|
||||
var sources: [(name: String, key: String, url: URL?, hint: String)] = []
|
||||
if let entry {
|
||||
let q = qualityFor(entry)
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ settings:
|
||||
# iOS needs a plain number for the bundle version; the label is what the app and releases show.
|
||||
MARKETING_VERSION: "0.9.0"
|
||||
VERSION_LABEL: "0.9.0"
|
||||
CURRENT_PROJECT_VERSION: "37"
|
||||
CURRENT_PROJECT_VERSION: "38"
|
||||
# Dev builds override these (build-ipa.sh dev) so they install next to the release app.
|
||||
APP_ID: org.cdnutter.stemify
|
||||
APP_NAME: Stemify
|
||||
|
||||
Reference in new issue
Block a user