46 lines
903 B
Go
46 lines
903 B
Go
package queue
|
|
|
|
import (
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type EntryCallback func(q *Queue, entry *Entry)
|
|
|
|
type Entry struct {
|
|
Identifier Identifier
|
|
Source audio.Source
|
|
ReadSamples atomic.Uint64
|
|
cancel chan struct{}
|
|
cancelMutex sync.Mutex
|
|
StartCallback EntryCallback
|
|
EndCallback EntryCallback
|
|
RemoveCallback EntryCallback
|
|
}
|
|
|
|
func NewEntry(identifier Identifier, source audio.Source, cancel chan struct{}, start, end, remove EntryCallback) *Entry {
|
|
return &Entry{
|
|
Identifier: identifier,
|
|
Source: source,
|
|
cancel: cancel,
|
|
StartCallback: start,
|
|
EndCallback: end,
|
|
RemoveCallback: remove,
|
|
}
|
|
}
|
|
|
|
func (e *Entry) Done() <-chan struct{} {
|
|
return e.cancel
|
|
}
|
|
|
|
func (e *Entry) Cancel() {
|
|
e.cancelMutex.Lock()
|
|
defer e.cancelMutex.Unlock()
|
|
select {
|
|
case <-e.Done():
|
|
default:
|
|
close(e.cancel)
|
|
}
|
|
}
|