Improve casting resume behavior
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 15m49s

This commit is contained in:
MassiveBox 2025-06-07 22:43:22 +02:00
parent 150348c391
commit 4cf9a2a58f
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
3 changed files with 57 additions and 11 deletions

View file

@ -39,16 +39,10 @@ func (c *Controller) StartCasting(selected string, mediaURL string) {
return return
} }
err := c.Model.ConnectToDevice(selected)
if err != nil {
c.LogAndShowError(fmt.Errorf("error connecting to device %v", err))
return
}
go func() { go func() {
err = c.Model.StartCast(mediaURL) err := c.Model.Cast(selected, mediaURL)
if err != nil { if err != nil {
c.LogAndShowError(fmt.Errorf("error starting cast %v", err)) c.LogAndShowError(fmt.Errorf("error starting cast: %v", err))
c.DiscoverDevices()
return return
} }
}() }()
@ -56,6 +50,10 @@ func (c *Controller) StartCasting(selected string, mediaURL string) {
} }
func (c *Controller) ShowReconnecting(reconnecting bool) {
c.View.PopupReconnecting(reconnecting)
}
// Note: these are app codes, they are NOT related to fcast opcodes! // Note: these are app codes, they are NOT related to fcast opcodes!
const ( const (
ActionPlay = 0 ActionPlay = 0
@ -78,7 +76,6 @@ func (c *Controller) PlayerAction(action int, args ...float32) {
} }
func (c *Controller) ExitCasting() { func (c *Controller) ExitCasting() {
c.PlayerAction(ActionStop)
c.DiscoverDevices() c.DiscoverDevices()
} }

View file

@ -7,6 +7,8 @@ import (
"io" "io"
"math" "math"
"net/http" "net/http"
"strings"
"time"
) )
type Model struct { type Model struct {
@ -14,9 +16,11 @@ type Model struct {
DiscoveredDevices []fcast.DiscoveredHost DiscoveredDevices []fcast.DiscoveredHost
Connection *fcast.Connection Connection *fcast.Connection
EventManager *fcast.EventManager EventManager *fcast.EventManager
DeviceName string
Time float32 Time float32
Length float32 Length float32
Volume float32 Volume float32
IsReconnecting bool
} }
func (m *Model) DiscoverDevices() ([]string, error) { func (m *Model) DiscoverDevices() ([]string, error) {
@ -47,10 +51,29 @@ func (m *Model) ConnectToDevice(name string) error {
return err return err
} }
m.Connection = connection m.Connection = connection
m.DeviceName = name
return nil return nil
} }
func (m *Model) StartCast(mediaURL string) error { func (m *Model) Cast(selectedDevice, mediaURL string) error {
err := m.doCast(selectedDevice, mediaURL)
if err != nil {
if errors.Is(err, io.EOF) || strings.Contains(err.Error(), "software caused connection abort") {
m.Controller.ShowReconnecting(true)
time.Sleep(500 * time.Millisecond)
return m.Cast(selectedDevice, mediaURL)
}
}
m.Controller.ShowReconnecting(false)
return err
}
func (m *Model) doCast(selectedDevice, mediaURL string) error {
err := m.ConnectToDevice(selectedDevice)
if err != nil {
return fmt.Errorf("error connecting to device %v", err)
}
m.EventManager = &fcast.EventManager{} m.EventManager = &fcast.EventManager{}
@ -81,6 +104,13 @@ func (m *Model) StartCast(mediaURL string) error {
if err != nil { if err != nil {
return err return err
} }
} else {
m.Volume = 1
}
err = m.Connection.SendMessage(&fcast.PingMessage{})
if err == nil {
m.Controller.ShowReconnecting(false)
} }
return m.Connection.ListenForMessages(m.EventManager) return m.Connection.ListenForMessages(m.EventManager)

View file

@ -18,10 +18,29 @@ type View struct {
Controller *Controller Controller *Controller
Window fyne.Window Window fyne.Window
CastingScreenElements CastingScreenElements CastingScreenElements CastingScreenElements
ReconnectingDialog *dialog.CustomDialog
} }
func (v *View) PopupError(err error) { func (v *View) PopupError(err error) {
fyne.Do(func() {
dialog.ShowError(err, v.Window) dialog.ShowError(err, v.Window)
})
}
func (v *View) PopupReconnecting(show bool) {
if v.ReconnectingDialog != nil && !show {
fyne.DoAndWait(func() {
v.ReconnectingDialog.Dismiss()
v.ReconnectingDialog = nil
})
return
}
if v.ReconnectingDialog == nil && show {
v.ReconnectingDialog = dialog.NewCustomWithoutButtons("Reconnecting to stream...", widget.NewProgressBarInfinite(), v.Window)
fyne.Do(func() {
v.ReconnectingDialog.Show()
})
}
} }
func NewView(controller *Controller) *View { func NewView(controller *Controller) *View {