Stop the player freeing a render context libmpv still uses
The segfault: Qt destroys and recreates a widget's QOpenGLContext whenever it moves to another top-level window, then calls initializeGL() again. libmpv permits one render context per handle, so the second creation failed with "There is already a mpv_render_context set" -- and the except branch assigned self._render_ctx = None, dropping the last Python reference to the first context, which libmpv was still holding a function pointer into. python-mpv's MpvRenderContext has no __del__ and free() does not unregister the callback, so the ctypes trampoline was collected while registered and the next frame notification jumped into freed memory. Two invariants fix it. initializeGL() now tears down any existing render context first, so a second call is an ordinary recreation. Teardown clears update_cb, calls free() with the GL context current, and only then drops the reference -- and it is connected to QOpenGLContext.aboutToBeDestroyed, so it runs before the GL context dies instead of never. The local reference during teardown is load-bearing: it is what keeps the trampoline alive until free() returns. Three things were destroying that context. Fullscreen reparented the panel into a new top-level window (twice per toggle) and put it back at the end of the splitter, losing the pane layout; it now fullscreens the main window and hides the chrome, reparenting nothing. The tab cross-fade and the dialog blur both grab() the widget tree, which on an OpenGL surface forces a framebuffer readback and returns black -- the fade is skipped for pages holding the video, and dialogs dim rather than blur. Verified on a real Wayland GL context: ten forced context destroy/recreate cycles re-establish the render context every time, and the full app survives tab switching, six fullscreen toggles and resizes with no render-context error and a clean exit. Before this, the same startup dumped core. Also here because they are one-line consequences of touching _create_mpv: an explicit per-platform hwdec list ending in software decoding, and the restored volume actually reaching mpv -- the slider set its value before connecting its signal, so playback always started at 100. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
"""
|
||||
Fullscreen without reparenting the video widget
|
||||
===============================================
|
||||
|
||||
The obvious implementation -- take the player out of its layout, make it a
|
||||
top-level window, `showFullScreen()` -- is what SageTube did, and it is a
|
||||
reliable way to crash. Reparenting a widget into a new top-level destroys and
|
||||
recreates the QOpenGLContext of every QOpenGLWidget beneath it, and each
|
||||
recreation is another chance to lose the libmpv render context. A single
|
||||
fullscreen toggle did it twice.
|
||||
|
||||
So nothing is reparented here. The **main window** goes fullscreen and the
|
||||
chrome around the video is hidden. QMainWindow.showFullScreen() reuses the
|
||||
same QWindow and native surface -- on Wayland it is an
|
||||
`xdg_toplevel.set_fullscreen` -- so the GL context is untouched and the video
|
||||
never blinks.
|
||||
|
||||
The same mechanism gives "cinema mode" (hide the chrome, stay windowed) for
|
||||
free, and it gives Escape one unambiguous owner.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from PySide6.QtCore import QObject, Qt, Signal
|
||||
from PySide6.QtWidgets import QMainWindow, QWidget
|
||||
|
||||
from ..utils.ytsage_logger import logger
|
||||
|
||||
|
||||
class FullscreenController(QObject):
|
||||
"""
|
||||
Drives fullscreen for the Watch page.
|
||||
|
||||
Owns no widgets and destroys nothing; it only toggles visibility and the
|
||||
main window's state, and can always put things back.
|
||||
"""
|
||||
|
||||
changed = Signal(bool)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
window: QMainWindow,
|
||||
tabs: QWidget,
|
||||
watch_page: QWidget,
|
||||
parent: Optional[QObject] = None,
|
||||
) -> None:
|
||||
super().__init__(parent)
|
||||
self._window = window
|
||||
self._tabs = tabs
|
||||
self._watch_page = watch_page
|
||||
self._active = False
|
||||
self._prev_window_state: Optional[Qt.WindowState] = None
|
||||
self._prev_margins = None
|
||||
|
||||
# ------------------------------------------------------------------ state
|
||||
|
||||
def is_active(self) -> bool:
|
||||
return self._active
|
||||
|
||||
def toggle(self) -> None:
|
||||
self.exit() if self._active else self.enter()
|
||||
|
||||
# ------------------------------------------------------------ transitions
|
||||
|
||||
def enter(self) -> None:
|
||||
if self._active:
|
||||
return
|
||||
try:
|
||||
# Fullscreening while another tab is showing would present an
|
||||
# empty screen, so make sure the video is the visible page first.
|
||||
self._show_watch_tab()
|
||||
|
||||
self._prev_window_state = self._window.windowState()
|
||||
|
||||
tab_bar = getattr(self._tabs, "tab_bar", None)
|
||||
if tab_bar is not None:
|
||||
tab_bar.hide()
|
||||
corner = getattr(self._tabs, "corner_widget", None)
|
||||
if corner is not None:
|
||||
corner.hide()
|
||||
|
||||
queue_panel = getattr(self._watch_page, "queue_panel", None)
|
||||
if queue_panel is not None:
|
||||
queue_panel.hide()
|
||||
|
||||
layout = self._watch_page.layout()
|
||||
if layout is not None:
|
||||
self._prev_margins = layout.contentsMargins()
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self._window.showFullScreen()
|
||||
self._active = True
|
||||
self.changed.emit(True)
|
||||
except Exception as e:
|
||||
logger.exception(f"Entering fullscreen failed: {e}")
|
||||
# Never leave the UI half-hidden with no way back.
|
||||
self.exit()
|
||||
|
||||
def exit(self) -> None:
|
||||
if not self._active and self._prev_window_state is None:
|
||||
return
|
||||
try:
|
||||
tab_bar = getattr(self._tabs, "tab_bar", None)
|
||||
if tab_bar is not None:
|
||||
tab_bar.show()
|
||||
corner = getattr(self._tabs, "corner_widget", None)
|
||||
if corner is not None:
|
||||
corner.show()
|
||||
|
||||
queue_panel = getattr(self._watch_page, "queue_panel", None)
|
||||
if queue_panel is not None:
|
||||
queue_panel.show()
|
||||
|
||||
layout = self._watch_page.layout()
|
||||
if layout is not None and self._prev_margins is not None:
|
||||
layout.setContentsMargins(self._prev_margins)
|
||||
|
||||
if self._prev_window_state is not None:
|
||||
self._window.setWindowState(self._prev_window_state)
|
||||
else:
|
||||
self._window.showNormal()
|
||||
except Exception as e:
|
||||
logger.exception(f"Leaving fullscreen failed: {e}")
|
||||
finally:
|
||||
self._prev_window_state = None
|
||||
self._prev_margins = None
|
||||
self._active = False
|
||||
self.changed.emit(False)
|
||||
|
||||
# ---------------------------------------------------------------- helpers
|
||||
|
||||
def _show_watch_tab(self) -> None:
|
||||
stack = getattr(self._tabs, "stack", None)
|
||||
set_index = getattr(self._tabs, "set_current_index", None)
|
||||
if stack is None or set_index is None:
|
||||
return
|
||||
for i in range(stack.count()):
|
||||
if stack.widget(i) is self._watch_page:
|
||||
set_index(i)
|
||||
return
|
||||
Reference in New Issue
Block a user