From e7f7ddfe4aad57906729cdd331260eda370a3c4d Mon Sep 17 00:00:00 2001 From: aynakeya Date: Sun, 25 Aug 2024 10:43:15 -0700 Subject: [PATCH] maybe fix panic when close the software --- gui/player_videoplayer.go | 3 --- internal/player/mpv/mpv.go | 30 ++++++++++++++++++++++++++---- todo.txt | 2 ++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/gui/player_videoplayer.go b/gui/player_videoplayer.go index 8ee6b2d..96bcb13 100644 --- a/gui/player_videoplayer.go +++ b/gui/player_videoplayer.go @@ -13,9 +13,6 @@ func setupPlayerWindow() { playerWindow.SetCloseIntercept(func() { playerWindow.Hide() }) - MainWindow.SetOnClosed(func() { - playerWindow.Close() - }) playerWindow.Hide() } diff --git a/internal/player/mpv/mpv.go b/internal/player/mpv/mpv.go index a125c44..b65d952 100644 --- a/internal/player/mpv/mpv.go +++ b/internal/player/mpv/mpv.go @@ -13,6 +13,7 @@ import ( "github.com/aynakeya/go-mpv" "github.com/tidwall/gjson" "math" + "time" ) var running bool = false @@ -60,7 +61,8 @@ func SetupPlayer() { } if e.EventId == mpv.EVENT_SHUTDOWN { log.Info("[MPV Player] libmpv shutdown") - StopPlayer() + // should not call, otherwise StopPlayer gonna be call twice and cause panic + // StopPlayer() } } }() @@ -68,11 +70,31 @@ func SetupPlayer() { func StopPlayer() { cfg.AudioDevice = libmpv.GetPropertyString("audio-device") + log.Debugf("successfully get audio-device and set config %s", cfg.AudioDevice) log.Info("stopping mpv player") running = false - // stop player async, should be closed in the end anyway - go libmpv.TerminateDestroy() - log.Info("mpv player stopped") + done := make(chan struct{}) + + // Stop player async but wait for at most 1 second + go func() { + //err := libmpv.SetOptionString("vo", "no") + //if err != nil { + // log.Error("fail to reseting window handle to 0") + //} + // todo: when call TerminateDestroy after wid has been set, a c code panic will arise. + // maybe because the window mpv attach to has been closed. so handle was closed twice + // for now. just don't destroy it. because it also might fix configuration + // not properly saved issue + libmpv.TerminateDestroy() + close(done) + }() + + select { + case <-done: + log.Info("mpv player stopped") + case <-time.After(1 * time.Second): + log.Error("mpv player stop timed out") + } } var prevPercentPos float64 = 0 diff --git a/todo.txt b/todo.txt index d9ef63c..f7ed7d8 100644 --- a/todo.txt +++ b/todo.txt @@ -18,6 +18,8 @@ ---- Finished +- 2024.08.25 : 添加是否播放闲置歌单选项 +- 2024.08.14 : 网页输出模版上线 - 2024.08.06 : 修复使用身份码连接的时候房管无法切歌的问题 - 2024.07.25 : 或许修复配置无法正确保存的问题 - 2024.07.20 : fyne升级,字体修改为自动加载系统字体, 设置中可设置mpv是否现实歌曲封面