limit MPV in-memory cache size and make it configurable

This commit is contained in:
Drew Weymouth
2023-03-28 17:34:39 -07:00
parent d67dfa07b3
commit 0a11df8c07
3 changed files with 13 additions and 6 deletions
+3 -1
View File
@@ -80,7 +80,9 @@ func (a *App) readConfig() {
func (a *App) initMPV() error { func (a *App) initMPV() error {
p := player.NewWithClientName(AppName) p := player.NewWithClientName(AppName)
if err := p.Init(a.Config.LocalPlayback.AudioExclusive); err != nil { c := a.Config.LocalPlayback
c.InMemoryCacheSizeMB = clamp(c.InMemoryCacheSizeMB, 10, 500)
if err := p.Init(c.AudioExclusive, c.InMemoryCacheSizeMB); err != nil {
return fmt.Errorf("failed to initialize mpv player: %s", err.Error()) return fmt.Errorf("failed to initialize mpv player: %s", err.Error())
} }
a.Player = p a.Player = p
+6 -4
View File
@@ -48,8 +48,9 @@ type PlaylistPageConfig struct {
} }
type LocalPlaybackConfig struct { type LocalPlaybackConfig struct {
AudioExclusive bool AudioExclusive bool
Volume int InMemoryCacheSizeMB int
Volume int
} }
type ScrobbleConfig struct { type ScrobbleConfig struct {
@@ -98,8 +99,9 @@ func DefaultConfig() *Config {
TracklistColumns: []string{"Artist", "Album", "Time", "Plays"}, TracklistColumns: []string{"Artist", "Album", "Time", "Plays"},
}, },
LocalPlayback: LocalPlaybackConfig{ LocalPlayback: LocalPlaybackConfig{
AudioExclusive: false, AudioExclusive: false,
Volume: 100, InMemoryCacheSizeMB: 30,
Volume: 100,
}, },
Scrobbling: ScrobbleConfig{ Scrobbling: ScrobbleConfig{
Enabled: true, Enabled: true,
+4 -1
View File
@@ -77,7 +77,7 @@ func NewWithClientName(c string) *Player {
// Initializes the Player and makes it ready for playback. // Initializes the Player and makes it ready for playback.
// Most Player functions will return ErrUnitialized if called before Init. // Most Player functions will return ErrUnitialized if called before Init.
func (p *Player) Init(audioExclusive bool) error { func (p *Player) Init(audioExclusive bool, maxCacheMB int) error {
if !p.initialized { if !p.initialized {
m, err := CreateMPV() m, err := CreateMPV()
if err != nil { if err != nil {
@@ -91,6 +91,9 @@ func (p *Player) Init(audioExclusive bool) error {
m.SetOptionString("force-seekable", "yes") m.SetOptionString("force-seekable", "yes")
m.SetOptionString("terminal", "no") m.SetOptionString("terminal", "no")
// limit in-memory cache size
m.SetOptionString("demuxer-max-bytes", fmt.Sprintf("%dMiB", maxCacheMB))
if p.vol < 0 { if p.vol < 0 {
p.vol = 100 p.vol = 100
} }