From 032383fae65acb4f9111a463415f47159e179988 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Sun, 10 Mar 2024 05:47:02 -0500 Subject: [PATCH] fix: Fix YouTube TV poll rate after a video is stopped --- internal/device/watch.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/device/watch.go b/internal/device/watch.go index 071296a..124a80e 100644 --- a/internal/device/watch.go +++ b/internal/device/watch.go @@ -23,6 +23,7 @@ import ( const ( StatePlaying = "PLAYING" StateBuffering = "BUFFERING" + StateIdle = "IDLE" StateAd = 1081 NoMutedSegment = -1 @@ -37,6 +38,7 @@ type Device struct { ctx context.Context cancel context.CancelFunc + mu sync.Mutex entry castdns.CastEntry opts []application.ApplicationOption app *application.Application @@ -45,6 +47,7 @@ type Device struct { tickInterval time.Duration ticker *time.Ticker + state string meta VideoMeta segments []sponsorblock.Segment mutedSegmentId int @@ -224,7 +227,11 @@ func (d *Device) tick() error { } } - d.changeTickInterval(config.Default.PlayingInterval) + d.mu.Lock() + defer d.mu.Unlock() + if d.state != StateIdle { + d.changeTickInterval(config.Default.PlayingInterval) + } return nil } @@ -273,14 +280,22 @@ func (d *Device) onMessage(msg *api.CastMessage) { switch msgType { case "RECEIVER_STATUS": appId, _ := jsonparser.GetString(payload, "status", "applications", "[0]", "displayName") - if appId == "YouTube" { + d.mu.Lock() + defer d.mu.Unlock() + if appId == "YouTube" && d.state != StateIdle { d.changeTickInterval(config.Default.PlayingInterval) } case "MEDIA_STATUS": playerState, _ := jsonparser.GetString(payload, "status", "[0]", "playerState") + d.mu.Lock() + defer d.mu.Unlock() + d.state = playerState switch playerState { case StatePlaying, StateBuffering: d.changeTickInterval(config.Default.PlayingInterval) + case StateIdle: + d.changeTickInterval(config.Default.PausedInterval) + d.unmuteSegment() } case "CLOSE": d.unmuteSegment()