From e38f103b82c604e38bd8af9c8df04ab3a5ea7f95 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 1 Oct 2024 19:32:55 +0200 Subject: [PATCH 1/4] mpv: Delays the fade in after enable returns --- sources/mpv/source.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sources/mpv/source.go b/sources/mpv/source.go index 9420fa2..38f4834 100644 --- a/sources/mpv/source.go +++ b/sources/mpv/source.go @@ -124,13 +124,20 @@ func (s *MPVSource) Enable() (err error) { } var pfc interface{} - for err == nil && pfc.(bool) { - time.Sleep(250 * time.Millisecond) - pfc, err = conn.Get("core-idle") - } - err = nil + pfc, err = conn.Get("core-idle") - s.FadeIn(conn, 3, 50) + if err == nil && pfc.(bool) { + go func() { + for err == nil && pfc.(bool) { + time.Sleep(250 * time.Millisecond) + pfc, err = conn.Get("core-idle") + } + + s.FadeIn(conn, 3, 50) + }() + } else { + s.FadeIn(conn, 3, 50) + } } return From eb2eeee3c72f248dca1b21cb5ade039f2f4d7ca8 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 6 Oct 2024 17:58:40 +0200 Subject: [PATCH 2/4] mpv: Add error logs --- sources/mpv/source.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sources/mpv/source.go b/sources/mpv/source.go index 9420fa2..e76be0d 100644 --- a/sources/mpv/source.go +++ b/sources/mpv/source.go @@ -1,6 +1,7 @@ package mpv import ( + "errors" "fmt" "log" "os" @@ -75,13 +76,23 @@ func (s *MPVSource) Enable() (err error) { s.process = exec.Command("mpv", opts...) if err = s.process.Start(); err != nil { + log.Println("Unable to launch mpv:", err.Error()) return } go func() { err := s.process.Wait() if err != nil { - s.process.Process.Kill() + var exiterr *exec.ExitError + if errors.As(err, &exiterr) { + if exiterr.ExitCode() > 0 { + log.Printf("mpv exited with error code = %d", exiterr.ExitCode()) + } else { + log.Print("mpv exited successfully") + } + } else { + s.process.Process.Kill() + } } if s.ipcSocketDir != "" { @@ -106,6 +117,7 @@ func (s *MPVSource) Enable() (err error) { err = conn.Open() } if err != nil { + log.Println("Unable to connect to mpv socket:", err.Error()) return err } defer conn.Close() @@ -120,15 +132,21 @@ func (s *MPVSource) Enable() (err error) { err = conn.Set("pause", false) if err != nil { + log.Println("Unable to unpause:", err.Error()) return err } var pfc interface{} + pfc, err = conn.Get("core-idle") + for err == nil && pfc.(bool) { time.Sleep(250 * time.Millisecond) pfc, err = conn.Get("core-idle") } - err = nil + + if err != nil { + log.Println("Unable to retrieve core-idle status:", err.Error()) + } s.FadeIn(conn, 3, 50) } From 207a4562e63ac503a244aea762fa70cc0056afcc Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 9 Oct 2024 09:02:32 +0200 Subject: [PATCH 3/4] Improve LoadableSource implementation --- app.go | 2 +- settings/settings.go | 4 ++-- sources/interfaces.go | 19 +++++++++++++++++-- sources/mpv/source.go | 31 +++++++++++-------------------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app.go b/app.go index 19037cc..c0d2d61 100644 --- a/app.go +++ b/app.go @@ -73,7 +73,7 @@ func (app *App) loadCustomSources() error { if newss, ok := sources.LoadableSources[csrc.Source]; !ok { return fmt.Errorf("Unable to load source #%d: %q: no such source registered", id, csrc.Source) } else { - src, err := newss(csrc.KV) + src, err := newss.LoadSource(csrc.KV) if err != nil { return fmt.Errorf("Unable to load source #%d (%s): %w", id, csrc.Source, err) } diff --git a/settings/settings.go b/settings/settings.go index d8d96bb..4088f75 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -9,8 +9,8 @@ import ( ) type CustomSource struct { - Source string `json:"src"` - KV map[string]string `json:"kv"` + Source string `json:"src"` + KV map[string]interface{} `json:"kv"` } type Settings struct { diff --git a/sources/interfaces.go b/sources/interfaces.go index 9ec5b2d..52b0fce 100644 --- a/sources/interfaces.go +++ b/sources/interfaces.go @@ -1,6 +1,8 @@ package sources -import () +import ( + "encoding/json" +) var ( LoadableSources = map[string]LoadaleSource{} @@ -19,4 +21,17 @@ type PlayingSource interface { CurrentlyPlaying() string } -type LoadaleSource func(map[string]string) (SoundSource, error) +type LoadaleSource struct { + LoadSource func(map[string]interface{}) (SoundSource, error) + Description string + SourceDefinition interface{} +} + +func Unmarshal(in map[string]interface{}, out interface{}) error { + jin, err := json.Marshal(in) + if err != nil { + return err + } + + return json.Unmarshal(jin, out) +} diff --git a/sources/mpv/source.go b/sources/mpv/source.go index e76be0d..c1826f3 100644 --- a/sources/mpv/source.go +++ b/sources/mpv/source.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "path" - "strings" "time" "github.com/DexterLB/mpvipc" @@ -18,31 +17,23 @@ import ( type MPVSource struct { process *exec.Cmd ipcSocketDir string - Name string - Options []string - File string + Name string `json:"name"` + Options []string `json:"opts"` + File string `json:"file"` } func init() { - sources.LoadableSources["mpv"] = NewMPVSource + sources.LoadableSources["mpv"] = sources.LoadaleSource{ + LoadSource: NewMPVSource, + Description: "Play any file, stream or URL through mpv", + SourceDefinition: &MPVSource{}, + } } -func NewMPVSource(kv map[string]string) (sources.SoundSource, error) { +func NewMPVSource(kv map[string]interface{}) (sources.SoundSource, error) { var s MPVSource - - if name, ok := kv["name"]; ok { - s.Name = name - } - - if opts, ok := kv["opts"]; ok { - s.Options = strings.Split(opts, " ") - } - - if file, ok := kv["file"]; ok { - s.File = file - } - - return &s, nil + err := sources.Unmarshal(kv, &s) + return &s, err } func (s *MPVSource) GetName() string { From 7cedd7470646074f82af2b105819209ca3f99dd1 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 9 Oct 2024 09:04:52 +0200 Subject: [PATCH 4/4] Refactor SourceSelection, display it in layout --- ui/src/routes/+layout.svelte | 53 +++++++++++++++++++++++++++++++++--- ui/src/routes/+page.svelte | 44 ------------------------------ 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/ui/src/routes/+layout.svelte b/ui/src/routes/+layout.svelte index 7a51938..fda31dd 100644 --- a/ui/src/routes/+layout.svelte +++ b/ui/src/routes/+layout.svelte @@ -1,15 +1,17 @@ @@ -18,7 +20,50 @@
-
+
+
+ +
+ + {#if $activeSources.length === 0 && $activeInputs.length === 0} +
+ Aucune source active pour l'instant. +
+ {:else} + + {#each $activeSources as source} +
+
+
+ {#if source.currentTitle} + {source.currentTitle} @ {source.name} + {:else} + {source.name} activée + {/if} +
+
+
+ {/each} + {#each $activeInputs as input} +
+
+
+ {#if input.streams.length} + {#each Object.keys(input.streams) as idstream} + {@const title = input.streams[idstream]} + {title} + {/each} + @ {input.name} + {:else} + {input.name} activée + {/if} +
+
+
+ {/each} +
+ {/if} +
diff --git a/ui/src/routes/+page.svelte b/ui/src/routes/+page.svelte index e3aa663..ebb741d 100644 --- a/ui/src/routes/+page.svelte +++ b/ui/src/routes/+page.svelte @@ -2,7 +2,6 @@ import Applications from '$lib/components/Applications.svelte'; import Inputs from '$lib/components/Inputs.svelte'; import Mixer from '$lib/components/Mixer.svelte'; - import SourceSelection from '$lib/components/SourceSelection.svelte'; import { activeSources } from '$lib/stores/sources'; import { activeInputs } from '$lib/stores/inputs'; @@ -10,50 +9,7 @@ let showInactiveInputs = false; -
- -
-
- {#if $activeSources.length === 0 && $activeInputs.length === 0} -
- Aucune source active pour l'instant. -
- {:else} - - {#each $activeSources as source} -
-
-
- {#if source.currentTitle} - {source.currentTitle} @ {source.name} - {:else} - {source.name} activée - {/if} -
-
-
- {/each} - {#each $activeInputs as input} -
-
-
- {#if input.streams.length} - {#each Object.keys(input.streams) as idstream} - {@const title = input.streams[idstream]} - {title} - {/each} - @ {input.name} - {:else} - {input.name} activée - {/if} -
-
-
- {/each} -
- {/if} -