mirror of
https://github.com/dirtydishes/dreamio.git
synced 2026-06-06 13:37:24 +00:00
queue vlc subtitles until media starts
This commit is contained in:
parent
151ae3ca5b
commit
c7bb408812
2 changed files with 254 additions and 0 deletions
|
|
@ -28,6 +28,9 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend {
|
||||||
private var lastLoggedState: String?
|
private var lastLoggedState: String?
|
||||||
private var lastBufferingLogTime: Date?
|
private var lastBufferingLogTime: Date?
|
||||||
private var attachedSubtitleURLs = Set<URL>()
|
private var attachedSubtitleURLs = Set<URL>()
|
||||||
|
private var pendingSubtitleCandidates: [SubtitleCandidate] = []
|
||||||
|
private var pendingSubtitleURLs = Set<URL>()
|
||||||
|
private var hasStartedMedia = false
|
||||||
private var didAutoSelectSubtitleTrack = false
|
private var didAutoSelectSubtitleTrack = false
|
||||||
private var didUserSelectSubtitleTrack = false
|
private var didUserSelectSubtitleTrack = false
|
||||||
private var autoSelectedSubtitleTrackID: Int32?
|
private var autoSelectedSubtitleTrackID: Int32?
|
||||||
|
|
@ -54,6 +57,9 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend {
|
||||||
#if canImport(MobileVLCKit)
|
#if canImport(MobileVLCKit)
|
||||||
playbackStartupTask?.cancel()
|
playbackStartupTask?.cancel()
|
||||||
attachedSubtitleURLs.removeAll()
|
attachedSubtitleURLs.removeAll()
|
||||||
|
pendingSubtitleCandidates.removeAll()
|
||||||
|
pendingSubtitleURLs.removeAll()
|
||||||
|
hasStartedMedia = false
|
||||||
didAutoSelectSubtitleTrack = false
|
didAutoSelectSubtitleTrack = false
|
||||||
didUserSelectSubtitleTrack = false
|
didUserSelectSubtitleTrack = false
|
||||||
autoSelectedSubtitleTrackID = nil
|
autoSelectedSubtitleTrackID = nil
|
||||||
|
|
@ -355,6 +361,8 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend {
|
||||||
print("[DreamioVLC] opening mode=\(playbackMode) cachingMs=\(cachingMilliseconds) url=\(URLRedactor.redactedURLString(url.absoluteString))")
|
print("[DreamioVLC] opening mode=\(playbackMode) cachingMs=\(cachingMilliseconds) url=\(URLRedactor.redactedURLString(url.absoluteString))")
|
||||||
#endif
|
#endif
|
||||||
mediaPlayer.play()
|
mediaPlayer.play()
|
||||||
|
hasStartedMedia = true
|
||||||
|
flushPendingSubtitleCandidates()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func addRemoteHeaders(to media: VLCMedia, request: NativePlaybackRequest) {
|
private func addRemoteHeaders(to media: VLCMedia, request: NativePlaybackRequest) {
|
||||||
|
|
@ -371,6 +379,16 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func attachSubtitles(_ candidates: [SubtitleCandidate]) -> Int {
|
private func attachSubtitles(_ candidates: [SubtitleCandidate]) -> Int {
|
||||||
|
guard hasStartedMedia else {
|
||||||
|
let queued = queuePendingSubtitleCandidates(candidates)
|
||||||
|
#if DEBUG
|
||||||
|
if !candidates.isEmpty {
|
||||||
|
print("[DreamioVLC] subtitle candidates=\(candidates.count) queued=\(queued) reason=media-not-started")
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return queued
|
||||||
|
}
|
||||||
|
|
||||||
var attachedCount = 0
|
var attachedCount = 0
|
||||||
var duplicateCount = 0
|
var duplicateCount = 0
|
||||||
let baselineTrackIDs = Set(rawSubtitleTracks().filter { $0.id >= 0 }.map(\.id))
|
let baselineTrackIDs = Set(rawSubtitleTracks().filter { $0.id >= 0 }.map(\.id))
|
||||||
|
|
@ -413,6 +431,36 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend {
|
||||||
return attachedCount
|
return attachedCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func queuePendingSubtitleCandidates(_ candidates: [SubtitleCandidate]) -> Int {
|
||||||
|
var queuedCount = 0
|
||||||
|
candidates.forEach { candidate in
|
||||||
|
guard !attachedSubtitleURLs.contains(candidate.url),
|
||||||
|
!pendingSubtitleURLs.contains(candidate.url)
|
||||||
|
else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingSubtitleURLs.insert(candidate.url)
|
||||||
|
pendingSubtitleCandidates.append(candidate)
|
||||||
|
queuedCount += 1
|
||||||
|
}
|
||||||
|
return queuedCount
|
||||||
|
}
|
||||||
|
|
||||||
|
private func flushPendingSubtitleCandidates() {
|
||||||
|
guard !pendingSubtitleCandidates.isEmpty else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let candidates = pendingSubtitleCandidates
|
||||||
|
pendingSubtitleCandidates.removeAll()
|
||||||
|
pendingSubtitleURLs.removeAll()
|
||||||
|
#if DEBUG
|
||||||
|
print("[DreamioVLC] flushing queued subtitles count=\(candidates.count)")
|
||||||
|
#endif
|
||||||
|
_ = attachSubtitles(candidates)
|
||||||
|
}
|
||||||
|
|
||||||
private func rawSubtitleTracks() -> [SubtitleTrack] {
|
private func rawSubtitleTracks() -> [SubtitleTrack] {
|
||||||
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
|
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
|
||||||
let indexes = mediaPlayer.videoSubTitlesIndexes as? [NSNumber] ?? []
|
let indexes = mediaPlayer.videoSubTitlesIndexes as? [NSNumber] ?? []
|
||||||
|
|
|
||||||
206
docs/turns/2026-05-25-queue-vlc-subtitles-until-media-start.html
Normal file
206
docs/turns/2026-05-25-queue-vlc-subtitles-until-media-start.html
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue