Prefetching

This commit is contained in:
2026-08-27 12:17:00 +02:00
parent 5a904a3286
commit 744a2815f1
3 changed files with 150 additions and 41 deletions
+31 -8
View File
@@ -70,6 +70,7 @@
[closed? #:mutable]
state-lock
command-lock
local-music-indexes
dlna-port)
#:transparent)
@@ -226,6 +227,14 @@
value
(λ ()
(when (eq? handle (player-backend value))
(let* ((music-id (hash-ref full-state 'at-music-id #f))
(audible-index
(and (number? music-id)
(hash-ref (player-local-music-indexes value)
music-id
#f))))
(when (exact-nonnegative-integer? audible-index)
(set-player-current-index! value audible-index)))
(set-player-state! value (normalize-state state))
(set-player-position!
value
@@ -248,7 +257,14 @@
((exn:fail?
(λ (exception)
(set-error! value (exn-message exception)))))
(player-command! value "next" (hasheq)))))
(call-with-semaphore
(player-command-lock value)
(λ ()
(let ((index (next-index value 1)))
;; Queue immediately at decoder EOF, but keep the old track selected
;; until libao reports the new music id as actually audible.
(when index
(play-index! value index #t))))))))
(define (make-local-backend value)
(let ((backend
@@ -351,7 +367,7 @@
(and (exact-nonnegative-integer? index)
(< index (length (player-tracks value)))))
(define (play-index! value index)
(define (play-index! value index [defer-current-index? #f])
(unless (valid-track-index? value index)
(raise-arguments-error
'player-command!
@@ -363,20 +379,26 @@
(with-state-lock
value
(λ ()
(set-player-current-index! value index)
(set-player-state! value 'starting)
(set-player-position! value 0)
(set-player-duration! value (track-duration item))))
(unless defer-current-index?
(set-player-current-index! value index)
(set-player-state! value 'starting)
(set-player-position! value 0)
(set-player-duration! value (track-duration item)))))
(cond
((eq? kind 'local)
(audio-play! backend (track-file item)))
(let ((music-id (audio-play! backend (track-file item))))
(with-state-lock
value
(λ ()
(hash-set! (player-local-music-indexes value)
music-id
index)))))
((eq? kind 'agent)
(let* ((token (fresh-media-token))
(data (agent-track-data item index token))
(following-index (next-index value 1))
(following-item
(and following-index
(not (= following-index index))
(list-ref (player-tracks value) following-index)))
(following-token
(and following-item (fresh-media-token)))
@@ -1027,6 +1049,7 @@
#f
(make-semaphore 1)
(make-semaphore 1)
(make-hash)
dlna-port)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;