| @@ -0,0 +1,547 @@ |
| |
1 | package internal |
| |
2 | |
| |
3 | import ( |
| |
4 | "fmt" |
| |
5 | "strings" |
| |
6 | "sync" |
| |
7 | "sync/atomic" |
| |
8 | "time" |
| |
9 | ) |
| |
10 | |
| |
11 | // Placeholder media for non-current playlist rows. Short black frame so idle |
| |
12 | // playlist construction never pulls real streams; on select we replace immediately. |
| |
13 | const mpvEpisodePlaceholder = "av://lavfi:color=c=0x101010:s=16x16:d=1" |
| |
14 | |
| |
15 | const ( |
| |
16 | mpvPlaylistIdleDelay = 3 * time.Second |
| |
17 | mpvPlaylistPollInterval = 400 * time.Millisecond |
| |
18 | mpvPlaylistStableSamples = 3 |
| |
19 | ) |
| |
20 | |
| |
21 | // playlistSlot maps an MPV playlist index to curd episode + audio mode. |
| |
22 | type playlistSlot struct { |
| |
23 | Episode int |
| |
24 | Mode string // "sub" or "dub" |
| |
25 | Label string |
| |
26 | } |
| |
27 | |
| |
28 | // MPVPlaylistController maintains a seamless MPV playlist of episodes (and |
| |
29 | // optional alternate audio entry) without interrupting active playback while |
| |
30 | // it is being built. Population happens only after playback is idle/stable. |
| |
31 | type MPVPlaylistController struct { |
| |
32 | config *CurdConfig |
| |
33 | anime *Anime |
| |
34 | socket string |
| |
35 | done <-chan struct{} |
| |
36 | |
| |
37 | mu sync.Mutex |
| |
38 | slots []playlistSlot |
| |
39 | suppress atomic.Bool // ignore playlist-pos churn we cause ourselves |
| |
40 | lastPos int |
| |
41 | built bool |
| |
42 | preferredMode string |
| |
43 | hasAlternate bool |
| |
44 | alternateMode string |
| |
45 | currentPlaying int // episode number currently intended |
| |
46 | currentMode string |
| |
47 | } |
| |
48 | |
| |
49 | // StartMPVPlaylistController waits until playback is stable, then builds the |
| |
50 | // episode playlist and watches for user selections. Safe to call in a goroutine. |
| |
51 | // Does nothing for android-intent / empty sockets or when disabled in config. |
| |
52 | func StartMPVPlaylistController(config *CurdConfig, anime *Anime, socket string, done <-chan struct{}) { |
| |
53 | if config == nil || anime == nil || !config.MpvEpisodePlaylist { |
| |
54 | return |
| |
55 | } |
| |
56 | if socket == "" || socket == "android-intent" { |
| |
57 | return |
| |
58 | } |
| |
59 | |
| |
60 | c := &MPVPlaylistController{ |
| |
61 | config: config, |
| |
62 | anime: anime, |
| |
63 | socket: socket, |
| |
64 | done: done, |
| |
65 | lastPos: -1, |
| |
66 | preferredMode: normalizeTranslationType(config.SubOrDub), |
| |
67 | currentPlaying: anime.Ep.Number, |
| |
68 | currentMode: normalizeTranslationType(config.SubOrDub), |
| |
69 | } |
| |
70 | if c.preferredMode == "" { |
| |
71 | c.preferredMode = "sub" |
| |
72 | } |
| |
73 | c.currentMode = c.preferredMode |
| |
74 | c.alternateMode = alternateTranslationType(c.preferredMode) |
| |
75 | |
| |
76 | go c.run() |
| |
77 | } |
| |
78 | |
| |
79 | func (c *MPVPlaylistController) closed() bool { |
| |
80 | select { |
| |
81 | case <-c.done: |
| |
82 | return true |
| |
83 | default: |
| |
84 | return false |
| |
85 | } |
| |
86 | } |
| |
87 | |
| |
88 | func (c *MPVPlaylistController) run() { |
| |
89 | if !c.waitUntilIdleStable() { |
| |
90 | return |
| |
91 | } |
| |
92 | if c.closed() { |
| |
93 | return |
| |
94 | } |
| |
95 | |
| |
96 | // Build episode rows without touching the currently playing demuxer path |
| |
97 | // beyond append/insert of placeholders. |
| |
98 | if err := c.buildEpisodePlaylist(); err != nil { |
| |
99 | Log(fmt.Sprintf("MPV playlist build failed: %v", err)) |
| |
100 | // Still watch in case a partial list exists. |
| |
101 | } else { |
| |
102 | c.built = true |
| |
103 | Log("MPV episode playlist ready") |
| |
104 | } |
| |
105 | |
| |
106 | if c.closed() { |
| |
107 | return |
| |
108 | } |
| |
109 | |
| |
110 | // Probe alternate audio only after the list is up — never blocks first frame. |
| |
111 | c.probeAndAttachAlternateAudio() |
| |
112 | |
| |
113 | c.watchPlaylistSelection() |
| |
114 | } |
| |
115 | |
| |
116 | // waitUntilIdleStable waits until MPV reports a real time-pos a few times in a |
| |
117 | // row so we do not mutate the playlist during initial buffering. |
| |
118 | func (c *MPVPlaylistController) waitUntilIdleStable() bool { |
| |
119 | deadline := time.Now().Add(45 * time.Second) |
| |
120 | stable := 0 |
| |
121 | // Initial grace so loadfile/subs settle. |
| |
122 | timer := time.NewTimer(mpvPlaylistIdleDelay) |
| |
123 | defer timer.Stop() |
| |
124 | select { |
| |
125 | case <-c.done: |
| |
126 | return false |
| |
127 | case <-timer.C: |
| |
128 | } |
| |
129 | |
| |
130 | for time.Now().Before(deadline) { |
| |
131 | if c.closed() { |
| |
132 | return false |
| |
133 | } |
| |
134 | pos, err := MPVSendCommand(c.socket, []interface{}{"get_property", "time-pos"}) |
| |
135 | if err == nil && pos != nil { |
| |
136 | if mpvNumber(pos) >= 0 { |
| |
137 | stable++ |
| |
138 | if stable >= mpvPlaylistStableSamples { |
| |
139 | return true |
| |
140 | } |
| |
141 | time.Sleep(mpvPlaylistPollInterval) |
| |
142 | continue |
| |
143 | } |
| |
144 | } |
| |
145 | stable = 0 |
| |
146 | time.Sleep(mpvPlaylistPollInterval) |
| |
147 | } |
| |
148 | Log("MPV playlist: playback never became stable; skipping playlist build") |
| |
149 | return false |
| |
150 | } |
| |
151 | |
| |
152 | func (c *MPVPlaylistController) totalEpisodes() int { |
| |
153 | if c.anime.TotalEpisodes > 0 { |
| |
154 | return c.anime.TotalEpisodes |
| |
155 | } |
| |
156 | providerName, providerID := AnimeProviderID(c.anime) |
| |
157 | if providerID == "" { |
| |
158 | return 0 |
| |
159 | } |
| |
160 | total, err := GetProviderTotalEpisodes(QualifyProviderID(providerName, providerID), c.preferredMode) |
| |
161 | if err != nil || total <= 0 { |
| |
162 | return 0 |
| |
163 | } |
| |
164 | c.anime.TotalEpisodes = total |
| |
165 | return total |
| |
166 | } |
| |
167 | |
| |
168 | func (c *MPVPlaylistController) episodeLabel(ep int, mode string) string { |
| |
169 | name := GetAnimeName(*c.anime) |
| |
170 | base := fmt.Sprintf("Ep %02d", ep) |
| |
171 | if name != "" { |
| |
172 | base = fmt.Sprintf("%s · Ep %02d", name, ep) |
| |
173 | } |
| |
174 | if c.anime.FillerEpisodes != nil && IsEpisodeFiller(c.anime.FillerEpisodes, ep) { |
| |
175 | base += " [Filler]" |
| |
176 | } |
| |
177 | mode = normalizeTranslationType(mode) |
| |
178 | if mode != "" && mode != c.preferredMode { |
| |
179 | base += " · " + strings.ToUpper(mode) |
| |
180 | } else if mode == "dub" { |
| |
181 | base += " · DUB" |
| |
182 | } |
| |
183 | if ep == c.currentPlaying && mode == c.currentMode { |
| |
184 | base += " ◀" |
| |
185 | } |
| |
186 | return base |
| |
187 | } |
| |
188 | |
| |
189 | func (c *MPVPlaylistController) buildEpisodePlaylist() error { |
| |
190 | total := c.totalEpisodes() |
| |
191 | if total <= 0 { |
| |
192 | return fmt.Errorf("unknown total episode count") |
| |
193 | } |
| |
194 | currentEp := c.anime.Ep.Number |
| |
195 | if currentEp < 1 { |
| |
196 | currentEp = 1 |
| |
197 | } |
| |
198 | if currentEp > total { |
| |
199 | currentEp = total |
| |
200 | } |
| |
201 | |
| |
202 | c.suppress.Store(true) |
| |
203 | defer c.suppress.Store(false) |
| |
204 | |
| |
205 | // Title the currently playing item (index 0). |
| |
206 | _ = c.setPlaylistTitle(0, c.episodeLabel(currentEp, c.currentMode)) |
| |
207 | |
| |
208 | // Insert previous episodes at the front (shifts current down). |
| |
209 | for ep := currentEp - 1; ep >= 1; ep-- { |
| |
210 | if c.closed() { |
| |
211 | return nil |
| |
212 | } |
| |
213 | if err := c.insertPlaceholderAt(0, ep, c.preferredMode); err != nil { |
| |
214 | Log(fmt.Sprintf("playlist insert ep %d: %v", ep, err)) |
| |
215 | } |
| |
216 | // Tiny yield so IPC is not flooded mid-watch. |
| |
217 | time.Sleep(30 * time.Millisecond) |
| |
218 | } |
| |
219 | |
| |
220 | // Append future episodes. |
| |
221 | for ep := currentEp + 1; ep <= total; ep++ { |
| |
222 | if c.closed() { |
| |
223 | return nil |
| |
224 | } |
| |
225 | if err := c.appendPlaceholder(ep, c.preferredMode); err != nil { |
| |
226 | Log(fmt.Sprintf("playlist append ep %d: %v", ep, err)) |
| |
227 | } |
| |
228 | time.Sleep(30 * time.Millisecond) |
| |
229 | } |
| |
230 | |
| |
231 | // Rebuild slot map: index i => episode i+1 (preferred mode). |
| |
232 | slots := make([]playlistSlot, 0, total) |
| |
233 | for ep := 1; ep <= total; ep++ { |
| |
234 | slots = append(slots, playlistSlot{ |
| |
235 | Episode: ep, |
| |
236 | Mode: c.preferredMode, |
| |
237 | Label: c.episodeLabel(ep, c.preferredMode), |
| |
238 | }) |
| |
239 | } |
| |
240 | c.mu.Lock() |
| |
241 | c.slots = slots |
| |
242 | c.currentPlaying = currentEp |
| |
243 | c.mu.Unlock() |
| |
244 | |
| |
245 | // Ensure MPV is still on the current episode index without forcing a reload |
| |
246 | // when already correct. |
| |
247 | wantPos := currentEp - 1 |
| |
248 | if pos, err := c.playlistPos(); err == nil && pos != wantPos { |
| |
249 | // Setting playlist-pos would reload — only do it if we drifted. |
| |
250 | // After inserts, mpv usually keeps the playing entry selected. |
| |
251 | Log(fmt.Sprintf("MPV playlist-pos=%d want=%d (leaving as-is to avoid stutter)", pos, wantPos)) |
| |
252 | } |
| |
253 | c.lastPos = wantPos |
| |
254 | if pos, err := c.playlistPos(); err == nil { |
| |
255 | c.lastPos = pos |
| |
256 | } |
| |
257 | return nil |
| |
258 | } |
| |
259 | |
| |
260 | func (c *MPVPlaylistController) insertPlaceholderAt(index, ep int, mode string) error { |
| |
261 | // loadfile <url> insert-at <index> |
| |
262 | _, err := MPVSendCommand(c.socket, []interface{}{"loadfile", mpvEpisodePlaceholder, "insert-at", index}) |
| |
263 | if err != nil { |
| |
264 | // Older mpv: fall back to append (order less perfect but non-fatal). |
| |
265 | _, err = MPVSendCommand(c.socket, []interface{}{"loadfile", mpvEpisodePlaceholder, "append"}) |
| |
266 | if err != nil { |
| |
267 | return err |
| |
268 | } |
| |
269 | } |
| |
270 | return c.setPlaylistTitle(index, c.episodeLabel(ep, mode)) |
| |
271 | } |
| |
272 | |
| |
273 | func (c *MPVPlaylistController) appendPlaceholder(ep int, mode string) error { |
| |
274 | _, err := MPVSendCommand(c.socket, []interface{}{"loadfile", mpvEpisodePlaceholder, "append"}) |
| |
275 | if err != nil { |
| |
276 | return err |
| |
277 | } |
| |
278 | count, err := c.playlistCount() |
| |
279 | if err != nil || count <= 0 { |
| |
280 | return err |
| |
281 | } |
| |
282 | return c.setPlaylistTitle(count-1, c.episodeLabel(ep, mode)) |
| |
283 | } |
| |
284 | |
| |
285 | func (c *MPVPlaylistController) setPlaylistTitle(index int, title string) error { |
| |
286 | // mpv property: playlist/N/title |
| |
287 | _, err := MPVSendCommand(c.socket, []interface{}{"set_property", fmt.Sprintf("playlist/%d/title", index), title}) |
| |
288 | return err |
| |
289 | } |
| |
290 | |
| |
291 | func (c *MPVPlaylistController) playlistPos() (int, error) { |
| |
292 | v, err := MPVSendCommand(c.socket, []interface{}{"get_property", "playlist-pos"}) |
| |
293 | if err != nil { |
| |
294 | return -1, err |
| |
295 | } |
| |
296 | return int(mpvNumber(v) + 0.5), nil |
| |
297 | } |
| |
298 | |
| |
299 | func (c *MPVPlaylistController) playlistCount() (int, error) { |
| |
300 | v, err := MPVSendCommand(c.socket, []interface{}{"get_property", "playlist-count"}) |
| |
301 | if err != nil { |
| |
302 | return 0, err |
| |
303 | } |
| |
304 | return int(mpvNumber(v) + 0.5), nil |
| |
305 | } |
| |
306 | |
| |
307 | // probeAndAttachAlternateAudio checks whether the opposite sub/dub mode has a |
| |
308 | // stream for the *current* episode. If yes, inserts a playlist row so the user |
| |
309 | // can switch audio by selecting it (full stream reload — only when chosen). |
| |
310 | func (c *MPVPlaylistController) probeAndAttachAlternateAudio() { |
| |
311 | if c.closed() || c.anime == nil { |
| |
312 | return |
| |
313 | } |
| |
314 | alt := c.alternateMode |
| |
315 | cfg := *c.config |
| |
316 | cfg.SubOrDub = alt |
| |
317 | |
| |
318 | // Background resolve — does not touch MPV until we know a stream exists. |
| |
319 | result, err := ResolveEpisodeURL(cfg, c.anime, c.currentPlaying) |
| |
320 | if err != nil || len(result.Links) == 0 { |
| |
321 | Log(fmt.Sprintf("MPV playlist: no %s stream for ep %d (%v)", alt, c.currentPlaying, err)) |
| |
322 | c.hasAlternate = false |
| |
323 | return |
| |
324 | } |
| |
325 | c.hasAlternate = true |
| |
326 | |
| |
327 | c.suppress.Store(true) |
| |
328 | defer c.suppress.Store(false) |
| |
329 | |
| |
330 | // Insert alternate entry right after the current episode index. |
| |
331 | c.mu.Lock() |
| |
332 | curIdx := c.currentPlaying - 1 |
| |
333 | if curIdx < 0 { |
| |
334 | curIdx = 0 |
| |
335 | } |
| |
336 | label := c.episodeLabel(c.currentPlaying, alt) |
| |
337 | // Physical insert in mpv |
| |
338 | insertAt := curIdx + 1 |
| |
339 | c.mu.Unlock() |
| |
340 | |
| |
341 | if _, err := MPVSendCommand(c.socket, []interface{}{"loadfile", mpvEpisodePlaceholder, "insert-at", insertAt}); err != nil { |
| |
342 | // Fallback append |
| |
343 | if err := c.appendPlaceholder(c.currentPlaying, alt); err != nil { |
| |
344 | Log(fmt.Sprintf("MPV playlist: failed to add %s entry: %v", alt, err)) |
| |
345 | return |
| |
346 | } |
| |
347 | c.mu.Lock() |
| |
348 | c.slots = append(c.slots, playlistSlot{Episode: c.currentPlaying, Mode: alt, Label: label}) |
| |
349 | c.mu.Unlock() |
| |
350 | return |
| |
351 | } |
| |
352 | _ = c.setPlaylistTitle(insertAt, label) |
| |
353 | |
| |
354 | c.mu.Lock() |
| |
355 | // Insert into slots map at insertAt |
| |
356 | if insertAt >= len(c.slots) { |
| |
357 | c.slots = append(c.slots, playlistSlot{Episode: c.currentPlaying, Mode: alt, Label: label}) |
| |
358 | } else { |
| |
359 | slot := playlistSlot{Episode: c.currentPlaying, Mode: alt, Label: label} |
| |
360 | c.slots = append(c.slots[:insertAt], append([]playlistSlot{slot}, c.slots[insertAt:]...)...) |
| |
361 | } |
| |
362 | c.mu.Unlock() |
| |
363 | Log(fmt.Sprintf("MPV playlist: added %s option for ep %d", alt, c.currentPlaying)) |
| |
364 | } |
| |
365 | |
| |
366 | func (c *MPVPlaylistController) watchPlaylistSelection() { |
| |
367 | for { |
| |
368 | if c.closed() { |
| |
369 | return |
| |
370 | } |
| |
371 | if !IsMPVRunning(c.socket) { |
| |
372 | return |
| |
373 | } |
| |
374 | if c.suppress.Load() { |
| |
375 | time.Sleep(mpvPlaylistPollInterval) |
| |
376 | continue |
| |
377 | } |
| |
378 | pos, err := c.playlistPos() |
| |
379 | if err != nil { |
| |
380 | time.Sleep(mpvPlaylistPollInterval) |
| |
381 | continue |
| |
382 | } |
| |
383 | if pos < 0 { |
| |
384 | time.Sleep(mpvPlaylistPollInterval) |
| |
385 | continue |
| |
386 | } |
| |
387 | if pos == c.lastPos { |
| |
388 | time.Sleep(mpvPlaylistPollInterval) |
| |
389 | continue |
| |
390 | } |
| |
391 | // Debounce rapid scrubbing through the playlist UI. |
| |
392 | time.Sleep(200 * time.Millisecond) |
| |
393 | pos2, err2 := c.playlistPos() |
| |
394 | if err2 != nil || pos2 != pos { |
| |
395 | continue |
| |
396 | } |
| |
397 | c.lastPos = pos |
| |
398 | c.handlePlaylistJump(pos) |
| |
399 | } |
| |
400 | } |
| |
401 | |
| |
402 | func (c *MPVPlaylistController) handlePlaylistJump(pos int) { |
| |
403 | c.mu.Lock() |
| |
404 | if pos < 0 || pos >= len(c.slots) { |
| |
405 | c.mu.Unlock() |
| |
406 | return |
| |
407 | } |
| |
408 | slot := c.slots[pos] |
| |
409 | curEp, curMode := c.currentPlaying, c.currentMode |
| |
410 | c.mu.Unlock() |
| |
411 | |
| |
412 | if slot.Episode == curEp && normalizeTranslationType(slot.Mode) == normalizeTranslationType(curMode) { |
| |
413 | return |
| |
414 | } |
| |
415 | |
| |
416 | Log(fmt.Sprintf("MPV playlist: user selected ep %d (%s) at index %d", slot.Episode, slot.Mode, pos)) |
| |
417 | if err := c.playSlot(slot); err != nil { |
| |
418 | Log(fmt.Sprintf("MPV playlist: failed to play ep %d: %v", slot.Episode, err)) |
| |
419 | CurdOut(fmt.Sprintf("Could not play episode %d: %v", slot.Episode, err)) |
| |
420 | // Try to restore previous playlist position without thrashing. |
| |
421 | c.suppress.Store(true) |
| |
422 | _, _ = MPVSendCommand(c.socket, []interface{}{"set_property", "playlist-pos", c.episodeIndex(curEp, curMode)}) |
| |
423 | c.suppress.Store(false) |
| |
424 | } |
| |
425 | } |
| |
426 | |
| |
427 | func (c *MPVPlaylistController) episodeIndex(ep int, mode string) int { |
| |
428 | c.mu.Lock() |
| |
429 | defer c.mu.Unlock() |
| |
430 | mode = normalizeTranslationType(mode) |
| |
431 | for i, s := range c.slots { |
| |
432 | if s.Episode == ep && normalizeTranslationType(s.Mode) == mode { |
| |
433 | return i |
| |
434 | } |
| |
435 | } |
| |
436 | // Fallback: first slot with that episode number. |
| |
437 | for i, s := range c.slots { |
| |
438 | if s.Episode == ep { |
| |
439 | return i |
| |
440 | } |
| |
441 | } |
| |
442 | if ep > 0 { |
| |
443 | return ep - 1 |
| |
444 | } |
| |
445 | return 0 |
| |
446 | } |
| |
447 | |
| |
448 | func (c *MPVPlaylistController) playSlot(slot playlistSlot) error { |
| |
449 | c.suppress.Store(true) |
| |
450 | defer c.suppress.Store(false) |
| |
451 | |
| |
452 | mode := normalizeTranslationType(slot.Mode) |
| |
453 | if mode == "" { |
| |
454 | mode = c.preferredMode |
| |
455 | } |
| |
456 | |
| |
457 | cfg := *c.config |
| |
458 | cfg.SubOrDub = mode |
| |
459 | |
| |
460 | // Work on a shallow copy for provider id resolution side effects, then copy back. |
| |
461 | anime := c.anime |
| |
462 | prevEp := anime.Ep.Number |
| |
463 | anime.Ep.Number = slot.Episode |
| |
464 | |
| |
465 | result, err := ResolveEpisodeURLForPlayback(cfg, anime, slot.Episode) |
| |
466 | if err != nil || len(result.Links) == 0 { |
| |
467 | anime.Ep.Number = prevEp |
| |
468 | if err == nil { |
| |
469 | err = fmt.Errorf("no streams") |
| |
470 | } |
| |
471 | return err |
| |
472 | } |
| |
473 | |
| |
474 | anime.Ep.Links = result.Links |
| |
475 | applyStreamPlaybackHints(anime, result.Links, result.LinkHints) |
| |
476 | link := PrioritizeLink(result.Links) |
| |
477 | if link == "" { |
| |
478 | anime.Ep.Number = prevEp |
| |
479 | return fmt.Errorf("empty stream link") |
| |
480 | } |
| |
481 | |
| |
482 | title := c.episodeLabel(slot.Episode, mode) |
| |
483 | // Reuse running MPV instance — loadfile replace is the intentional user switch. |
| |
484 | anime.Ep.Player.SocketPath = c.socket |
| |
485 | if _, err := StartVideo(link, []string{}, title, anime); err != nil { |
| |
486 | anime.Ep.Number = prevEp |
| |
487 | return err |
| |
488 | } |
| |
489 | |
| |
490 | anime.Ep.Number = slot.Episode |
| |
491 | anime.Ep.Started = false |
| |
492 | anime.Ep.IsCompleted = false |
| |
493 | anime.Ep.Player.PlaybackTime = 0 |
| |
494 | anime.Ep.Duration = 0 |
| |
495 | anime.Ep.SkipTimes = SkipTimes{} |
| |
496 | |
| |
497 | c.mu.Lock() |
| |
498 | c.currentPlaying = slot.Episode |
| |
499 | c.currentMode = mode |
| |
500 | c.mu.Unlock() |
| |
501 | |
| |
502 | // Refresh skip times in background (non-blocking for playback). |
| |
503 | go func(ep int) { |
| |
504 | if c.anime.MalId > 0 { |
| |
505 | if err := GetAndParseAniSkipData(c.anime.MalId, ep, 0, c.anime); err != nil { |
| |
506 | Log(fmt.Sprintf("AniSkip for ep %d: %v", ep, err)) |
| |
507 | } |
| |
508 | if c.anime.Ep.SkipTimes.Op.Start != c.anime.Ep.SkipTimes.Op.End || |
| |
509 | c.anime.Ep.SkipTimes.Ed.Start != c.anime.Ep.SkipTimes.Ed.End { |
| |
510 | _ = SendSkipTimesToMPV(c.anime) |
| |
511 | } |
| |
512 | } |
| |
513 | }(slot.Episode) |
| |
514 | |
| |
515 | // Rebuild playlist markers after a short idle (titles ◀ indicator, alternate audio). |
| |
516 | go func() { |
| |
517 | time.Sleep(mpvPlaylistIdleDelay) |
| |
518 | if c.closed() { |
| |
519 | return |
| |
520 | } |
| |
521 | // Soft retitle only — avoid full playlist rebuild (would stutter). |
| |
522 | c.retitleSlots() |
| |
523 | // Re-probe alternate for the new episode. |
| |
524 | c.hasAlternate = false |
| |
525 | c.probeAndAttachAlternateAudio() |
| |
526 | }() |
| |
527 | |
| |
528 | CurdOut(fmt.Sprintf("Playing episode %d (%s)", slot.Episode, mode)) |
| |
529 | return nil |
| |
530 | } |
| |
531 | |
| |
532 | func (c *MPVPlaylistController) retitleSlots() { |
| |
533 | c.suppress.Store(true) |
| |
534 | defer c.suppress.Store(false) |
| |
535 | c.mu.Lock() |
| |
536 | slots := append([]playlistSlot(nil), c.slots...) |
| |
537 | curEp, curMode := c.currentPlaying, c.currentMode |
| |
538 | c.mu.Unlock() |
| |
539 | for i, s := range slots { |
| |
540 | label := c.episodeLabel(s.Episode, s.Mode) |
| |
541 | // episodeLabel uses c.currentPlaying for ◀ marker |
| |
542 | _ = curEp |
| |
543 | _ = curMode |
| |
544 | _ = c.setPlaylistTitle(i, label) |
| |
545 | time.Sleep(15 * time.Millisecond) |
| |
546 | } |
| |
547 | } |